Search Engine Switcher

Add a select to switch between search engines

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Search Engine Switcher
// @namespace    binjie09Scripts
// @version      1
// @description  Add a select to switch between search engines
// @include      /^https?:\/\/(www\.)?(cn\.)?(baidu|google|bing)\.com/
// @connect      *
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
  'use strict';

  // Create an array with search engine information
  const engines = [
    { name: "Baidu", value: "https://www.baidu.com/s?wd=", icon: "https://www.baidu.com/favicon.ico" },
    { name: "Google", value: "https://www.google.com/search?q=", icon: "https://www.google.com/favicon.ico" },
    { name: "Bing", value: "https://www.bing.com/search?q=", icon: "https://www.bing.com/favicon.ico" }
  ];

  // Get the current search query
  const searchInput = document.querySelector("#kw, textarea[name='q'], input[name='q']");

  const originalQuery = searchInput.value;


  // Create the dropdown select element and options
  const select = document.createElement("select");
  engines.forEach(engine => {
    const option = document.createElement("option");
    option.value = engine.value + encodeURIComponent(originalQuery);
    option.innerHTML = engine.name;
    if (document.location.href.startsWith(engine.value.split("?")[0])) {
      option.selected = true;
    }
    select.appendChild(option);
  });


  // Save the current search engine name to local storage and update the button text when the select is changed
  select.addEventListener("change", function() {
    let selectedEngineName = "";
    engines.forEach(engine => {
      if (engine.value + encodeURIComponent(originalQuery) === select.value) {
        selectedEngineName = engine.name;
      }
    });
    localStorage.setItem("previousEngine", selectedEngineName);
    document.location.href = select.value;
  });

  // Style the select and button elements
  select.style.marginRight = "10px";


  // Append the select and button to the search bar element
  let searchBar = document.querySelector("#form, #sb_form");

  //searchBar.appendChild(select);
  if (searchBar != null) {
      searchBar.appendChild(select);
  } else {
      searchBar = document.querySelector("button[aria-label='Search']");

      searchBar.insertAdjacentElement("afterend", select);

  }



})();