Search Engine Switcher

Add a select to switch between search engines

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);

  }



})();