// ==UserScript== // @name Github search on Google // @version 2.0.1 // @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 none // ==/UserScript== const queryRegex = /q=[^&]+/g; const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g; const githubUrl = '+site%3Agithub.com'; let githubIcon = ''; const isImageSearch = /[?&]tbm=isch/.test(location.search); if (typeof trustedTypes !== "undefined") { const policy = trustedTypes.createPolicy("html", { createHTML: (input) => input, }); githubIcon = policy.createHTML(githubIcon); } (function () { // Create the link element const el = document.createElement("a"); el.className = isImageSearch ? "NZmxZe" : "zItAnd FOU1zf GMT2kb"; // Add icon to the link const span = document.createElement("span"); span.className = isImageSearch ? "m3kSL" : "mUKzod"; span.style.cssText = "height:16px;width:16px"; span.innerHTML = githubIcon; el.appendChild(span); // Create the div element for the text const link = document.createElement("div"); link.textContent = "Github"; el.appendChild(link); // Add site:github.com to the query el.href = window.location.href.replace(queryRegex, (match) => match.search(siteRegex) >= 0 ? match.replace(siteRegex, githubUrl) : match + githubUrl ); // Insert the link into Google search const menuBar = document.querySelector(isImageSearch ? ".T47uwc" : ".nfdoRb"); if (isImageSearch) { menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]); } else { menuBar.appendChild(el); } // Fix Sizing const buttonBox = document.querySelector(".TrmO7"); buttonBox.classList.add("size-fix"); const buttonBoxCSS = document.createElement("style"); buttonBoxCSS.innerHTML = ".size-fix { min-width: fit-content !important; }"; document.head.appendChild(buttonBoxCSS); })();