// ==UserScript== // @name Reddit search on Google // @version 1.3.4 // @description Adds a button to search Reddit posts with Google // @author Mario O.M. // @namespace https://github.com/marioortizmanero/reddit-search-on-google/ // @include http*://www.google.*/search* // @include http*://google.*/search* // @run-at document-end // @downloadURL none // ==/UserScript== // Change this to false if you don't want an icon const useIcon = true; // Change this to true if you want to add the button to the right of the 'Tools' button const appendRight = false; 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 () { // Creating the element let el = document.createElement('div'); el.className = 'hdtb-mitem'; const link = document.createElement('a'); // Adding the svg icon if (useIcon) { const span = document.createElement('span'); span.className = isImageSearch ? 'm3kSL' : 'bmaJhd iJddsb'; span.style.cssText = 'height:16px;width:16px'; span.innerHTML = redditIcon; link.appendChild(span); } // Hyperlink to add 'site:reddit.com' to the query link.appendChild(document.createTextNode('Reddit')); link.href = window.location.href.replace(queryRegex, (match) => { // Replaces the existing `site` flags return match.search(siteRegex) >= 0 ? match.replace(siteRegex, redditUrl) : match + redditUrl; }); if (isImageSearch) { link.classList.add('NZmxZe'); el = link; } else { el.appendChild(link); } // Inserting the element into Google search if (appendRight) { const toolsBtn = document.querySelector(isImageSearch ? '.ssfWCe' : '.t2vtad'); toolsBtn.parentNode.insertBefore(el, toolsBtn.nextSibling); } else { const menuBar = document.querySelector(isImageSearch ? '.T47uwc' : '.MUFPAc'); if (isImageSearch) { menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]); } else { menuBar.appendChild(el); } } })();