Advertisement
Guest User

General URL Cleaner

a guest
May 25th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @run-at document-start
  3. // @name           General URL Cleaner
  4. // @namespace      
  5. // @description    Cleans URL's from various popular sites. Also, makes sure the sites are using HTTPS.
  6. // @include        *
  7. // @grant   unsafeWindow
  8. // @version        1.3.0
  9. // ==/UserScript==
  10.  
  11. var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
  12. var google = new RegExp(/^https?:\/\/(www|maps)\.google\.(.+?)\/(search|maps)\?/);
  13. var googleInstant = new RegExp(/^https?:\/\/(www|maps)\.google\.(.+?)\/(search|maps)\?.*\#q\=/);
  14. var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
  15. var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/);
  16. var amazon = new RegExp(/^https?:\/\/www\.amazon\.*\/gp\/product\//);
  17. var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
  18. var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
  19.  
  20.  
  21. // Clean the current page URL if it needs cleaning
  22. var newPageUrl = cleanUrl(document.URL);
  23. if (newPageUrl != document.URL) location.replace(newPageUrl);
  24.  
  25. // If the current page is a google search results page, removes redirection of the search results
  26. // Code taken from https://greasyfork.org/en/scripts/5357-remove-google-results-redirect
  27. if (google.test(newPageUrl)) {
  28.     if(unsafeWindow.top == unsafeWindow.self){
  29.         document.addEventListener('DOMNodeInserted',function(e){
  30.             window.setTimeout(function(){
  31.             var rl = document.querySelectorAll('a[onmousedown*="return rwt"]');
  32.             for (var l=0;l<rl.length;l++)
  33.                 rl[l].removeAttribute('onmousedown');
  34.             }, 250);}
  35.         , false);
  36.     }
  37.  
  38.     // Google Instant: if the search terms change, remove the extra stuff. Eg: "google.com/search?q=example1#q=example2" -> "google.com/search?q=example2"
  39.     window.addEventListener('hashchange', function() {
  40.         var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,"");
  41.         var newSearchUrl = String(document.URL.replace(/search\?.*/,"search?" + newSearchString));
  42.         location.replace(newSearchUrl);
  43.     }, false);
  44. }
  45.  
  46. // Waits until page has finished loading, then cleans links on the page
  47. window.addEventListener('load', function() {
  48.     console.time('Cleaning links took');
  49.     var elems = document.getElementsByTagName('a');
  50.     for (var i = 0; i < elems.length; i++) {
  51.         var newurl = cleanUrl(elems[i]['href'])
  52.         if (newurl != elems[i]['href']) {
  53.             // console.log("Cleaned link: " + elems[i]['href'] + " -> " + newurl);
  54.             elems[i]['href'] = newurl;
  55.         }
  56.     }
  57.     console.timeEnd('Cleaning links took');
  58. }, false);
  59.  
  60. // Main function for cleaning the url's
  61. function cleanUrl(oldurl) {
  62.     var parser = document.createElement('a');
  63.     parser.href = oldurl;
  64.     var newurl = oldurl;
  65.     switch(true) {
  66.         case google.test(oldurl):
  67.             newurl = oldurl.replace("search?","search?&")
  68.                            .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,"")
  69.                            .replace("search?&","search?");
  70.             break;
  71.         case bing.test(oldurl):
  72.             newurl = oldurl.replace("search?","search?&")
  73.                            .replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,"")
  74.                            .replace("search?&","search?");
  75.             break;
  76.         case youtube.test(oldurl):
  77.             newurl = "https://www.youtube.com/watch?" + oldurl.match(/v\=[^&]*/);
  78.             break;
  79.         case ebay.test(oldurl):
  80.             newurl = "https://" + parser.hostname + "/itm" + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/);
  81.             break;
  82.         case amazon.test(oldurl):
  83.             newurl = "https://" + parser.hostname + oldurl.match(/\/gp\/product\/[A-Z0-9]{10}/);
  84.             break;
  85.         case newegg.test(oldurl):
  86.             newurl = "http://" + parser.hostname + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
  87.             break;
  88.         case dealtime.test(oldurl):
  89.             newurl = decodeURIComponent(oldurl.replace(/.*\&url\=/,"").replace(/(\%26|)\&linkin_id\=.*$/,"")).replace(/\&(url|partner)\=[^&]*/g,"");
  90.             break;
  91.         default:
  92.             break;
  93.     }
  94.     if (/((\?|\&|)utm_(source|medium|campaign)\=[^&\/]*|\&amp\;)/g.test(newurl))
  95.         newurl = newurl.replace(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g,"");
  96.     return newurl;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement