General URL Cleaner

Cleans URL's from various popular sites. Also, makes sure the sites are using HTTPS.

À partir de 2015-07-16. Voir la dernière version.

  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 /^https?://[a-z]*.google.(com|ca|co.uk|com.au|co.nz|ac|ad|ae|com.af|com.ag|com.ai|al|am|co.ao|com.ar)\/.*$/
  7. // @include /^https?://[a-z]*.google.(us|as|at|az|ba|com.bd|be|bf|bg|com.bh|bi|bj|com.bn|com.bo|com.br|bs|bt|co.bw)\/.*$/
  8. // @include /^https?://[a-z]*.google.(by|com.bz|com.kh|cc|cd|cf|cat|cg|ch|ci|co.ck|cl|cm|cn|com.co|co.cr|com.cu|cv)\/.*$/
  9. // @include /^https?://[a-z]*.google.(com.cy|cz|de|dj|dk|dm|com.do|dz|com.ec|ee|com.eg|es|com.et|fi|com.fj|fm|fr)\/.*$/
  10. // @include /^https?://[a-z]*.google.(ga|ge|gf|gg|com.gh|com.gi|gl|gm|gp|gr|com.gt|gy|com.hk|hn|hr|ht|hu|co.id|ir)\/.*$/
  11. // @include /^https?://[a-z]*.google.(iq|ie|co.il|im|co.in|io|is|it|je|com.jm|jo|co.jp|co.ke|ki|kg|co.kr|com.kw|kz)\/.*$/
  12. // @include /^https?://[a-z]*.google.(la|com.lb|com.lc|li|lk|co.ls|lt|lu|lv|com.ly|co.ma|md|me|mg|mk|ml|com.mm|mn)\/.*$/
  13. // @include /^https?://[a-z]*.google.(ms|com.mt|mu|mv|mw|com.mx|com.my|co.mz|com.na|ne|com.nf|com.ng|com.ni|nl|no)\/.*$/
  14. // @include /^https?://[a-z]*.google.(com.np|nr|nu|com.om|com.pa|com.pe|com.ph|com.pk|pl|com.pg|pn|com.pr|ps|com.py)\/.*$/
  15. // @include /^https?://[a-z]*.google.(vg|pt|com.qa|ro|rs|ru|rw|com.sa|com.sb|sc|se|com.sg|sh|si|sk|com.sl|sn|sm|so)\/.*$/
  16. // @include /^https?://[a-z]*.google.(st|com.sv|td|tg|co.th|com.tj|tk|tl|tm|to|tn|com.tn|com.tr|tt|com.tw|co.tz)\/.*$/
  17. // @include /^https?://[a-z]*.google.(com.ua|co.ug|com.uy|co.uz|com.vc|co.ve|co.vi|com.vn|vu|ws|co.za|co.zm|co.zw)\/.*$/
  18. // @include /^https?://[a-z]*.amazon.(cn|in|co.jp|fr|de|it|nl|es|co.uk|ca|com.mx|com|com.au|com.br)\/.*$/
  19. // @include /^https?://[a-z]*.newegg.(com|ca|cn)\/.*$/
  20. // @include /^https?://[a-z]*.ebay.(com.au|at|be|ca|fr|de|com.hk|in|ie|co.il|it|com.my|nl|co.za|ph|pl|com.sg|co.za|es|ch|co.th|co.uk|com|vn)\/.*$/
  21. // @include /^https?://[a-z]*.bing.com\/.*$/
  22. // @include /^https?://[a-z]*.youtube.com\/.*$/
  23. // @include /^https?://[a-z]*.dealtime.com\/.*$/
  24. // @exclude https://apis.google.com/*
  25. // @exclude https://www.google.com/recaptcha/api2/*
  26. // @version 1.9.1.2
  27. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  28. // ==/UserScript==
  29.  
  30. // compile these regexes beforehand to improve efficiency
  31. var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
  32. var google = new RegExp(/^https?:\/\/[a-z]*\.google\.(.+?)\/[a-z]*\?/);
  33. var googleImageRedirect = new RegExp(/^https?:\/\/www\.google\.(.+?)\/url\?/);
  34. var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
  35. var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/);
  36. var ebaySearch = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//);
  37. var amazondp = new RegExp(/^https?:\/\/www\.amazon\..*\/dp\//);
  38. var amazongp = new RegExp(/^https?:\/\/www\.amazon\..*\/gp\/product\//);
  39. var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
  40. var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
  41.  
  42. var googleSearchParameters = new RegExp(
  43. /\&(aqs|es_sm|channel|tab|num|hl|safe|tbo|sclient|sourceid|spell|site|sa|ei|client|complete|as_qdr|um|sa|tab|authuser|rlz|cad|rct|ved|usg|source|oe|oq|ie|dpr|gs_l|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd)\=[^&]*/g);
  44.  
  45. // Clean the current page URL
  46. var newPageUrl = cleanUrl(document.URL);
  47. if (newPageUrl != document.URL) location.replace(newPageUrl);
  48.  
  49. // Cleans links on the page
  50. var links = document.links;
  51. var excludeLinks = new RegExp(/(^$|^javascript\:|^mailto\:|^data\:)/); // don't do anything with links that are blank, javascript, email addresses, data
  52.  
  53. if (google.test(newPageUrl)) {
  54. document.addEventListener("DOMContentLoaded", cleanGooglePageLinks, false);
  55. window.onhashchange = googleInstant;
  56. }
  57. else {
  58. document.addEventListener("DOMContentLoaded", cleanPageLinks, false);
  59. }
  60.  
  61. // Standard link cleaning function
  62. function cleanPageLinks() {
  63. for (var i = links.length; i--;) {
  64. if (excludeLinks.test(links[i].href)) continue; // Links to skip
  65. links[i].href = cleanUrl(links[i].href); // Standard link cleaning
  66. }
  67. this.removeEventListener('DOMContentLoaded', cleanPageLinks, false); // We don't need to keep the event listener running
  68. }
  69.  
  70. // Google search results link cleaning function
  71. function cleanGooglePageLinks() {
  72. for (var i = links.length; i--;) {
  73. if (excludeLinks.test(links[i].href)) continue; // Links to skip
  74. links[i].removeAttribute('onmousedown'); // Remove search results redirection
  75. links[i].href = cleanUrl(links[i].href); // Standard link cleaning
  76. }
  77. this.removeEventListener('DOMContentLoaded', cleanGooglePageLinks, false); // We don't need to keep event listener running
  78. }
  79.  
  80. // Google Instant document URL cleaning - if the search terms change, remove the extra stuff.
  81. function googleInstant() {
  82. if (!document.URL.includes('#imgrc=')) { // Don't rewrite anything if an image is clicked in image searches
  83. var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,''); // The string after the hash, containing the new search terms
  84. var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchString)); // Remake the full URL with only the new search terms
  85. location.replace(newSearchUrl);
  86. }
  87. }
  88.  
  89. // Main function for cleaning the url's
  90. function cleanUrl(oldurl) {
  91. var newurl = oldurl;
  92. switch(true) {
  93. case googleImageRedirect.test(oldurl):
  94. newurl = decodeURIComponent(oldurl.replace(/^.*\&url\=/,'').replace(/\&(psig|ei|bvm)\=.*$/g,''));
  95. break;
  96. case google.test(oldurl):
  97. newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match
  98. .replace(googleSearchParameters,'')
  99. .replace('?&','?')
  100. .replace(/^http\:/,'https:'); // always use https
  101. break;
  102. case bing.test(oldurl):
  103. newurl = oldurl.replace('?','?&')
  104. .replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,'')
  105. .replace('?&','?')
  106. .replace(/^http\:/,'https:');
  107. break;
  108. case youtube.test(oldurl):
  109. newurl = 'https://www.youtube.com/watch?' + oldurl.match(/v\=[^&]*/);
  110. break;
  111. case ebay.test(oldurl):
  112. // the split gets the domain name. Should be more efficient than a regex.
  113. newurl = 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/);
  114. break;
  115. case ebaySearch.test(oldurl):
  116. newurl = oldurl.replace('?','?&')
  117. .replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'')
  118. .replace('?&','?');
  119. break;
  120. case amazondp.test(oldurl):
  121. newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}\/?/);
  122. break;
  123. case amazongp.test(oldurl):
  124. newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/gp\/product\/[A-Z0-9]{10}\/?/);
  125. break;
  126. case newegg.test(oldurl):
  127. newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
  128. break;
  129. case dealtime.test(oldurl):
  130. newurl = decodeURIComponent(oldurl.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,'');
  131. break;
  132. default:
  133. break;
  134. }
  135. newurl = newurl.replace(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g,'');
  136. return newurl;
  137. }