// ==UserScript== // @name Reddit on Google Search // @version 1.1.1 // @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 https://update.greasyfork.icu/scripts/462356/Reddit%20on%20Google%20Search.user.js // @updateURL https://update.greasyfork.icu/scripts/462356/Reddit%20on%20Google%20Search.meta.js // ==/UserScript== const settings = { // Choose exactly one: "icon" or "label" displayMode: "icon", btnPosition: "start", // "start", "end", or "afterai" fixSize: false, }; // Start Code const queryRegex = /q=[^&]+/g; const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g; const redditUrl = "+site%3Areddit.com"; // Reddit SVG let redditIcon = ` `; // Trusted Types for CSP if (typeof trustedTypes !== "undefined") { const p = trustedTypes.createPolicy("html", { createHTML: (x) => x }); redditIcon = p.createHTML(redditIcon); } (function waitForNav() { const nav = Array.from( document.querySelectorAll('div[role="navigation"]') ).find((n) => n.querySelector('div[role="listitem"] a')); if (!nav) return setTimeout(waitForNav, 200); // 1) grab the first wrapper
const sampleItem = nav.querySelectorAll(':scope div[role="listitem"]'); if (!sampleItem) return; // bail if nothing there // 2) clone the entire wrapper (this gives us
) const newItem = sampleItem[2].cloneNode(true); // 3) inside that clone, find the (Google uses role="link") const btn = newItem.querySelector("a"); btn.href = window.location.href.replace(queryRegex, (m) => m.search(siteRegex) >= 0 ? m.replace(siteRegex, redditUrl) : m + redditUrl ); // find their inner div (jsname) or fallback to the const inner = btn.querySelector("div[jsname]") || btn; if (settings.displayMode === "label") { const textWrapper = document.createElement("span"); inner.innerHTML = ""; textWrapper.textContent = "Reddit"; textWrapper.className = "R1QWuf"; inner.appendChild(textWrapper); } else { inner.innerHTML = ""; const iconWrapper = document.createElement("span"); iconWrapper.className = "R1QWuf"; iconWrapper.style.lineHeight = "17px"; iconWrapper.innerHTML = redditIcon; inner.appendChild(iconWrapper); } // 4) insert the **wrapper** at the requested position const first = nav.querySelector(':scope div[role="listitem"]'); if (settings.btnPosition === "start") { first ? first.before(newItem) : nav.prepend(newItem); } else if (settings.btnPosition === "end") { nav.append(newItem); } else { first ? first.after(newItem) : nav.append(newItem); } // optional: prevent wrapping if (settings.fixSize) { nav.style.maxWidth = "inherit"; nav.style.overflowX = "auto"; nav.style.whiteSpace = "nowrap"; } })();