以图搜图

长按图片后出现选择框,点击后可选择搜图方式,点击空白处或退出则消失

目前為 2024-03-29 提交的版本,檢視 最新版本

// ==UserScript==
// @name         以图搜图
// @namespace   https://github.com/Daidai0912/x-via
// @version      0.1
// @description  长按图片后出现选择框,点击后可选择搜图方式,点击空白处或退出则消失
// @author       You
// @match        *://*/*
// @grant        GM_openInTab
// @icon https://cdn.jsdelivr.net/gh/Daidai0912/Dai_dai@master/picture/搜图.png
// ==/UserScript==

(function() {
    'use strict';

    let imageLink = '';
    let selectBox = null;
    var longPressTimer;

    let searchEngines = [
        { name: "以图搜图", url: "exit" },
        { name: "谷歌", url: "https://www.google.com/searchbyimage?safe=off&sbisrc=tg&image_url=" },
        { name: "必应", url: "https://www.bing.com/images/search?view=detailv2&iss=sbi&form=SBIVSP&sbisrc=UrlPaste&q=imgurl:" },
        { name: "百度", url: "https://graph.baidu.com/details?isfromtusoupc=0&promotion_name=pc_image_shituindex&carousel=0&image=" },
        { name: "百度_PC", url: "https://graph.baidu.com/details?isfromtusoupc=1&promotion_name=pc_image_shituindex&carousel=0&image=" },
        { name: "Yandex", url: "https://yandex.com/images/touch/search?family=yes&rpt=imageview&url=" },
        { name: "TinEye", url: "https://tineye.com/search/?url=" },
        { name: "查找动漫", url: "https://trace.moe/?url=" },
        { name: "iqdb", url: "https://iqdb.org/?url=" },
        { name: "3d.iqdb", url: "https://3d.iqdb.org/?url=" },
        { name: "360", url: "https://st.so.com/r?img_url=" },
        { name: "Pixiv", url: "https://saucenao.com/search.php?db=999&url=" },
        { name: "ascii2d", url: "https://ascii2d.net/search/url/" },
        { name: "退出", url: "exit" },
        { name: "打开全部", url: "openall" }
    ];

    document.addEventListener('touchstart', function(event) {
        var target = event.target;
        if (target.tagName === 'IMG') {
            longPressTimer = setTimeout(function() {
                imageLink = target.src;
                if (imageLink.startsWith("http")) {
                    if (selectBox !== null) {
                        selectBox.remove();
                        selectBox = null;
                    }
                    showMenu(event.touches[0].clientX, event.touches[0].clientY);
                }
            }, 300);
        }
    });
    
    document.addEventListener('touchend', function(event) {
        clearTimeout(longPressTimer);
    });

    document.addEventListener('click', function(e) {
        if (selectBox !== null && e.target !== selectBox) {
            selectBox.remove();
            selectBox = null;
        }
    });

    function showMenu(clientX, clientY) {
        selectBox = document.createElement('select');
        selectBox.style.position = 'fixed';
        selectBox.style.top = clientY + 'px';
        selectBox.style.left = clientX + 'px';
        selectBox.style.background = '#87CEFF';
        selectBox.style.border = '1px solid #ffffff';
        selectBox.style.padding = '10px';
        selectBox.style.zIndex = '10000';
        selectBox.style.textAlign = 'center';

        searchEngines.forEach(engine => {
            let option = document.createElement('option');
            option.text = engine.name;
            option.value = engine.url;
            selectBox.appendChild(option);
        });

        selectBox.onchange = function() {
            let selectedValue = selectBox.value;
            if (selectedValue === 'openall') {
                searchEngines.forEach(engine => {
                    if (engine.url !== 'exit' && engine.url !== 'openall') {
                        GM_openInTab(engine.url + imageLink, { active: false, insert: true, setParent: false });
                    }
                });
            } else if (selectedValue === 'exit') {
                selectBox.remove();
                selectBox = null;
            } else {
                GM_openInTab(selectedValue + imageLink, { active: true, insert: true, setParent: false });
            }
        };

        document.body.appendChild(selectBox);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址