聚合搜索

在页面底部显示一个聚合搜索引擎切换导航

当前为 2024-10-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         聚合搜索
// @namespace    http://tampermonkey.net/
// @version      9.9.10
// @description  在页面底部显示一个聚合搜索引擎切换导航
// @author       晚风知我意
// @include      *://*/*
// @grant        unsafeWindow
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-body
// @license     MIT
// ==/UserScript==

const punkDeafultMark = "Google-Bing-Baidu-YandexSearch-MetaSo-Swisscows-Zhihu-360-Quark-Sougou-Toutiao-DuckDuckGo-GitHub-Pinterest";

const searchUrlMap = [
    {name: "谷歌", searchUrl: "https://www.google.com/search?q=", searchkeyName: ["q"], matchUrl:/google\.com.*?search.*?q=/g, mark:"Google"},    
    {name: "必应", searchUrl: "https://www.bing.com/search?q=", searchkeyName: ["q"], matchUrl:/bing\.com.*?search\?q=?/g, mark:"Bing"},
    {name: "百度", searchUrl: "https://baidu.com/s?wd=", searchkeyName: ["wd", "word"], matchUrl:/baidu\.com.*?w(or)?d=?/g, mark:"Baidu"},
    {name: "Yandex", searchUrl: "https://yandex.com/search/?text=", searchkeyName: ["text"], matchUrl:/yandex\.com.*?text=/g, mark:"YandexSearch"},
    {name: "Swisscows", searchUrl: "https://swisscows.com/en/web?query=", searchkeyName: ["query"], matchUrl:/swisscows\.com.*?query=/g, mark:"Swisscows"},
    {name: "秘塔", searchUrl: "https://metaso.cn/?s=itab1&q=", searchkeyName: ["q"], matchUrl:/metaso\.cn.*?q=/g, mark:"MetaSo"},
    {name: "知乎", searchUrl: "https://www.zhihu.com/search?q=", searchkeyName: ["q"], matchUrl:/zhihu\.com\/search.*?q=/g, mark:"Zhihu"},
    {name: "360", searchUrl: "https://www.so.com/s?q=", searchkeyName: ["q"], matchUrl:/\.so\.com.*?q=/g, mark:"360"},
    {name: "夸克", searchUrl: "https://quark.sm.cn/s?q=", searchkeyName: ["q"], matchUrl:/sm\.cn.*?q=/g, mark:"Quark"},
    {name: "搜狗", searchUrl: "https://m.sogou.com/web/searchList.jsp?keyword=", searchkeyName: ["keyword"], matchUrl:/sogou\.com.*?keyword=/g, mark:"Sougou"},
    {name: "头条", searchUrl: "https://so.toutiao.com/search/?keyword=", searchkeyName: ["keyword"], matchUrl:/toutiao\.com.*?keyword=/g, mark:"Toutiao"},
    {name: "DuckDuckGo", searchUrl: "https://duckduckgo.com/?q=", searchkeyName: ["q"], matchUrl:/duckduckgo\.com.*?q=/g, mark:"DuckDuckGo"},
    {name: "GitHub", searchUrl: "https://github.com/search?q=", searchkeyName: ["q"], matchUrl:/github\.com\/search.*?q=/g, mark:"GitHub"},
    {name: "图片搜", searchUrl: "https://www.pinterest.jp/search/pins/?q=", searchkeyName: ["q"], matchUrl:/pinterest\.jp.*?q=/g, mark:"Pinterest"}
];

let lastScrollTop = 0; // 上一次滚动位置
let punkJetBoxVisible = true; // 功能条可见状态

function getKeywords() {
    let keywords = "";
    for (let urlItem of searchUrlMap) {
        if (window.location.href.match(urlItem.matchUrl) != null) {
            for (let keyItem of urlItem.searchkeyName) {
                if (window.location.href.indexOf(keyItem) >= 0) {
                    let url = new URL(window.location.href);
                    keywords = url.searchParams.get(keyItem);
                    if (keywords) {
                        // 存储成功获取的关键词
                        GM_setValue("last_successful_keywords", keywords);
                    }
                    return keywords;
                }
            }
        }
    }

    // 如果没有找到关键词,尝试获取上一次成功的关键词
    return GM_getValue("last_successful_keywords", "");
}

function addSearchBox() {
    const punkJetBox = document.createElement("div");
    punkJetBox.id = "punkjet-search-box";
    punkJetBox.style.position = 'fixed';
    punkJetBox.style.bottom = '0';
    punkJetBox.style.left = '0';
    punkJetBox.style.right = '0';
    punkJetBox.style.padding = '5px 0';
    punkJetBox.style.zIndex = '9999';
    punkJetBox.style.display = 'flex';
    punkJetBox.style.overflowX = 'auto';
    punkJetBox.style.whiteSpace = 'nowrap';
    punkJetBox.style.backgroundColor = 'rgba(255, 255, 255, 0)'; // 设置为透明
    punkJetBox.style.border = '1px solid transparent'; // 设置边框为透明

    const ulList = document.createElement('ul');
    ulList.style.margin = '0';
    ulList.style.padding = '0';
    ulList.style.listStyle = 'none';
    ulList.style.overflowX = 'auto';
    ulList.style.whiteSpace = 'nowrap';
    ulList.style.height = 'auto';

    let fragment = document.createDocumentFragment();
    let showList = GM_getValue("punk_setup_search", punkDeafultMark).split('-');
    for (let showListIndex = 0; showListIndex < showList.length; showListIndex++) {
        for (let index = 0; index < searchUrlMap.length; index++) {
            let item = searchUrlMap[index];
            if (item.mark == showList[showListIndex]) {
                let liItem = document.createElement('li');
                liItem.style.display = 'inline-block';
                liItem.style.marginLeft = '2px';
                liItem.innerHTML = `<button style="padding: 6px 10px; border: none; border-radius: 8px; background-color: rgba(255, 255, 255, 0.8); color: #42A5F5; font-size: 12px; cursor: pointer; box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2); /* 添加向内的阴影效果 */ backdrop-filter: blur(5px); transition: background-color 0.3s, transform 0.2s; outline: none;" id="punk-url-a" url='${item.searchUrl}'>${item.name}</button>`;
                fragment.appendChild(liItem);
                break;
            }
        }
    }
    ulList.appendChild(fragment);
    punkJetBox.appendChild(ulList);
    document.body.appendChild(punkJetBox);

    // 为按钮添加鼠标事件
    const buttons = document.querySelectorAll("#punk-url-a");
    buttons.forEach(button => {
        button.onmouseover = () => button.style.backgroundColor = 'rgba(241, 241, 241, 0.8)';
        button.onmouseout = () => button.style.backgroundColor = 'rgba(255, 255, 255, 0.8)';

        button.onclick = (event) => {
            event.preventDefault();
            let keywords = getKeywords();
            if (keywords) {
                window.open(button.getAttribute("url") + encodeURIComponent(keywords), '_blank');
            } else {
                alert('未找到搜索关键词。使用上一次成功的关键词进行搜索。');
                let lastKeywords = GM_getValue("last_successful_keywords", "");
                if (lastKeywords) {
                    window.open(button.getAttribute("url") + encodeURIComponent(lastKeywords), '_blank');
                } else {
                    alert('没有找到上一次的关键词。');
                }
            }
        };
    });

    // 添加滚动事件监听
    window.addEventListener('scroll', () => {
        const scrollTop = window.scrollY || document.documentElement.scrollTop;

        if (scrollTop < lastScrollTop) {
            // 向上滚动,显示功能条
            punkJetBox.style.display = 'flex';
            punkJetBoxVisible = true;
        } else if (scrollTop > lastScrollTop) {
            // 向下滚动,隐藏功能条
            punkJetBox.style.display = 'none';
            punkJetBoxVisible = false;
        }

        lastScrollTop = scrollTop <= 0 ? 0 : scrollTop; // For Mobile or negative scrolling
    });
}

function injectStyle() {
    const css = `
        #punkjet-search-box {
            display: flex;
            flex-direction: column;
            /* 去掉阴影效果 */
            /* box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.2); */
        }
        #punk-search-navi-box {
            display: flex;
            width: 100%;
            height: auto;
        }
        #punk-search-app-box {
            flex: 1;
            width: 0;
        }
    `;
    const cssNode = document.createElement("style");
    cssNode.setAttribute("type", "text/css");
    cssNode.appendChild(document.createTextNode(css));
    document.head.appendChild(cssNode);
}

(function () {
    "use strict";
    if (getKeywords() != null) {
        if (!GM_getValue("punk_setup_search")) {
            GM_setValue("punk_setup_search", punkDeafultMark);
        }
        addSearchBox();
        injectStyle();
    }
})();

QingJ © 2025

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