Greasy Fork

Greasy Fork is available in English.

拖拽搜索和打开链接

拖动搜索和打开链接

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         拖拽搜索和打开链接
// @namespace    http://your.namespace.com
// @version      0.2
// @description  拖动搜索和打开链接
// @author       You
// @match        *://*/*
// @grant        GM_openInTab
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isDragging = false;
    let startX = 0;
    let startY = 0;
    let selectedText = "";

    document.addEventListener('mousedown', function(event) {
        startX = event.clientX;
        startY = event.clientY;
        selectedText = getSelectedText();
        isDragging = true;
    });

    document.addEventListener('mouseup', function() {
        isDragging = false;
    });

    document.addEventListener('mousemove', function(event) {
        if (isDragging) {
            const deltaX = event.clientX - startX;
            const deltaY = event.clientY - startY;

            if (deltaX > 50) { // Adjust the threshold as needed
                if (selectedText) {
                    googleSearch(selectedText);
                } else {
                    openLink();
                }

                isDragging = false;
            }
        }
    });

    function getSelectedText() {
        return window.getSelection().toString();
    }

    function googleSearch(query) {
        GM_openInTab('https://www.google.com/search?q=' + encodeURIComponent(query), {
            active: false,
            insert: true,
            setParent: true
        });
    }

    function openLink() {
        const link = getLinkUnderCursor();
        if (link) {
            GM_openInTab(link, {
                active: false,
                insert: true,
                setParent: true
            });
        }
    }

    function getLinkUnderCursor() {
        const element = document.elementFromPoint(startX, startY);
        if (element.tagName === 'A' && element.href) {
            return element.href;
        }
        return null;
    }
})();