Greasy Fork

拖拽搜图

拖拽图片进行以图搜图,左拖百度,右拖谷歌,下拖Yandex

目前为 2024-12-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         拖拽搜图
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  拖拽图片进行以图搜图,左拖百度,右拖谷歌,下拖Yandex
// @author       Your name
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isDragging = false;
    let initialMouseX = 0;
    let initialMouseY = 0;

    // 搜索引擎URL模板
    const searchEngines = {
        google: "https://lens.google.com/uploadbyurl?url=",
        baidu: "https://graph.baidu.com/details?isfromtusoupc=1&tn=pc&carousel=0&promotion_name=pc_image_shituindex&extUiData%5bisLogoShow%5d=1&image=",
        yandex: "https://yandex.com/images/search?rpt=imageview&url="
    };

    // 监听拖动开始
    document.addEventListener('dragstart', function(e) {
        const imgElement = e.target;
        if (imgElement.tagName === 'IMG') {
            isDragging = true;
            initialMouseX = e.clientX;
            initialMouseY = e.clientY;
        }
    });

    // 监听拖动结束
    document.addEventListener('dragend', function(e) {
        if (!isDragging) return;

        const imgElement = e.target;
        if (imgElement.tagName === 'IMG') {
            const currentMouseX = e.clientX;
            const currentMouseY = e.clientY;
            const deltaX = currentMouseX - initialMouseX;
            const deltaY = currentMouseY - initialMouseY;

            // 获取图片URL
            let imageUrl = imgElement.src;

            // 确保图片URL是完整的
            if (imageUrl.startsWith('//')) {
                imageUrl = 'https:' + imageUrl;
            } else if (imageUrl.startsWith('/')) {
                imageUrl = window.location.origin + imageUrl;
            }

            // 编码图片URL
            const encodedUrl = encodeURIComponent(imageUrl);
            let searchUrl;

            // 判断拖动方向
            if (Math.abs(deltaX) > Math.abs(deltaY)) {
                // 水平拖动
                if (deltaX < 0) {
                    // 向左:百度搜图
                    searchUrl = searchEngines.baidu + encodedUrl;
                } else {
                    // 向右:谷歌搜图
                    searchUrl = searchEngines.google + encodedUrl;
                }
            } else if (deltaY > 0) {
                // 向下:Yandex搜图
                searchUrl = searchEngines.yandex + encodedUrl;
            }

            // 如果有搜索URL,打开新标签页
            if (searchUrl) {
                window.open(searchUrl, '_blank');
            }
        }

        isDragging = false;
    });

    // 添加拖动提示
    document.addEventListener('mouseover', function(e) {
        if (e.target.tagName === 'IMG') {
            e.target.title = '拖动图片搜索:\n← 左拖百度搜图\n→ 右拖谷歌搜图\n↓ 下拖Yandex搜图';
        }
    });
})();