Google Search Site Switch

Adds search engines to Google's results page

  1. // ==UserScript==
  2. // @name Google Search Site Switch
  3. // @namespace https://github.com/yookibooki/userscripts
  4. // @description Adds search engines to Google's results page
  5. // @version 1.0
  6. // @match https://www.google.com/search*
  7. // @grant GM_addStyle
  8. // @grant GM_openInTab
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. if (document.readyState === 'loading') {
  14. document.addEventListener('DOMContentLoaded', init);
  15. } else {
  16. init();
  17. }
  18.  
  19. function init() {
  20. const input = document.querySelector('input[name="q"], textarea[name="q"]');
  21. if (!input?.value) return;
  22. const query = encodeURIComponent(input.value);
  23. const engines = [
  24. ['Yandex', 'yandex.com', `https://yandex.com/search/?text=${query}`],
  25. ['Bing', 'bing.com', `https://www.bing.com/search?q=${query}`],
  26. ['DuckDuckGo', 'duckduckgo.com', `https://duckduckgo.com/?q=${query}`],
  27. ['Perplexity', 'perplexity.ai', `https://www.perplexity.ai/search?q=${query}`],
  28. ['Reddit', 'reddit.com', `https://www.reddit.com/search/?q=${query}`],
  29. ['Wikipedia', 'wikipedia.org', `https://en.wikipedia.org/wiki/Special:Search?search=${query}`],
  30. ['ChatGPT', 'chatgpt.com', `https://chatgpt.com/?q=${query}`],
  31. ['GitHub', 'github.com', `https://github.com/search?q=${query}`]
  32. ];
  33.  
  34. GM_addStyle(`
  35. .gm-search-icons {
  36. display: flex;
  37. align-items: center;
  38. margin-left: 8px;
  39. gap: 8px;
  40. }
  41. .gm-search-icons img {
  42. width: 20px;
  43. height: 20px;
  44. transition: opacity 0.2s;
  45. }
  46. .gm-search-icons a:hover img {
  47. opacity: 0.8;
  48. }
  49. `);
  50.  
  51. const container = document.createElement('div');
  52. container.className = 'gm-search-icons';
  53.  
  54. container.innerHTML = engines.map(([name, domain, url]) =>
  55. `<a href="${url}" title="Search on ${name}">
  56. <img src="https://www.google.com/s2/favicons?domain=${domain}"
  57. alt="${name}" width="20" height="20">
  58. </a>`
  59. ).join('');
  60.  
  61. container.addEventListener('click', e => {
  62. const link = e.target.closest('a');
  63. if (link) {
  64. e.preventDefault();
  65. GM_openInTab(link.href, { active: true, insert: true });
  66. }
  67. });
  68.  
  69. const targetSelectors = [
  70. '#hdtb-sc > div > div > div.crJ18e',
  71. '#appbar',
  72. '#hdtb',
  73. '#top_nav'
  74. ];
  75. let target;
  76. for (const selector of targetSelectors) {
  77. target = document.querySelector(selector);
  78. if (target) break;
  79. }
  80. if (target) {
  81. target.appendChild(container);
  82. }
  83. }
  84. })();