聚合搜索V4

整合百度、F搜、Google、微信、Bing、知乎、知网空间搜索,提高搜索效率。在原作者基础上自行修改了部分内容,原作者链接:https://greasyfork.org/zh-CN/scripts/436652

当前为 2022-11-19 提交的版本,查看 最新版本

// ==UserScript==
// @name         聚合搜索V4
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  整合百度、F搜、Google、微信、Bing、知乎、知网空间搜索,提高搜索效率。在原作者基础上自行修改了部分内容,原作者链接:https://greasyfork.org/zh-CN/scripts/436652
// @author       Liao Brant

// @match        *://www.baidu.com/s*
// @match        *://www.baidu.com/baidu*
// @match        *://kaifa.baidu.com/searchPage*
// @match        *://fsoufsou.com/search*
// @match        *://www.google.com/search*
// @match        *://www.google.com.hk/search*
// @match        *://weixin.sogou.com/weixin*
// @match        *://www.bing.com/search*
// @match        *://cn.bing.com/search*
// @match        *://www.zhihu.com/search*
// @match        *://search.cnki.com.cn/Search/Result*

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

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

// 获取搜索配置
function getSearchConfig() {
  const urlConfig = GM_getValue("SearchList");
  if (!urlConfig || !urlConfig.length) {
    // 默认的网址配置
    const defaultUrlMap = [
      {
        name: "百度",
        searchUrl: "https://www.baidu.com/s?wd=%s",
        keyName: "wd",
      },
      {
        name: "F搜",
        searchUrl: "https://fsoufsou.com/search?q=%s",
        keyName: "q",
      },
      {
        name: "Google",
        searchUrl: "https://www.google.com/search?q=%s",
        keyName: "q",
      },
      {
        name: "开发者",
        searchUrl: "https://kaifa.baidu.com/searchPage?wd=%s&module=SEARCH",
        keyName: "searchPage",
      },
      {
        name: "微信文章",
        searchUrl:
          "https://weixin.sogou.com/weixin?type=2&s_from=input&query=%s",
        keyName: "query",
      },
      {
        name: "Bing",
        searchUrl: "https://www.bing.com/search?ensearch=0&q=%s",
        keyName: "q",
      },
      {
        name: "知乎",
        searchUrl: "https://www.zhihu.com/search?type=content&q=%s",
        keyName: "q",
      },
      {
        name: "知网空间",
        searchUrl: "https://search.cnki.com.cn/Search/Result?content=%s",
        keyName: "q",
      },
    ];
    GM_setValue("SearchList", defaultUrlMap);
    return defaultUrlMap;
  } else {
    return urlConfig;
  }
}

// 搜索网址配置
const urlMapping = getSearchConfig();

// 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) {
    // 判断url是否符合搜索配置的条件
    if (
      item.searchUrl.includes(
        window.location.hostname + window.location.pathname
      )
    ) {
      keywords = getQueryVariable(item.keyName);
      break;
    }
  }
  console.log(keywords);
  return keywords;
}

// 域名
const hostname = window.location.hostname;

let isBlank = GM_getValue("isBlank");

console.log("新标签页打开?", isBlank);
if (isBlank === undefined) {
  GM_setValue("isBlank", false);
  isBlank = false;
}

// 改变打开搜索引擎的方式
const engine = document.getElementsByClassName("search-engine-a");
function triggerAttribute(value) {
  for (const item of engine) {
    item.target = value;
  }
}

// 适配火狐浏览器的百度搜索
const isFirefox = () => {
  if (navigator.userAgent.indexOf("Firefox") > 0) {
    console.warn("[ Firefox ] 🚀");
    urlMapping[0].searchUrl = "https://www.baidu.com/baidu?wd=%s";
  } else {
    return;
  }
};

// 适配cn.bing.com的必应域名
const cnBing = {
  name: "Bing",
  searchUrl: "https://cn.bing.com/search?q=%s",
  keyName: "q",
};
// 匹配到cn.bing就修改必应配置对象
if (window.location.hostname === "cn.bing.com") {
  for (let item of urlMapping) {
    if (item.name === "Bing") {
      item = cnBing;
    }
  }
}

// 添加自定义样式
function addStyle() {
  const styleSheet = document.createElement("style");
  document.head.appendChild(styleSheet);

  styleSheet.textContent = `
  .search-container {
    position: fixed;
    top: 160px;
    left: 20px;
    width: 100px;
    background-color: #EEEEEE;
    font-size: 12px;
    z-index: 99999;
  }

  .search-title {
    display: block;
    text-align: center;
    margin-top: 10px; 
    margin-bottom: 5px;
    font-size: 14px;
    font-weight: bold;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }

  .search-list__item {
    display: flex;
    justify-content: space-between;
    align-items: center; 
    color: #333333 !important;
    padding-right: 6px;
  }
  .search-list__item:hover {
    color: #ffffff !important;
    background-color: #666666;
  }

  .search-list__item .search-engine-a {
    color: inherit !important;
    padding: 10px 6px 10px 20px;
    text-decoration: none;
    flex-grow: 2; 
  }

  .search-list__close {
    width: 12px;
    height: 12px;
    opacity: 0;
    z-index: 10;
  }
  .search-list__close:hover {
    opacity: 1;
  }

`;
}


// 添加节点
function addBox() {
  addStyle();
  isFirefox();
  // 主元素
  const div = document.createElement("div");
  div.id = "search-app-box";
  div.className = "search-container";
  document.body.insertAdjacentElement("afterBegin", div);

  // 标题
  let title = document.createElement("span");
  title.innerText = "聚合搜索";
  title.className = "search-title";
  title.style.textDecoration = isBlank ? "underline" : "";
  title.ondblclick = () => {
    title.style.textDecoration = !isBlank ? "underline" : "";
    GM_setValue("isBlank", !isBlank);
    isBlank = !isBlank;
    triggerAttribute(isBlank ? "_blank" : "");
  };
  div.appendChild(title);

  // 搜索列表
  for (let index in urlMapping) {
    const item = urlMapping[index];
    // 单个搜索引擎
    const searchItem = document.createElement("div");
    searchItem.className = "search-list__item";
    const a_target =
      !item.searchUrl.includes(hostname) && isBlank ? "_blank" : "";

    searchItem.innerHTML = `
      <a class="search-engine-a" href="${item.searchUrl.replace(
        "%s",
        getKeywords()
      )}" ${a_target}>
        ${item.name}
      </a>
    `;
    // 移除按钮
    const closeImg = document.createElement("img");
    closeImg.className = "search-list__close";
    closeImg.src =
      "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzM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C4wMTIxODR6IiBwLWlkPSIyNjgyIiBkYXRhLXNwbS1hbmNob3ItaWQ9ImEzMTN4Ljc3ODEwNjkuMC5pMCIgY2xhc3M9IiIgZmlsbD0iI2UxNjUzMSI+PC9wYXRoPjwvc3ZnPg==";
    // 点击按钮移除节点
    closeImg.onclick = function () {
      urlMapping.splice(index, 1);
      searchItem.remove();
      GM_setValue("SearchList", urlMapping);
    };

    searchItem.appendChild(closeImg);
    div.appendChild(searchItem);
  }
}

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