// ==UserScript== // @name Github search on Google // @version 2.1.0 // @description Adds a button to search github.com with Google // @author Alexyoe // @namespace https://github.com/Alexyoe/Github-on-Google-Search // @license MIT // @include http*://www.google.*/search* // @include http*://google.*/search* // @run-at document-end // @downloadURL https://update.greasyfork.icu/scripts/462358/Github%20search%20on%20Google.user.js // @updateURL https://update.greasyfork.icu/scripts/462358/Github%20search%20on%20Google.meta.js // ==/UserScript== // Settings 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 githubUrl = "+site%3Agithub.com"; // Github SVG let githubIcon = ''; const isImageSearch = /[?&]tbm=isch/.test(location.search); // Trusted Types for CSP if (typeof trustedTypes !== "undefined") { const p = trustedTypes.createPolicy("html", { createHTML: (x) => x }); githubIcon = p.createHTML(githubIcon); } // Main function to run on load. Waits for nav to load. (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); // Grab the first wrapper
that isn't selected or "AI Mode" const sampleItem = Array.from( document.querySelectorAll('div[role="listitem"]') ).find((item) => { const isSelected = item.querySelector('[selected], [aria-current="page"]'); const text = item.textContent.trim(); return !isSelected && text !== "AI Mode"; }); if (!sampleItem) return; // bail if nothing there // Clone the entire wrapper. const newItem = sampleItem.cloneNode(true); // Inside that clone, find the const btn = newItem.querySelector("a"); btn.href = window.location.href.replace(queryRegex, (m) => m.search(siteRegex) >= 0 ? m.replace(siteRegex, githubUrl) : m + githubUrl ); // find their inner div (jsname) or fallback to the const inner = btn.querySelector("div[jsname]") || btn; // Clear the inner div inner.innerHTML = ""; if (settings.displayMode === "label") { const textWrapper = document.createElement("span"); textWrapper.textContent = "Github"; textWrapper.className = "R1QWuf"; inner.appendChild(textWrapper); } else { const iconWrapper = document.createElement("span"); iconWrapper.className = "R1QWuf"; iconWrapper.style.lineHeight = "17px"; iconWrapper.innerHTML = githubIcon; inner.appendChild(iconWrapper); } // 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"; } })();