// ==UserScript== // @name Reddit search on Google // @version 1.1 // @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== // Modify this to false if you don't want an icon const useIcon = true; // Modify this to true if you want to add the icon to the right of the 'Tools' button const appendRight = false; const queryRegex = /q=(.*?)([^&]+)/g; const redditUrl = "+site%3Areddit.com"; (function() { // Creating the element var el = document.createElement('div'); el.className = 'hdtb-mitem'; var link = document.createElement('a'); // Adding the svg icon if (useIcon) { var span = document.createElement('span'); span.className = 'HF9Klc ZYMsjf'; span.innerHTML += ''; link.appendChild(span); } // Adding a hyperlink to add 'site:reddit.com' link.appendChild(document.createTextNode('Reddit')); link.href = window.location.href.replace(queryRegex, (match,p1,p2) => { return p2.lastIndexOf(redditUrl)==-1 ? ['q=', p2, redditUrl].join('') : ['q=', p2].join(''); // Only adds the url once }); el.appendChild(link); // Inserting the element into Google search if (appendRight) { var toolsBtn = document.getElementById('hdtb-tls'); toolsBtn.parentNode.insertBefore(el, toolsBtn.nextSibling); } else { var button = document.getElementById('hdtb-msb-vis'); button.appendChild(el); } })();