Google Search Site Switch

Adds search engines to Google's results page

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Google Search Site Switch
// @namespace    https://github.com/yookibooki/userscripts
// @description  Adds search engines to Google's results page
// @version      1.0
// @match        https://www.google.com/search*
// @grant        GM_addStyle
// @grant        GM_openInTab
// ==/UserScript==

(function() {
    'use strict';
  
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', init);
    } else {
      init();
    }

    function init() {
      const input = document.querySelector('input[name="q"], textarea[name="q"]');
      if (!input?.value) return;
  
      const query = encodeURIComponent(input.value);
      const engines = [
        ['Yandex', 'yandex.com', `https://yandex.com/search/?text=${query}`],
        ['Bing', 'bing.com', `https://www.bing.com/search?q=${query}`],
        ['DuckDuckGo', 'duckduckgo.com', `https://duckduckgo.com/?q=${query}`],
        ['Perplexity', 'perplexity.ai', `https://www.perplexity.ai/search?q=${query}`],
        ['Reddit', 'reddit.com', `https://www.reddit.com/search/?q=${query}`],
        ['Wikipedia', 'wikipedia.org', `https://en.wikipedia.org/wiki/Special:Search?search=${query}`],
        ['ChatGPT', 'chatgpt.com', `https://chatgpt.com/?q=${query}`],
        ['GitHub', 'github.com', `https://github.com/search?q=${query}`]
      ];

      GM_addStyle(`
        .gm-search-icons {
          display: flex;
          align-items: center;
          margin-left: 8px;
          gap: 8px;
        }
        .gm-search-icons img {
          width: 20px;
          height: 20px;
          transition: opacity 0.2s;
        }
        .gm-search-icons a:hover img {
          opacity: 0.8;
        }
      `);

      const container = document.createElement('div');
      container.className = 'gm-search-icons';

      container.innerHTML = engines.map(([name, domain, url]) =>
        `<a href="${url}" title="Search on ${name}">
          <img src="https://www.google.com/s2/favicons?domain=${domain}"
               alt="${name}" width="20" height="20">
        </a>`
      ).join('');

      container.addEventListener('click', e => {
        const link = e.target.closest('a');
        if (link) {
          e.preventDefault();
          GM_openInTab(link.href, { active: true, insert: true });
        }
      });

      const targetSelectors = [
        '#hdtb-sc > div > div > div.crJ18e',
        '#appbar',
        '#hdtb',
        '#top_nav'
      ];
  
      let target;
      for (const selector of targetSelectors) {
        target = document.querySelector(selector);
        if (target) break;
      }
  
      if (target) {
        target.appendChild(container);
      }
    }
  })();