// ==UserScript== // @name Reddit on Google Search // @version 1.0.3 // @description Adds a button to search Reddit via Google Search // @author Alexyoe // @namespace https://github.com/Alexyoe/Reddit-on-Google-Search // @license MIT // @include http*://www.google.*/search* // @include http*://google.*/search* // @run-at document-end // @downloadURL none // ==/UserScript== // Settings const iconVisible = true; // Start Code const queryRegex = /q=[^&]+/g; const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g; const redditUrl = "+site%3Areddit.com"; let redditIcon = ''; const isImageSearch = /[?&]tbm=isch/.test(location.search); if (typeof trustedTypes !== "undefined") { const policy = trustedTypes.createPolicy("html", { createHTML: (input) => input, }); redditIcon = policy.createHTML(redditIcon); } (function () { // Create the link element const el = document.createElement("a"); el.className = isImageSearch ? "NZmxZe" : "zItAnd FOU1zf GMT2kb"; // Add icon to the link if (iconVisible) { const span = document.createElement("span"); span.className = isImageSearch ? "m3kSL" : "mUKzod"; span.style.cssText = "height:16px;width:16px;display:block"; span.innerHTML = redditIcon; el.appendChild(span); } // Create the div element for the text const link = document.createElement("div"); link.textContent = "Reddit"; el.appendChild(link); // Add site:reddit.com to the query el.href = window.location.href.replace(queryRegex, (match) => match.search(siteRegex) >= 0 ? match.replace(siteRegex, redditUrl) : match + redditUrl ); // Insert the link into Google search if (isImageSearch) { let menuBar = document.querySelector(".T47uwc"); menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]); } else { let menuBar = document.querySelectorAll(".nfdoRb")[1]; menuBar.appendChild(el); } // Fix Sizing const buttonBox = document.querySelector(".xhjkHe"); buttonBox.style.width = "auto"; })();