General URL Cleaner

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

Fra og med 25.05.2016. Se den nyeste 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?)?\.[a-z]{2,3}/.*$/
  7. // @include /^https?://www\.amazon(\.com?)?\.[a-z]{2,3}/.*$/
  8. // @include /^https?://www\.newegg.c(om|a)/.*$/
  9. // @include /^https?://[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/.*$/
  10. // @include /^https?://www\.bing\.com/*$/
  11. // @include /^https?://www\.youtube\.com/*$/
  12. // @include http://stat.dealtime.com/*
  13. // @exclude https://apis.google.com/*
  14. // @exclude https://www.google.com/recaptcha/api2/*
  15. // @version 2.2.2.2
  16. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  17. // ==/UserScript==
  18.  
  19. // -------- Pre-compile regexes -----------
  20. var bing = /^https?:\/\/www\.bing\.com\/(profile\/)?[a-z]*\?/;
  21. var ebay = /^https?:\/\/[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/;
  22. var amazon = /^https?:\/\/www\.amazon(\.com?)?\.[a-z]{2,3}\//;
  23. var newegg = /^http:\/\/www\.newegg\.c(om|a)/;
  24. var youtube = /^https?:\/\/www\.youtube\.com/;
  25. var google = /^https?:\/\/[a-z]*\.google(\.com?)?\.[a-z]{2,3}\//;
  26. var dealtime = /http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/;
  27. var utmParameters = /([?&]?utm_[a-z]+=[^&#]*|&)/g;
  28. var googleSearchParameters = new RegExp(
  29. '&(aqs|as_qdr|authuser|bav|bi[wh]|bs|bvm|cad|channel|s?client|complete|cp|dpr|es_sm|g(fe|ws)_rd|gpsrc|h[ls]|ie|n?um'+
  30. '|btnG|o[eq]|pbx|p[fq]|rct|rlz|sa(fe)?|s?ei|site|source(id)?|spell|tab|tbas|tbo|usg|ved|xhr|gs_(l|r[ni]|mss|id))=[^&]*','g'
  31. );
  32.  
  33. // -------- Main --------
  34. var doc = document;
  35. if (bing.test(doc.URL)) {
  36. var newUrl = cleanBingSearch(doc.URL);
  37. if (/^http:/.test(newUrl)) location.href = newUrl.replace(/^http:/,'https:');
  38. else cleanPageUrl(newUrl);
  39. cleanLinks('all');
  40. }
  41. else if (google.test(doc.URL)) {
  42. if (doc.URL.includes('/url?')) location.replace(cleanGoogleRedirect(doc.URL));
  43. else if (doc.URL.includes('/imgres?imgurl=')) location.replace(cleanGoogleImageRedirect(doc.URL));
  44. else if (/\.[a-z]{2,3}\/[a-z]*\?/.test(doc.URL)) {
  45. cleanPageUrl(cleanGoogleSearch(doc.URL));
  46. cleanLinks('google');
  47. googleInstant();
  48. }
  49. }
  50. else if (youtube.test(doc.URL)) {
  51. if (doc.URL.includes('/watch/')) cleanPageUrl(cleanYoutubeVideo(doc.URL));
  52. else if (doc.URL.includes('redirect?')) location.replace(cleanYoutubeRedirect(doc.URL));
  53. cleanLinks('youtube');
  54. }
  55. else if (ebay.test(doc.URL)) {
  56. if (doc.URL.includes('/itm/')) cleanPageUrl(cleanEbayItem(doc.URL));
  57. else if (doc.URL.includes('/sch/')) cleanPageUrl(cleanEbaySearch(doc.URL));
  58. cleanLinks('ebay');
  59. }
  60. else if (amazon.test(doc.URL)) {
  61. if (doc.URL.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(doc.URL));
  62. else if (doc.URL.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(doc.URL));
  63. cleanLinks('amazon');
  64. }
  65. else if (newegg.test(doc.URL)) {
  66. if (doc.URL.includes('/Product/')) cleanPageUrl(cleanNeweggItem(doc.URL));
  67. cleanLinks('newegg');
  68. }
  69. else if (dealtime.test(doc.URL)) {
  70. location.replace(cleanDealtime(doc.URL));
  71. }
  72.  
  73. // -------- Front functions --------
  74. function cleanPageUrl(newUrl) {
  75. if (newUrl != doc.URL) history.replaceState(null,null,newUrl);
  76. }
  77. function cleanLinks(site) { new MutationObserver(function(M) { M.forEach(function(m) {
  78. links = m.target.getElementsByTagName('a');
  79. for (i=links.length; i--;) {
  80. linkCleaners[site](links[i]);
  81. }
  82. });}).observe(doc,{childList:true,attributes:true,attributeFilter:['href'],subtree:true});}
  83. function googleInstant() { new MutationObserver(function(_,self){
  84. var search = doc.getElementById('lst-ib');
  85. if (search) {
  86. new MutationObserver( function() {
  87. if (doc.URL.includes('#') && !doc.URL.includes('#imgrc='))
  88. history.replaceState(null,null,cleanGoogleSearch(doc.URL.match(/.+?\?/)+doc.URL.match(/#(.*)/)[1]));
  89. }).observe(search,{childList:true,attributes:true});
  90. self.disconnect();
  91. }
  92. }).observe(doc,{childList:true,subtree:true});}
  93.  
  94. // -------- URL cleaning functions --------
  95. function cleanGoogleSearch(url) {
  96. return url.replace('?','?&').replace(googleSearchParameters,'').replace('?&','?').replace(/^http\:/,'https:');
  97. }
  98. function cleanGoogleRedirect(url) {
  99. return decodeURIComponent(url.replace(/.+\/url\?.*(url|q)=/,'').replace(/&(rct|psig|ei|bvm|sa)=.*$/g,''));
  100. }
  101. function cleanGoogleImageRedirect(url) {
  102. return decodeURIComponent(url.replace(/(.+?)\?imgurl=/,'').replace(/&imgrefurl=.*/,''));
  103. }
  104. function cleanBingSearch(url) {
  105. return url.replace('?','?&').replace(/&(go|qs|form|FORM|filt|pq|s[cpk]|qpvt)=[^&]*/g,'').replace('?&','?');
  106. }
  107. function cleanYoutubeVideo(url) {
  108. return url.replace("?","?&").replace(/&(feature|src_vid|annotation_id|[gh]l)=[^&]*/g,'').replace("?&","?").replace(/^http:/,'https:');
  109. }
  110. function cleanYoutubeRedirect(url) {
  111. return decodeURIComponent(url.replace(/(.+?)\?q=/,'').replace(/&redir_token=.*/,''));
  112. }
  113. function cleanEbayItem(url) {
  114. return 'http://' + url.split('/')[2] + '/itm' + url.match(/\/[0-9]{11,13}/) + (url.match(/#[A-Za-z]+$/)||'');
  115. }
  116. function cleanEbaySearch(url) {
  117. return url.replace('?','?&').replace(/&(_o?sacat|_odkw|_from|_trksid|rt)=[^&]*/g,'').replace('?&','?');
  118. }
  119. function cleanAmazonItemgp(url) {
  120. return 'http://' + url.split('/')[2] + url.match(/\/gp\/product\/[A-Z0-9]{10}/);
  121. }
  122. function cleanAmazonItemdp(url) {
  123. return 'http://' + url.split('/')[2] + url.match(/\/dp\/[A-Z0-9]{10}/);
  124. }
  125. function cleanNeweggItem(url) {
  126. return 'http://' + url.split('/')[2] + '/Product/Product.aspx' + url.match(/\?Item=[^&]*/);
  127. }
  128. function cleanDealtime(url) {
  129. return decodeURIComponent(url.replace(/.*&url=/,'').replace(/(%26)?&linkin_id=.*$/,'')).replace(/&(url|partner)=[^&]*/g,'');
  130. }
  131.  
  132. // -------- Link cleaning functions --------
  133. var linkCleaners = {
  134. all:function(a) {
  135. wbr = a.getElementsByTagName('wbr');
  136. for (k = wbr.length; k--;) a.removeChild(wbr[k]);
  137. linkCleaners._google(a); linkCleaners._youtube(a); linkCleaners.utm(a);
  138. linkCleaners.amazon(a); linkCleaners.ebay(a); linkCleaners.newegg(a);
  139. },
  140. amazon:function(a) { if (amazon.test(a.href)) {
  141. if (a.href.includes('/dp/')) a.href = cleanAmazonItemdp(a.href);
  142. else if (a.href.includes('/gp/product')) a.href = cleanAmazonItemgp(a.href);
  143. }},
  144. ebay:function(a) { if (ebay.test(a.href)) {
  145. if (a.href.includes('/itm/')) a.href = cleanEbayItem(a.href);
  146. else if (a.href.includes('/sch/')) a.href = cleanEbaySearch(a.href);
  147. }},
  148. newegg:function(a) { if (newegg.test(a.href)) {
  149. if (a.href.includes('/Product/')) a.href = cleanNeweggItem(a.href);
  150. }},
  151. _google:function(a) { if (google.test(a.href)) {
  152. if (a.href.includes('/url=')) a.href = cleanGoogleRedirect(a.href);
  153. else if (/\/[a-z]+\?/.test(a.href)) a.href = cleanGoogleSearch(a.href);
  154. }},
  155. _youtube:function(a) { if (youtube.test(a.href)) {
  156. if (a.href.includes('/watch/')) a.href = cleanYoutubeVideo(a.href);
  157. else if (a.href.includes('/redirect?')) a.href = cleanGoogleRedirect(a.href);
  158. }},
  159. utm:function(a) { if (utmParameters.test(a.href)) {
  160. url = a.href.replace(utmParameters,'');
  161. if (a.innerText==a.href) a.innerText = url;
  162. if (a.title==a.href) a.title = url;
  163. a.href = url;
  164. }},
  165. google:function(a) {
  166. a.removeAttribute('onmousedown');
  167. linkCleaners.all(a);
  168. },
  169. youtube:function(a) {
  170. linkCleaners._youtube(a);
  171. if (/yt-uix-redirect-link/.test(a.className)) {
  172. a.removeAttribute('class');
  173. if (a.title == a.href) {
  174. linkCleaners.all(a);
  175. a.title = a.href;
  176. }
  177. else linkCleaners.all(a);
  178. }
  179. else if (/spf-link/.test(a.className)) {
  180. a.className = 'content-link';
  181. a.removeAttribute('data-sessionlink');
  182. a.removeAttribute('rel');
  183. }
  184. }
  185. };