壹鍵切換搜索

在常用的搜索引擎頁面中添加互相切換的按鈕。

目前為 2020-05-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name                Search Switcher
// @name:zh-CN          一键切换搜索
// @name:zh-TW          壹鍵切換搜索
// @description         Add links to each other in search engines. Including multiple search modes.
// @description:zh-CN   在常用的搜索引擎页面中添加互相切换的按钮。
// @description:zh-TW   在常用的搜索引擎頁面中添加互相切換的按鈕。

// @author              XanderWang
// @namespace           http://xander/wang
// @homepageURL         http://xander.wang/xx
// @supportURL          https://github.com/h2y/link-fix
// @icon                https://i.loli.net/2020/05/29/DxSmHAy2o53FdUY.png
// @license             GPL-3.0
// @include             *.baidu.com/*
// @exclude             *.baidu.com/link?*
// @include             *.so.com/*
// @include             *.bing.com/*
// @include             *.zhihu.com/search?*
// @include             *.soku.com/*
// @include             *.sogou.com/*
// @include             /^https?://[a-z]+\.google\.[a-z,\.]+/.+$/
// @grant               none
// @run-at              document_end

// @date                05/29/2020
// @modified            05/29/2020
// @version             1.0.0
// ==/UserScript==

{
  const sites = [
    {
      name: '百度',
      host: 'baidu.com',
      link: 'https://www.baidu.com/s',
      key: 'wd'
    },
    {
      name: '必应',
      host: 'bing.com',
      link: 'https://bing.com/search',
      key: 'q'
    },
    {
      name: '谷歌',
      host: 'google.com',
      link: 'https://www.google.com/search',
      key: 'q'
    },
    {
      name: '谷歌镜像',
      host: 'google.fuckcloudnative.io',
      link: 'https://google.fuckcloudnative.io/search',
      key: 'q'
    },
    {
      name: '搜搜',
      host: 'so.com',
      link: 'https://www.so.com/s',
      key: 'q'
    },
    {
      name: '搜狗',
      host: 'sogou.com',
      link: 'https://www.sogou.com/web',
      key: 'query'
    }
  ];

  const css = `
    .search-warpper {
      position: fixed;
      left: 0;
      top: 0;
    }

    .search-switcher {
      position: fixed;
      box-sizing:border-box;
      background-color: #000;
      opacity: 0.2;
      border-radius: 0px 20px;
      color: #fff;
      padding: 5px 5px;
      top: 80px;
      left: 0px;
      z-index: 9999999;
      transition: all 400ms;
    }

    .search-switcher:hover {
      top: 80px;
      left: 0px;
      opacity: 1;
      border-radius: 0 20px;
      box-shadow: 5px 5px 5px #777;
    }
    
    .search-switcher .search-list {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }

    .search-switcher .search-list a {
      color: #0cf;
      height: 25px;
      line-height: 25px;
    }

    .search-switcher .search-list a.mirror {
      font-weight: bold;
    }
    `;

  function setup() {
    console.log('location:', location);
    let site;
    let isMirror;
    for (let s of sites) {
      if (location.host.includes(s.host)) {
        site = s;
      }
    }
    let siteList = sites.filter(({ host }) => !location.hostname.includes(host));
    console.log('siteList:', siteList);
    
    let query = new URLSearchParams(location.search).get(site.key || 'q');
    console.log('site:', site, ',query:', query);
    if( query == null ) {
      return   
    }
    
    const body = document.getElementsByTagName('body')[0];

    // 样式
    const style = document.createElement('style');
    style.innerHTML = css;
    body.appendChild(style);

    // 生成切换框
    const content = document.createElement('div');
    const aTag = ({ link, name, host, mirror, key }) => {
      let className = '';
      let text = name;
      let href = `${link}?${key}=${query}`;
      console.log('href:', href);
      return `<a href='${href}' target='_blank' >${text}</a>`;
    };
    const tags = siteList
      .filter(({ hidden }) => !hidden)
      .map(aTag)
      .join('');

    content.innerHTML = `
    <div id='search-switcher' class='search-switcher'>
      <div id='search-list' class="search-list">${tags}</p>
    </div>`;
    body.appendChild(content);
  }

  let href0 = '';

  !(function init() {
    var href = location.href;
    if (href0 != href) {
      var oldDOM = document.getElementById('search-switcher');
      if (oldDOM) {
        oldDOM.parentNode.removeChild(oldDOM);
      }
      setup();
      href0 = href;
    }
    setTimeout(init, 2222);
  })();
}
//end userScript