Greasy Fork is available in English.

General URL Cleaner

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

As of 2017-08-01. See the latest 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?://[a-z.]+\.ebay(desc)?(\.com?)?\.[a-z]{2,3}/.*$/
  8. // @include /^https?://www\.amazon(\.com?)?\.[a-z]{2,3}/.*$/
  9. // @include /^https?://www\.newegg\.c(om|a)/.*$/
  10. // @include /^https?://www\.bing\.com/.*$/
  11. // @include https://www.youtube.com/*
  12. // @include http://stat.dealtime.com/*
  13. // @include http://www.imdb.com/*
  14. // @include /https?://(www\.)?staticice\.com\.au/.*$/
  15. // @include https://www.facebook.com/*
  16. // @include /https?://[a-z0-9.]*twitter.com/.*$/
  17. // @exclude https://apis.google.com/*
  18. // @exclude https://accounts.google.com/*
  19. // @exclude https://support.google.com/*
  20. // @exclude https://www.google.com/recaptcha/api2/*
  21. // @version 2.10.2
  22. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  23. // ==/UserScript==
  24. var doc = document;
  25. var loc = location;
  26. var lhost = loc.host;
  27. var lpath = loc.pathname;
  28. var ebay = /^[a-z.]+\.ebay(desc)?(\.com?)?\.[a-z]{2,3}$/;
  29. var amazon = /^www\.amazon\.com?(\.[a-z]{2,3})?$/;
  30. var google = /^[a-z]+\.google\.com?(\.[a-z]{2,3})?$/;
  31. var amazonParams = /&(url|ie|pf_rd_[a-z]|bbn|rw_html_to_wsrp|ref_)=[^&#]*/;
  32. var utmParams = /&utm_[a-z]+=[^&]*/g;
  33. var neweggParams = /&(cm_sp|icid|ignorebbr)=[^&#]*/g;
  34. var bingParams = /&(go|qs|form|FORM|filt|pq|s[cpk]|qpvt|cvid)=[^&#]*/g;
  35. var youtubeParams = /&(feature|src_vid|annotation_id|[gh]l)=[^&#]*/g;
  36. var ebayParams = /&(_(o?sacat|odkw|from|trksid)|rt)=[^&#]*/g;
  37. var googleParams = /&(sa(fe)?|ved|source(id)?|s?ei|tab|tbo|h[ls]|authuser|n?um|ie|aqs|as_qdr|bav|bi[wh]|bs|bvm|cad|channel|complete|cp|s?client|dpr|e(ch|msg|s_sm)|g(fe|ws)_rd|gpsrc|noj|btnG|o[eq]|p(si|bx|f|q)|rct|rlz|site|spell|tbas|usg|xhr|gs_[a-z]+)=[^&#]*/g;
  38.  
  39. // -------- Main --------
  40. function main() {
  41. if (lhost=='www.bing.com') {
  42. var newUrl = cleanBing(doc.URL);
  43. if (loc.protocol=='http:') loc.replace(newUrl);
  44. else cleanPageUrl(newUrl);
  45. cleanLinks('all');
  46. }
  47. else if (lhost=='www.youtube.com') {
  48. if (lpath=='/watch') cleanPageUrl(cleanYoutube(doc.URL));
  49. else if (lpath=='/redirect') loc.replace(cleanYoutubeRedir(loc.search));
  50. cleanLinks('youtube');
  51. }
  52. else if (lhost.endsWith('.newegg.com')||lhost.endsWith('.newegg.ca')) {
  53. if (loc.search) cleanPageUrl(cleanNewegg(doc.URL));
  54. cleanLinks('newegg');
  55. }
  56. else if (lhost=='www.imdb.com') {
  57. if (loc.search) cleanPageUrl(cleanImdb(doc.URL));
  58. cleanLinks('imdb');
  59. deleteHash();
  60. }
  61. else if (google.test(lhost)) {
  62. if (lpath=='/url'||lpath=='/imgres') loc.replace(cleanGoogleRedir(loc.search));
  63. else if (loc.search||loc.hash.match(/[&#]q=/)) {
  64. cleanPageUrl(cleanGoogle(doc.URL));
  65. cleanLinks('google');
  66. googleInstant();
  67. }
  68. }
  69. else if (ebay.test(lhost)) {
  70. if (lpath.includes('/itm/')) cleanPageUrl(cleanEbayItem(loc));
  71. else if (loc.search) cleanPageUrl(cleanEbayParams(doc.URL));
  72. cleanLinks('ebay');
  73. deleteHash();
  74. }
  75. else if (amazon.test(lhost)) {
  76. if (lpath.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(loc));
  77. else if (lpath.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(loc));
  78. else if (loc.search) cleanPageUrl(cleanAmazonParams(doc.URL));
  79. cleanLinks('amazon');
  80. onhashchange = function() {
  81. if (!loc.hash.startsWith('#reader_'))
  82. history.replaceState(null,null,loc.href.replace(loc.hash,''));
  83. };
  84. }
  85. else if (lhost.endsWith('staticice.com.au')) {
  86. cleanLinks('staticice');
  87. }
  88. else if (lhost=='twitter.com') {
  89. cleanLinks('twitter');
  90. }
  91. else if(lhost=='www.facebook.com'){
  92. cleanLinks('facebook');
  93. }
  94. }
  95.  
  96. // -------- Front functions --------
  97. function cleanPageUrl(newUrl) {
  98. if (newUrl != doc.URL) history.replaceState(null, null, newUrl);
  99. }
  100.  
  101. function cleanLinks(site) {
  102. new MutationObserver(function(_,self) {
  103. console.time('clean');
  104. for (let link of doc.links) {
  105. if (!link.classList.contains('cleaned')) {
  106. linkCleaners[site](link);
  107. link.classList.add('cleaned');
  108. }
  109. }
  110. console.timeEnd('clean');
  111. }).observe(doc, {childList:true, subtree:true});
  112. }
  113.  
  114. function deleteHash() {
  115. function onhashchange() {
  116. history.replaceState(null, null, loc.href.replace(loc.hash, ''));
  117. }
  118. }
  119. function googleInstant() {
  120. changeState(function(url) {
  121. if (url.endsWith('#imgrc=_'))
  122. return url.slice(0,-8);
  123. else if (url.match(/#(.+&)?q=/))
  124. return lpath + cleanGoogle(url.replace(/.+#/,'?'));
  125. return url;
  126. });
  127. }
  128. function changeState(mod) {
  129. pushState = history.pushState;
  130. replaceState = history.replaceState;
  131. history.pushState = function() {
  132. arguments[2] = mod(arguments[2]);
  133. pushState.apply(history, arguments);
  134. };
  135. history.replaceState = function() {
  136. arguments[2] = mod(arguments[2]);
  137. replaceState.apply(history, arguments);
  138. };
  139. }
  140.  
  141. // -------- URL cleaning functions --------
  142. function cleanGoogle(url) {
  143. return url.replace('?','?&').replace(googleParams,'').replace('?&','?');
  144. }
  145. function cleanGoogleRedir(url) {
  146. return decodeURIComponent(url.match(/[&?](img)?url=([^&]+)/)[2]);
  147. }
  148. function cleanBing(url) {
  149. return url.replace('?','?&').replace(bingParams,'').replace('?&','?').replace(/^http\:/,'https:');
  150. }
  151. function cleanYoutube(url) {
  152. return url.replace("?","?&").replace(youtubeParams,'').replace("?&","?");
  153. }
  154. function cleanYoutubeRedir(url) {
  155. return decodeURIComponent(url.match(/[?&]q=([^&]+)/)[1]);
  156. }
  157. function cleanEbayParams(url) {
  158. return url.replace('?','?&').replace(ebayParams,'').replace('?&','?');
  159. }
  160. function cleanEbayItem(a) {
  161. return a.origin+'/itm'+a.pathname.match(/\/[0-9]{12}/)+(a.search.replace('&','?').match(/\?orig_cvip=[^?]+/)||'')+a.hash;
  162. }
  163. function cleanEbayPulsar(url) {
  164. return loc.origin + '/itm/' + url.match(/%7B%22mecs%22%3A%22([0-9]{12})/).pop();
  165. }
  166. function cleanNewegg(url) {
  167. return url.replace('?','?&').replace(neweggParams,'').replace('?&','?');
  168. }
  169. function cleanAmazonParams(url) {
  170. return url.replace('?','?&').replace(amazonParams,'').replace('?&','?').replace(/\?$/,'');
  171. }
  172. function cleanAmazonItemgp(a) {
  173. return a.origin+'/gp/product'+a.pathname.match(/\/[A-Z0-9]{10}/)+a.hash;
  174. }
  175. function cleanAmazonItemdp(a) {
  176. return a.origin+'/dp'+a.pathname.match(/\/[A-Z0-9]{10}/)+a.hash;
  177. }
  178. function cleanImdb(url) {
  179. return url.replace('?','?&').replace(/&(pf_rd_[a-z]|ref_)=[^&#]*/,'').replace('?&','?').replace(/\?$/,'');
  180. }
  181. function cleanGenericRedir(url) {
  182. return decodeURIComponent(url.match(/[?&]u(rl)?=([^&]+)/)[2]);
  183. }
  184. function cleanUtm(url) {
  185. return url[0]+('&'+url.slice(1)).replace(utmParams,'').slice(1);
  186. }
  187. function cleanStaticiceRedir(url) {
  188. return decodeURIComponent(url.match(/[?&]newurl=([^&]+)/)[1]);
  189. }
  190. function cleanClixgalore(url) {
  191. return decodeURIComponent(url.match(/[?&]AffDirectURL=([^&]+)/)[1]);
  192. }
  193.  
  194. // -------- Link cleaning functions --------
  195. var linkCleaners = {
  196. all:function(a) { host=a.host; path=a.pathname;
  197. if (google.test(host))
  198. if (path=='/imgres'||path=='/url') a.href = cleanGoogleRedir(a.search);
  199. else if (a.search) a.search = cleanGoogle(a.search);
  200. else if (host=='www.youtube.com')
  201. if (path=='/watch') a.search = cleanYoutube(a.search);
  202. else if (path=='/redirect') a.href = cleanYoutubeRedir(a.search);
  203. else if (host.endsWith('.newegg.com') || host.endsWith('.newegg.ca'))
  204. if(a.search) a.search = cleanNewegg(a.search);
  205. else if (host=='www.imdb.com' && a.search)
  206. a.search = cleanImdb(a.search);
  207. else if (amazon.test(host))
  208. if (path.includes('/dp/')) a.href = cleanAmazonItemdp(a);
  209. else if (path.includes('/gp/product')) a.href = cleanAmazonItemgp(a);
  210. else if (a.pathname.includes('/picassoRedirect')) {
  211. a.href = cleanGenericRedir(a.search);
  212. a.search = '';
  213. }
  214. else if (a.search) a.href = cleanAmazonParams(a.href);
  215. if (a.pathname.includes('/ref=')) a.pathname = cleanAmazonParams(a.pathname);
  216. else if (ebay.test(host))
  217. if (path.includes('/itm/')) a.href = cleanEbayItem(a);
  218. else if (a.host.startsWith('pulsar.')) a.href = cleanEbayPulsar(a.search);
  219. else if (a.search) a.search = cleanEbayParams(a.search);
  220. else {
  221. if (a.search) a.search = cleanUtm(a.search);
  222. if (a.hash) a.hash = cleanUtm(a.hash);
  223. }
  224. },
  225. amazon:function(a) {
  226. if (amazon.test(a.host))
  227. if (a.pathname.includes('/dp/')) a.href = cleanAmazonItemdp(a);
  228. else if (a.pathname.includes('/gp/product')) a.href = cleanAmazonItemgp(a);
  229. else if (a.pathname.includes('/picassoRedirect')) {
  230. a.href = cleanGenericRedir(a.search);
  231. a.search = '';
  232. }
  233. else if (a.search) a.href = cleanAmazonParams(a.href);
  234. if (a.pathname.includes('/ref=')) a.pathname = cleanAmazonParams(a.pathname);
  235. },
  236. ebay:function(a) {
  237. if (ebay.test(a.host))
  238. if (a.pathname.includes('/itm/')) a.href = cleanEbayItem(a);
  239. else if (a.host.startsWith('pulsar.')) a.href = cleanEbayPulsar(a.search);
  240. else if (a.search) a.search = cleanEbayParams(a.search);
  241. },
  242. newegg:function(a) {
  243. if (a.host.endsWith('.newegg.com')||a.host.endsWith('.newegg.ca'))
  244. if (a.pathname.includes('/marketplace/'));
  245. else if (a.search) a.search = cleanNewegg(a.search);
  246. },
  247. google:function(a) {
  248. a.removeAttribute('onmousedown');
  249. if (a.hasAttribute('jsaction') && a.getAttribute('jsaction').includes('down:irc.rl'))
  250. a.removeAttribute('jsaction');
  251. linkCleaners.all(a);
  252. },
  253. youtube:function(a) {
  254. a.classList.remove('yt-uix-redirect-link');
  255. a.classList.remove('spf-link');
  256. a.removeAttribute('data-sessionlink');
  257. old = a.href;
  258. linkCleaners.all(a);
  259. if (a.title==old) a.title=a.href;
  260. },
  261. staticice:function(a) {
  262. if (a.host.endsWith('staticice.com.au')) {
  263. if (a.pathname=='/cgi-bin/redirect.cgi') {
  264. a.href = cleanStaticiceRedir(a.search);
  265. if (a.host=='www.clixgalore.com' && a.pathname=='/PSale.aspx')
  266. a.href = cleanClixgalore(a.search);
  267. }
  268. else if (a.pathname.startsWith('/cgi-bin/www.'))
  269. a.href = 'http://'+a.pathname.slice(9);
  270. }
  271. if (a.host=='t.dgm-au.com' || a.host=='www.kqzyfj.com') a.href = cleanGenericRedir(a.search);
  272. if (a.search) a.search = cleanUtm(a.search);
  273. },
  274. twitter:function(a) {
  275. if (a.host=='t.co') {
  276. if (a.hasAttribute('data-expanded-url')) {
  277. var dirty = 'twitterRedir_'+a.pathname.slice(1);
  278. a.href = a.getAttribute('data-expanded-url');
  279. a.removeAttribute('data-expanded-url');
  280. sessionStorage.setItem(dirty, a.href);
  281. }
  282. else if (a.classList.contains('TwitterCard-container')) {
  283. var dirty = 'twitterRedir_'+a.pathname.slice(1);
  284. var clean = sessionStorage.getItem(dirty);
  285. if (clean) a.href = clean;
  286. }
  287. }
  288. },
  289. facebook:function(a) {
  290. if (a.hasAttribute('onclick') && a.hasAttribute('onmouseover')) {
  291. if (a.getAttribute('onclick').startsWith('LinkshimAsyncLink')) {
  292. a.removeAttribute('onclick');
  293. a.removeAttribute('onmouseover');
  294. if (a.host=='l.facebook.com')
  295. a.href = cleanGenericRedir(a.search);
  296. }
  297. }
  298. },
  299. imdb:function(a) {
  300. if (a.host=='www.imdb.com' && a.search)
  301. a.search = cleanImdb(a.search);
  302. }
  303. };
  304.  
  305. main()