CN搜索

整合中文区常见搜索引擎,提高搜索效率。

目前为 2023-10-08 提交的版本。查看 最新版本

// ==UserScript==
// @name         CN搜索
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  整合中文区常见搜索引擎,提高搜索效率。
// @author       github funcdfs

// @match        *://www.baidu.com/s*
// @match        *://*.bing.com/search*
// @match        *://www.duckduckgo.com/*
// @match        *://www.google.com/search*

// @grant        window.onurlchange
// @run-at       document-end

// @license     MIT
// ==/UserScript==

// 针对百度不重载网页
if (window.onurlchange === null) {
    window.addEventListener('urlchange', OnURLChange)
}

// 搜索网址配置
const urlMapping = [
    {
        name: 'DuckDuckGo',
        searchUrl: 'https://www.duckduckgo.com//?q=',
        keyName: 'q',
        testUrl: /https:\/\/www.duckduckgo.com\/search.*/,
    },
    {
        name: 'Bing',
        searchUrl: 'https://www.bing.com/search?ensearch=1&q=',
        keyName: 'q',
        testUrl: /https:\/\/www.bing.com\/search.*/,
    },
    {
        name: 'Google',
        searchUrl: 'https://www.google.com/search?q=',
        keyName: 'q',
        testUrl: /https:\/\/www.google.com\/search.*/,
    },
    {
        name: 'Baidu',
        searchUrl: 'https://www.baidu.com/s?wd=',
        keyName: 'wd',
        testUrl: /https:\/\/www.baidu.com\/s.*/,
    }
];

// JS 获取 url 参数
function getQueryVariable(variable) {
    let query = window.location.search.substring(1);
    let pairs = query.split("&");
    for (let pair of pairs) {
        let [key, value] = pair.split("=");
        if (key == variable) {
            return decodeURIComponent(value);
        }
    }
    return null;
};

// 从 url 中获取搜索关键词
function getKeywords() {
    let keywords = '';

    for (let item of urlMapping) {
        if (item.testUrl.test(window.location.href)) {
            keywords = getQueryVariable(item.keyName);
            break
        }
    }
    return keywords;
};

// 添加节点
function addBox() {
    // 主元素
    var div = document.createElement('div');
    div.id = 'search-app-box';
    div.style = "position: fixed; top: 160px; left: 20px; width: 130px; background-color: #00000000; font-size: 12px;z-index: 99999;";
    // document.body.appendChild(div);
    document.body.insertAdjacentElement("afterBegin", div);

    // 标题
    let title = document.createElement('span');
    title.innerText = "fw原作者没有审美,lj";
    title.style = "display: none; text-align: center; margin-top: 10px; font-size: 14px; font-weight: bold;";
    div.appendChild(title);

    // 搜索列表
    for (let index in urlMapping) {

        let item = urlMapping[index];

        // 还得重写样式,原作者太土了
        let style = "display: block; background-color: #FFFFFFb8; color: #000000; font-size: 16px; margin-bottom: 10px; padding: 10px 0 10px 20px; text-decoration: none;";
        let defaultStyle = style + "border-radius: 24px; color: #333333 !important; box-shadow: 0 2px 5px 1px rgba(64,60,67,.16);";
        let hoverStyle = style + "border-radius: 24px; box-shadow: none; box-shadow: 0 0 5px #2a8017; font: border";

        let a = document.createElement('a');
        a.innerText = item.name;
        a.style = defaultStyle;
        a.id = index;
        a.href = item.searchUrl + getKeywords();

        // 鼠标移入移除效果,相当于 hover
        a.onmouseenter = function () {
            this.style = hoverStyle;
        }
        a.onmouseleave = function () {
            this.style = defaultStyle;
        }

        div.appendChild(a);
    }
};

function OnURLChange() {
    // 删除原菜单,重新添加
    document.getElementById('search-app-box').remove();
    addBox();
}

(function () {
    'use strict';
    window.onload = addBox;
})();

QingJ © 2025

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