Greasy Fork

聚合搜索引擎切换导航[手机版][移动端]

在搜索顶部显示一个聚合搜索引擎切换导航,模拟M浏览器的综合搜索引擎。专注手机网页搜索引擎切换,纯粹的搜索。SearchJump、搜索跳转、聚合搜索。

目前为 2023-03-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         聚合搜索引擎切换导航[手机版][移动端]
// @namespace    http://tampermonkey.net/
// @version      1.0.5
// @description  在搜索顶部显示一个聚合搜索引擎切换导航,模拟M浏览器的综合搜索引擎。专注手机网页搜索引擎切换,纯粹的搜索。SearchJump、搜索跳转、聚合搜索。
// @author       PunkJet
// @home-url     https://greasyfork.org/zh-CN/scripts/462130

// @match        *://www.baidu.com/s*
// @match        *://m.baidu.com/s*
// @match        *://m.baidu.com/*/s*
// @include      *://www.baidu.com/*/s*
// @include      *://m.baidu.com/*/s*
// @match        *://duckduckgo.com/*
// @match        *://www.google.com/search*
// @match        *://www.google.com.hk/search*
// @match        *://www.bing.com/search*
// @match        *://cn.bing.com/search*
// @match        *://www.zhihu.com/search*
// @match        *://m.sogou.com/web/searchList.jsp*

// @match        *//m.sogou.com/web/*
// @match        *://m.douban.com/search*
// @match        *://yandex.com/search*
// @match        *://quark.sm.cn/s*

// @grant        unsafeWindow
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @run-at       document-idle

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

const searchUrlMap = [
{
    name: "必应",
    searchUrl: "https://cn.bing.com/search?q=",
    searchkeyName: ["q"],
    matchUrl:"cn.bing.com",
    mark:"Bing",
},
{
    name: "百度",
    searchUrl: "https://baidu.com/s?wd=",
    searchkeyName: ["wd", "word"],
    matchUrl:"baidu.com",
    mark:"Baidu",
},
{
    name: "谷歌",
    searchUrl: "https://www.google.com/search?q=",
    searchkeyName: ["q"],
    matchUrl:"google.com",
    mark:"Google",
},
{
    name: "知乎",
    searchUrl: "https://www.zhihu.com/search?q=",
    searchkeyName: ["q"],
    matchUrl:"zhihu.com",
    mark:"Zhihu",
},
{
    name: "豆瓣",
    searchUrl: "https://m.douban.com/search/?query=",
    searchkeyName: ["query"],
    matchUrl:"m.douban.com",
    mark:"Douban",
},
{
    name: "夸克",
    searchUrl: "https://quark.sm.cn/s?q=",
    searchkeyName: ["q"],
    matchUrl:"quark.sm.cn",
    mark:"Quark",
},
{
    name: "搜狗",
    searchUrl: "https://m.sogou.com/web/searchList.jsp?keyword=",
    searchkeyName: ["keyword"],
    matchUrl:"m.sogou.com/web",
    mark:"Sougou",
},
{
  name: "Yandex",
  searchUrl: "https://yandex.com/search/touch/?text=",
  searchkeyName: ["text"],
  matchUrl:"yandex.com",
  mark:"Yandex",
},
{
    name: "DuckDuckGo",
    searchUrl: "https://duckduckgo.com/?q=",
    searchkeyName: ["q"],
    matchUrl:"duckduckgo.com",
    mark:"DuckDuckGo",
  }

];

const deafultMark = "Bing-Baidu-Google-Zhihu-Douban-Yandex-Quark-Sougou-DuckDuckGo";

function getSearchKeywords(name) {
    const url_string = window.location.href;
    const url = new URL(url_string);
    return url.searchParams.get(name);
}


function getKeywords() {
    let keywords = "";
    for (let urlItem of searchUrlMap) {
        if( window.location.href.indexOf(urlItem.matchUrl) >= 0 ) {
            for (let keyItem of urlItem.searchkeyName) {
                if ( window.location.href.indexOf(keyItem) >= 0 )
                {
                    keywords = getSearchKeywords(keyItem);
                    return keywords;
                }
            }
        }
    }
    return keywords;
}


function addSearchBox() {
    const searchBox = document.createElement("div");
    searchBox.id = "search-box";

    const appBoxDiv = document.createElement("div");
    appBoxDiv.id = "search-app-box";
    searchBox.appendChild(appBoxDiv);

    var ulList = document.createElement('ul');
    appBoxDiv.appendChild(ulList);

    let fragment = document.createDocumentFragment();//创建一个文档碎片,减少DOM渲染次数
  
    let showList = GM_getValue("setup_search").split('-');
    for (let showListIndex in showList) {
        for (let index in searchUrlMap) {
            let item = searchUrlMap[index];
            if (item.mark == showList[showListIndex]) {
                let liItem = document.createElement('li');
                let a = document.createElement("a");
                a.innerText = item.name;

                if ( window.location.href.indexOf(item.matchUrl) >= 0 ) {
                    a.className = "search-engine-highlight";
                }
                a.href = item.searchUrl + getKeywords();

                liItem.appendChild(a);
                fragment.appendChild(liItem);
                break;
            }
        }
    }
    ulList.appendChild(fragment);

    const setBoxDiv = document.createElement("div");
    setBoxDiv.id = "search-setting-box";
    setBoxDiv.innerHTML = `<span id="btnSet"><svg width="18" height="18" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M34.0003 41L44 24L34.0003 7H14.0002L4 24L14.0002 41H34.0003Z" fill="none" stroke="#444444" stroke-width="4" stroke-linejoin="round"/><path d="M24 29C26.7614 29 29 26.7614 29 24C29 21.2386 26.7614 19 24 19C21.2386 19 19 21.2386 19 24C19 26.7614 21.2386 29 24 29Z" fill="none" stroke="#444444" stroke-width="4" stroke-linejoin="round"/></svg></span>`;
    searchBox.appendChild(setBoxDiv);

    const closeBoxDiv = document.createElement("div");
    closeBoxDiv.id = "search-close-box";
    closeBoxDiv.innerHTML = `<span id="btnClose"><svg width="18" height="18" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z" fill="none" stroke="#444444" stroke-width="4" stroke-linejoin="round"/><path d="M29.6567 18.3432L18.343 29.6569" stroke="#444444" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M18.3433 18.3432L29.657 29.6569" stroke="#444444" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg></span>`;
    searchBox.appendChild(closeBoxDiv);

    document.getElementsByTagName('head')[0].after(searchBox);

  
    let btnSet = document.querySelector("#btnSet");
    btnSet.onclick = function () {
        let sss = prompt("输入需要显示的搜索引擎。" + '\n' + "格式:搜索名-搜索名" + '\n' + "例如:Bing-Baidu-Google"+ '\n' + "支持的搜索:" + '\n' + deafultMark + '\n' + "当前的设置为:"+ '\n' + GM_getValue("setup_search"));
        if (sss) {
            GM_setValue("setup_search", sss);
        }
        //alert("用户设置" + GM_getValue("setup_search"));
    }

    let btnClose = document.querySelector("#btnClose");
    btnClose.onclick = function () {
        searchBox.style = `display:none;`;
    }
}


(function () {
  "use strict";

  const css =
    `
    #search-box {
      opacity:1 !important;
      position: fixed;
      display: -webkit-flex;
      display:flex;
      top: 0px;
      left: 0px;
      width: 100%;
      background-color: #FFFFFF !important;
      font-size: 15px;
      border-radius: 1px;
      z-index: 99999;
    }

    #search-app-box {
      flex:1;
      width: 0;
    }
    #search-setting-box {
      flex: 0 0 30px;
      text-align: center;
      margin: auto;
    }
    #search-close-box {
      flex: 0 0 36px;
      text-align: center;
      margin: auto;

    }

    #search-app-box ul {
      margin: 0;
      padding: 0;
      overflow: hidden;
      overflow-x: auto;
      list-style: none;
      white-space:nowrap;
    }

    #search-app-box ul::-webkit-scrollbar {
      display: none !important;
    }

    #search-app-box li {
      margin-left: 0px;
      display: inline-block;
    }

    #search-app-box ul li a {
      display: block;
      /*color: #767676 !important;*/
      color: #666666 !important;
      padding: 8px;
      text-decoration: none;
      font-weight:bold;
      /*background-color: hsla(211, 60%, 35%, .1);*/
      font-family:Helvetica Neue,Helvetica,Arial,Microsoft Yahei,Hiragino Sans GB,Heiti SC,WenQuanYi Micro Hei,sans-serif;
    }

    .search-engine-highlight  {
      background-color: hsla(211, 60%, 35%, .1) !important;
    }

    body{
      margin-top: 35px !important;
    }

    .his-wrap-new .fix-wrap {
       top:35px !important;
    }

  `
    if (!GM_getValue("setup_search")) {
        GM_setValue("setup_search", deafultMark);
    }
    GM_addStyle(css);
    addSearchBox();

})();