Reddit on Google Search (forked from Alexyoe's original script)

Adds a button to search Reddit via Google Search

当前为 2024-02-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit on Google Search (forked from Alexyoe's original script)
  3. // @version 1.0.7
  4. // @description Adds a button to search Reddit via Google Search
  5. // @author mefengl (original author: Alexyoe)
  6. // @namespace https://github.com/mefengl/Reddit-on-Google-Search
  7. // @license MIT
  8. // @include http*://www.google.*/search*
  9. // @include http*://google.*/search*
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. function useOption(optionName, defaultValue, type = "boolean", enumOptions = []) {
  14. let value = GM_getValue(optionName, defaultValue);
  15.  
  16. if (type === "boolean") {
  17. GM_registerMenuCommand(`Toggle ${optionName} (currently ${value ? "Enabled" : "Disabled"})`, () => {
  18. GM_setValue(optionName, !value);
  19. window.location.reload();
  20. });
  21. } else if (type === "enum") {
  22. GM_registerMenuCommand(`Cycle ${optionName} (currently ${value})`, () => {
  23. const currentIndex = enumOptions.indexOf(value);
  24. const nextIndex = (currentIndex + 1) % enumOptions.length;
  25. GM_setValue(optionName, enumOptions[nextIndex]);
  26. window.location.reload();
  27. });
  28. }
  29.  
  30. return value;
  31. }
  32.  
  33. // Settings
  34. const iconVisible = useOption("iconVisible", true); // Toggle icon visibility
  35. const nameVisible = useOption("nameVisible", true); // Toggle name visibility
  36. const btnPosition = useOption("btnPosition", "end", "enum", ["start", "end"]); // Start or End
  37. const fixSize = useOption("fixSize", false); // Expands the search buttons bar
  38.  
  39. // Start Code
  40. const queryRegex = /q=[^&]+/g;
  41. const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g;
  42. const redditUrl = "+site%3Areddit.com";
  43. let redditIcon =
  44. '<svg class="DCxYpf" foscusable="false" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path d="M0 7.59c.1-.26.15-.54.28-.77.57-1 1.83-1.29 2.79-.67.09.06.15.06.24 0A8.94 8.94 0 017.5 5c.1 0 .15-.04.19-.14l1.17-3.32.05-.15 1.23.3c.56.12 1.12.25 1.68.4.1.02.15 0 .2-.09a1.66 1.66 0 013.06 1.02c-.06.8-.72 1.46-1.5 1.52-.85.06-1.6-.47-1.77-1.28-.04-.17-.1-.23-.27-.27L9.54 2.5l-.88 2.48c.37.05.72.08 1.07.14 1.05.17 2.04.5 2.96 1.04.09.05.14.05.23 0a1.99 1.99 0 012.34 3.2c-.04.04-.08.11-.08.17a3.67 3.67 0 01-.9 2.73 6.36 6.36 0 01-2.9 1.92c-2.47.86-4.93.8-7.35-.22a5.89 5.89 0 01-2.48-1.9A3.56 3.56 0 01.8 9.5c0-.04 0-.1-.03-.11a2.1 2.1 0 01-.76-1.32v-.01-.47zm7.72 6.22c1.5 0 2.67-.21 3.73-.66a5.2 5.2 0 002.15-1.54c.83-1.06.85-2.3.06-3.38-.4-.56-.93-.97-1.52-1.3a8.15 8.15 0 00-3.44-.97 9.04 9.04 0 00-4.17.63c-.83.35-1.58.82-2.15 1.54a2.66 2.66 0 00-.08 3.38c.41.57.95.99 1.55 1.32 1.29.7 2.68.97 3.87.98zm5.7-11.63a.7.7 0 00-.7.7c0 .39.32.7.7.71a.7.7 0 00.72-.72.7.7 0 00-.72-.7zm1.44 6.2a1 1 0 00-.05-1.22c-.29-.34-.79-.48-1.14-.3.49.43.9.92 1.19 1.53zM1.12 8.38c.3-.6.7-1.09 1.19-1.52-.35-.16-.81-.03-1.1.29-.3.33-.34.83-.09 1.23z"/><path d="M7.98 13.1a4.77 4.77 0 01-1.99-.45 3.58 3.58 0 01-.65-.45c-.24-.2-.24-.5-.04-.7.19-.2.47-.19.7.01.33.3.73.44 1.16.52.76.14 1.52.11 2.24-.17.24-.1.45-.24.66-.38.22-.16.5-.16.68.03.18.2.17.5-.04.68-.43.39-.95.62-1.5.73-.4.08-.81.12-1.22.18zM11.67 9.04a1.3 1.3 0 01-2.6 0 1.3 1.3 0 012.6 0zM4.46 9.04c0-.73.6-1.3 1.32-1.3a1.3 1.3 0 11-1.32 1.3z"/></svg>';
  45. const isImageSearch = /[?&]tbm=isch/.test(location.search);
  46.  
  47. // Allow importing SVG
  48. if (typeof trustedTypes !== "undefined") {
  49. const policy = trustedTypes.createPolicy("html", {
  50. createHTML: (input) => input,
  51. });
  52. redditIcon = policy.createHTML(redditIcon);
  53. }
  54.  
  55. // Main function runs on load
  56. (function () {
  57. // Create the main link element
  58. const el = document.createElement("a");
  59. el.className = isImageSearch ? "NZmxZe" : "nPDzT T3FoJb";
  60.  
  61. // Create the div element for the text
  62. const mainDiv = document.createElement("div");
  63. mainDiv.className = "GKS7s";
  64.  
  65. // Create the span to wrap the icon and title
  66. const span = document.createElement("span");
  67. span.style.cssText = "display:inline-flex;gap:5px;";
  68. span.className = isImageSearch ? "m3kSL" : "FMKtTb UqcIvb";
  69.  
  70. // create the div to hold our SVG
  71. const iconDiv = document.createElement("div");
  72. iconDiv.style.cssText = nameVisible
  73. ? "height:16px;width:16px;display:block;fill:white;"
  74. : "height:16px;width:16px;display:block;margin:auto;fill:white;";
  75. iconDiv.innerHTML = redditIcon;
  76.  
  77. // Create the text node to hold the button title
  78. const textNode = document.createTextNode("Reddit");
  79. // Add iconDiv to the span element
  80. if (iconVisible) {
  81. span.appendChild(iconDiv);
  82. }
  83. // Add textNode to the span element
  84. if (nameVisible) {
  85. if (!isImageSearch) {
  86. span.appendChild(textNode);
  87. }
  88. }
  89.  
  90. // Add span to the mainDiv
  91. mainDiv.appendChild(span);
  92. // Add mainDiv to the main link element
  93. el.appendChild(mainDiv);
  94. // Add text node last if isImageSearch is true
  95. if (isImageSearch) {
  96. el.appendChild(textNode);
  97. }
  98.  
  99. // Add site:reddit.com to the query
  100. el.href = window.location.href.replace(queryRegex, (match) =>
  101. match.search(siteRegex) >= 0
  102. ? match.replace(siteRegex, redditUrl)
  103. : match + redditUrl
  104. );
  105.  
  106. // Insert the link into Google search
  107. if (isImageSearch) {
  108. let menuBar = document.querySelector(".T47uwc");
  109. menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]);
  110. } else {
  111. let menuBar = document.querySelectorAll(".IUOThf")[0];
  112. switch (btnPosition) {
  113. case "start":
  114. menuBar.insertBefore(el, menuBar.children[0]);
  115. break;
  116. case "end":
  117. menuBar.appendChild(el);
  118. break;
  119. default:
  120. menuBar.appendChild(el);
  121. break;
  122. }
  123. }
  124.  
  125. // Fix Sizing
  126. if (fixSize) {
  127. const buttonBox = document.querySelector(".xhjkHe");
  128. buttonBox.style.maxWidth = "inherit";
  129. }
  130. })();