Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==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 *
- // @grant unsafeWindow
- // @version 1.3.0
- // ==/UserScript==
- var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
- var google = new RegExp(/^https?:\/\/(www|maps)\.google\.(.+?)\/(search|maps)\?/);
- var googleInstant = new RegExp(/^https?:\/\/(www|maps)\.google\.(.+?)\/(search|maps)\?.*\#q\=/);
- var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
- var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/);
- var amazon = new RegExp(/^https?:\/\/www\.amazon\.*\/gp\/product\//);
- 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 if it needs cleaning
- var newPageUrl = cleanUrl(document.URL);
- if (newPageUrl != document.URL) location.replace(newPageUrl);
- // If the current page is a google search results page, removes redirection of the search results
- // Code taken from https://greasyfork.org/en/scripts/5357-remove-google-results-redirect
- if (google.test(newPageUrl)) {
- if(unsafeWindow.top == unsafeWindow.self){
- document.addEventListener('DOMNodeInserted',function(e){
- window.setTimeout(function(){
- var rl = document.querySelectorAll('a[onmousedown*="return rwt"]');
- for (var l=0;l<rl.length;l++)
- rl[l].removeAttribute('onmousedown');
- }, 250);}
- , false);
- }
- // Google Instant: if the search terms change, remove the extra stuff. Eg: "google.com/search?q=example1#q=example2" -> "google.com/search?q=example2"
- window.addEventListener('hashchange', function() {
- var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,"");
- var newSearchUrl = String(document.URL.replace(/search\?.*/,"search?" + newSearchString));
- location.replace(newSearchUrl);
- }, false);
- }
- // Waits until page has finished loading, then cleans links on the page
- window.addEventListener('load', function() {
- console.time('Cleaning links took');
- var elems = document.getElementsByTagName('a');
- for (var i = 0; i < elems.length; i++) {
- var newurl = cleanUrl(elems[i]['href'])
- if (newurl != elems[i]['href']) {
- // console.log("Cleaned link: " + elems[i]['href'] + " -> " + newurl);
- elems[i]['href'] = newurl;
- }
- }
- console.timeEnd('Cleaning links took');
- }, false);
- // Main function for cleaning the url's
- function cleanUrl(oldurl) {
- var parser = document.createElement('a');
- parser.href = oldurl;
- var newurl = oldurl;
- switch(true) {
- case google.test(oldurl):
- newurl = oldurl.replace("search?","search?&")
- .replace(/\&(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)\=[^&]*/g,"")
- .replace("search?&","search?");
- break;
- case bing.test(oldurl):
- newurl = oldurl.replace("search?","search?&")
- .replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,"")
- .replace("search?&","search?");
- break;
- case youtube.test(oldurl):
- newurl = "https://www.youtube.com/watch?" + oldurl.match(/v\=[^&]*/);
- break;
- case ebay.test(oldurl):
- newurl = "https://" + parser.hostname + "/itm" + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/);
- break;
- case amazon.test(oldurl):
- newurl = "https://" + parser.hostname + oldurl.match(/\/gp\/product\/[A-Z0-9]{10}/);
- break;
- case newegg.test(oldurl):
- newurl = "http://" + parser.hostname + 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;
- }
- if (/((\?|\&|)utm_(source|medium|campaign)\=[^&\/]*|\&\;)/g.test(newurl))
- newurl = newurl.replace(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&\;)/g,"");
- return newurl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement