// ==UserScript== // @run-at document-start // @name General URL Cleaner // @namespace // @description Cleans URL's from various popular sites. Also, makes sure the sites are using HTTPS. // @include *.google.tld/* // @include *.amazon.tld/* // @include *.newegg.tld/* // @include *.ebay.tld/* // @include *.bing.tld/* // @include *.youtube.com/* // @include *.dealtime.com/* // @exclude https://apis.google.com/* // @exclude https://www.google.com/recaptcha/api2/* // @version 1.9.0.3 // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html // @downloadURL none // ==/UserScript== // compile these regexes beforehand to improve efficiency var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/); var google = new RegExp(/^https?:\/\/(www|mail|maps|books|play|news|plus|photos|docs|drive|support)\.google\.(.+?)\/[a-z]*\?/); var googleImageRedirect = new RegExp(/^https?:\/\/www\.google\.(.+?)\/url\?/); var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/); var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/); var ebaySearch = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//); var amazon = new RegExp(/^https?:\/\/www\.amazon\..*\/dp\//); var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/); var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/); // Clean the current page URL var newPageUrl = cleanUrl(document.URL); if (newPageUrl != document.URL) location.replace(newPageUrl); // Cleans links on the page var links = document.links; var excludeLinks = new RegExp(/(^$|^data\:|^javascript\:)/); // don't do anything with links that are blank, javascript, or containing data if (google.test(newPageUrl)) { document.addEventListener("DOMContentLoaded", cleanGooglePageLinks, false); window.onhashchange = googleInstant; } else { document.addEventListener("DOMContentLoaded", cleanPageLinks, false); } // Standard link cleaning function function cleanPageLinks() { for (var i = links.length; i--;) { if (excludeLinks.test(links[i].href)) continue; // Links to skip links[i].href = cleanUrl(links[i].href); // Standard link cleaning } this.removeEventListener('DOMContentLoaded', cleanPageLinks, false); // We don't need to keep the event listener running } // Google search results link cleaning function function cleanGooglePageLinks() { for (var i = links.length; i--;) { if (excludeLinks.test(links[i].href)) continue; // Links to skip links[i].removeAttribute('onmousedown'); // Remove search results redirection links[i].href = cleanUrl(links[i].href); // Standard link cleaning } this.removeEventListener('DOMContentLoaded', cleanGooglePageLinks, false); // We don't need to keep event listener running } // Google Instant document URL cleaning - if the search terms change, remove the extra stuff. function googleInstant() { if (!document.URL.includes('#imgrc=')) { // Don't rewrite anything if an image is clicked in image searches var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,''); // The string after the hash, containing the new search terms var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchString)); // Remake the full URL with only the new search terms location.replace(newSearchUrl); } } // Main function for cleaning the url's function cleanUrl(oldurl) { var newurl = oldurl; switch(true) { case googleImageRedirect.test(oldurl): newurl = decodeURIComponent(oldurl.replace(/^.*\&url\=/,'').replace(/\&psig\=.*$/,'')); break; case google.test(oldurl): newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match .replace(/\&(channel|tab|num|hl|safe|tbo|sclient|sourceid|spell|client|complete|as_qdr|um|sa|tab|authuser|rlz|cad|rct|ved|usg|site|source|oe|oq|sa|ei|ie|dpr|gs\_l|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd)\=[^&]*/g,'') .replace('?&','?') .replace(/^http\:/,'https:'); // always use https break; case bing.test(oldurl): newurl = oldurl.replace('?','?&') .replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,'') .replace('?&','?') .replace(/^http\:/,'https:'); break; case youtube.test(oldurl): newurl = 'https://www.youtube.com/watch?' + oldurl.match(/v\=[^&]*/); break; case ebay.test(oldurl): newurl = 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/); // the split gets the domain name. Should be more efficient than a regex. break; case ebaySearch.test(oldurl): newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match .replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'') .replace('?&','?') break; case amazon.test(oldurl): newurl = 'https://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}/); break; case newegg.test(oldurl): newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/); break; case dealtime.test(oldurl): newurl = decodeURIComponent(oldurl.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,''); break; default: break; } newurl = newurl.replace(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&\;)/g,''); return newurl; }