General URL Cleaner

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

Versión del día 2/7/2015. Echa un vistazo a la versión más reciente.

  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.0.10
  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 amazon = new RegExp(/^https?:\/\/www\.amazon\..*\/dp\//);
  38. var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
  39. var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
  40.  
  41. // Clean the current page URL
  42. var newPageUrl = cleanUrl(document.URL);
  43. if (newPageUrl != document.URL) location.replace(newPageUrl);
  44.  
  45. // Cleans links on the page
  46. var links = document.links;
  47. var excludeLinks = new RegExp(/(^$|^javascript\:|^mailto\:|^data\:)/); // don't do anything with links that are blank, javascript, email addresses, data
  48.  
  49. if (google.test(newPageUrl)) {
  50. document.addEventListener("DOMContentLoaded", cleanGooglePageLinks, false);
  51. window.onhashchange = googleInstant;
  52. }
  53. else {
  54. document.addEventListener("DOMContentLoaded", cleanPageLinks, false);
  55. }
  56.  
  57. // Standard link cleaning function
  58. function cleanPageLinks() {
  59. for (var i = links.length; i--;) {
  60. if (excludeLinks.test(links[i].href)) continue; // Links to skip
  61. links[i].href = cleanUrl(links[i].href); // Standard link cleaning
  62. }
  63. this.removeEventListener('DOMContentLoaded', cleanPageLinks, false); // We don't need to keep the event listener running
  64. }
  65.  
  66. // Google search results link cleaning function
  67. function cleanGooglePageLinks() {
  68. for (var i = links.length; i--;) {
  69. if (excludeLinks.test(links[i].href)) continue; // Links to skip
  70. links[i].removeAttribute('onmousedown'); // Remove search results redirection
  71. links[i].href = cleanUrl(links[i].href); // Standard link cleaning
  72. }
  73. this.removeEventListener('DOMContentLoaded', cleanGooglePageLinks, false); // We don't need to keep event listener running
  74. }
  75.  
  76. // Google Instant document URL cleaning - if the search terms change, remove the extra stuff.
  77. function googleInstant() {
  78. if (!document.URL.includes('#imgrc=')) { // Don't rewrite anything if an image is clicked in image searches
  79. var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,''); // The string after the hash, containing the new search terms
  80. var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchString)); // Remake the full URL with only the new search terms
  81. location.replace(newSearchUrl);
  82. }
  83. }
  84.  
  85. // Main function for cleaning the url's
  86. function cleanUrl(oldurl) {
  87. var newurl = oldurl;
  88. switch(true) {
  89. case googleImageRedirect.test(oldurl):
  90. newurl = decodeURIComponent(oldurl.replace(/^.*\&url\=/,'').replace(/\&psig\=.*$/,''));
  91. break;
  92. case google.test(oldurl):
  93. newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match
  94. .replace(/\&(aqs|es_sm|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,'')
  95. .replace('?&','?')
  96. .replace(/^http\:/,'https:'); // always use https
  97. break;
  98. case bing.test(oldurl):
  99. newurl = oldurl.replace('?','?&')
  100. .replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,'')
  101. .replace('?&','?')
  102. .replace(/^http\:/,'https:');
  103. break;
  104. case youtube.test(oldurl):
  105. newurl = 'https://www.youtube.com/watch?' + oldurl.match(/v\=[^&]*/);
  106. break;
  107. case ebay.test(oldurl):
  108. 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.
  109. break;
  110. case ebaySearch.test(oldurl):
  111. newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match
  112. .replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'')
  113. .replace('?&','?');
  114. break;
  115. case amazon.test(oldurl):
  116. newurl = 'https://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}/);
  117. break;
  118. case newegg.test(oldurl):
  119. newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
  120. break;
  121. case dealtime.test(oldurl):
  122. newurl = decodeURIComponent(oldurl.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,'');
  123. break;
  124. default:
  125. break;
  126. }
  127. newurl = newurl.replace(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g,'');
  128. return newurl;
  129. }