Search Engine Switcher (Jerry Modified)

More transparent and working better on both dark and light mode; added stackoverflow, devdocs etc; modified from https://greasyfork.org/en/scripts/446492

当前为 2022-12-07 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Search Engine Switcher (Jerry Modified)
// @namespace    http://tampermonkey.net/
// @version      0.1.28-Jerry
// @description  More transparent and working better on both dark and light mode; added stackoverflow, devdocs etc; modified from https://greasyfork.org/en/scripts/446492
// @homepage     https://greasyfork.org/en/scripts/456231
// @author       https://greasyfork.org/en/users/28298

// @match        *://www.baidu.com/s*
// @match        *://www.baidu.com/baidu*
// @match        *://duckduckgo.com/*
// @match        *://www.google.com/search*
// @match        *://www.google.com.hk/search*
// @match        *://scholar.google.com/*
// @match        *://translate.google.com/*
// @match        *://weixin.sogou.com/weixin*
// @match        *://www.bing.com/search*
// @match        *://cn.bing.com/search*
// @match        *://www.zhihu.com/search*
// @match        *://devdocs.io/*
// @match        *://stackoverflow.com/*


// @grant        unsafeWindow
// @grant        window.onload
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-body

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

// 搜索网址配置 (youtube, maps, not show, i.e., 1 way)
const urlMapping = [
  {
    name: "Google",
    searchUrl: "https://www.google.com/search?q=",
    keyName: "q",
    testUrl: /https:\/\/www.google.com\/search.*/,
  },
  {
    name: "GImages",
    searchUrl: "https://www.google.com/search?tbm=isch&q=",
    keyName: "q",
    testUrl: /https:\/\/www.google.com\/search.*/,
  },
  {
    name: "Youtube*",
    searchUrl: "https://www.youtube.com/results?search_query=",
    keyName: "search_query",
    testUrl: /https:\/\/www.youtube.com\/results.*/,
  },
  {
    name: "GScholars",
    searchUrl: "https://scholar.google.com/scholar?hl=en&as_sdt=0%2C14&oq=&q=",
    keyName: "q",
    testUrl: /https:\/\/scholar.google.com\/scholar.*/,
  },
  {
    name: "GMaps*",
    searchUrl: "https://maps.google.com/maps?q=",
    keyName: "q",
    testUrl: /https:\/\/maps.google.com\/maps.*/,
  },
  {
    name: "GTranslate",
    searchUrl: "https://translate.google.com/?sl=auto&tl=en&op=translate&text=",
    keyName: "text",
    testUrl: /https:\/\/translate.google.com\/*/,
  },
  {
    name: "DuckDuckGo",
    searchUrl: "https://duckduckgo.com/?q=",
    keyName: "q",
    testUrl: /https:\/\/duckduckgo.com\/*/,
  },
  {
    name: "Bing",
    searchUrl: "https://www.bing.com/search?q=",
    keyName: "q",
    testUrl: /https:\/\/www.bing.com\/search.*/,
  },
  {
    name: "Baidu",
    searchUrl: "https://www.baidu.com/s?wd=",
    keyName: "wd",
    testUrl: /https:\/\/www.baidu.com\/s.*/,
  },
  {
    name: "Wechat",
    searchUrl: "https://weixin.sogou.com/weixin?type=2&s_from=input&query=",
    keyName: "query",
    testUrl: /https:\/\/weixin.sogou.com\/weixin.*/,
  },
  {
    name: "Zhihu",
    searchUrl: "https://www.zhihu.com/search?q=",
    keyName: "q",
    testUrl: /https:\/\/www.zhihu.com\/search.*/,
  },
  {
    name: "DevDocs",
    searchUrl: "https://devdocs.io/#q=",
    keyName: "q",
    testUrl: /https:\/\/devdocs.io\/*/,
  },
  {
    name: "StackOverflow",
    searchUrl: "https://stackoverflow.com/search?q=",
    keyName: "q",
    testUrl: /https:\/\/stackoverflow.com\/search.*/,
  },
];

// 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;
    }
  }
  console.log(keywords);
  return keywords;
}

// 适配火狐浏览器的百度搜索
const isFirefox = () => {
  if (navigator.userAgent.indexOf("Firefox") > 0) {
    console.warn("[ Firefox ] 🚀");
    for (var i = 0; i < urlMapping.length; i++) {
      if (urlMapping[i]['name']=='Baidu') {
        break;
      }
    }
    urlMapping[i].searchUrl = "https://www.baidu.com/baidu?wd=";
    urlMapping[i].testUrl = /https:\/\/www.baidu.com\/baidu.*/;
  } else {
    return;
  }
};

// 适配cn.bing.com的必应域名
const cnBing = {
    name: "Bing",
    searchUrl: "https://cn.bing.com/search?q=",
    keyName: "q",
    testUrl: /https:\/\/cn.bing.com\/search.*/,
};

// 匹配到cn.bing就修改必应配置对象
if(window.location.hostname === 'cn.bing.com'){
  for(let item of urlMapping){
    if(item.name === "Bing"){
      item = cnBing
    }
  }
}

// 添加节点
function addBox() {
  isFirefox();
  // 主元素
  const div = document.createElement("div");
  div.id = "search-app-box";
  div.style = `
    position: fixed;
    top: 140px;
    right: 12px;
    width: 100px;
    background-color: hsla(200, 40%, 96%, .0);
    font-size: 12px;
    border-radius: 6px;
    z-index: 99999;`;
  document.body.insertAdjacentElement("afterbegin", div);

  // 标题
  let title = document.createElement("span");
  title.innerText = " ";
  title.style = `
    display: block;
	color: hsla(211, 20%, 64%, .4);
    text-align: center;
    margin-top: 10px;
    margin-bottom: 5px;
    font-size: 12px;
    font-weight: bold;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;`;
  div.appendChild(title);

  // 搜索列表
  for (let index in urlMapping) {
    let item = urlMapping[index];

    // 列表样式
    let style = `
        display: block;
		color: hsla(225, 9%, 36%, .8) !important;
        padding: 8px;
        text-decoration: none;`;
    let defaultStyle = style + "color: hsla(211, 20%, 64%, .4) !important;";
    let hoverStyle =
      style + "background-color: hsla(211, 20%, 64%, .1); color: hsla(211, 100%, 100%, .9) !important;";

    // 设置搜索引擎链接
    let a = document.createElement("a");
    a.innerText = item.name;
    a.style = defaultStyle;
    a.className = "search-engine-a";
    a.href = item.searchUrl + getKeywords();

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

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