// ==UserScript== // @name Github search on Google // @version 2.0.3 // @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== // Settings const iconVisible = true; const nameVisible = true; const btnPosition = "end"; // Start or End // Start Code 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 if (iconVisible) { const span = document.createElement("span"); span.className = isImageSearch ? "m3kSL" : "mUKzod"; span.style.cssText = nameVisible ? "height:16px;width:16px;display:block" : "height:16px;width:16px;display:block;margin:auto"; span.innerHTML = githubIcon; el.appendChild(span); } // Create the div element for the text const link = document.createElement("div"); link.textContent = "Github"; if (nameVisible) { 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) { let menuBar = document.querySelector(".T47uwc"); menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]); } else { let menuBar = document.querySelectorAll(".nfdoRb")[1]; switch (btnPosition) { case "start": menuBar.insertBefore(el, menuBar.children[0]); break; case "end": menuBar.appendChild(el); break default: menuBar.appendChild(el); break; } } // Fix Sizing and Icons const buttonBox = document.querySelector(".xhjkHe"); buttonBox.style.width = "auto"; })();