IMDb - fix links

Removes all tracking info from imdb links. Keeps other parameters intact.

  1. // ==UserScript==
  2. // @name IMDb - fix links
  3. // @namespace https://github.com/Procyon-b
  4. // @version 1.2.2
  5. // @description Removes all tracking info from imdb links. Keeps other parameters intact.
  6. // @author Achernar
  7. // @match https://www.imdb.com/*
  8. // @run-at document-body
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14. var t;
  15.  
  16. function fix(u) {
  17. return u.replace('?', '?&').replace(/&(pf_rd_[a-z]|ref_)=[^&#]*/g, '').replace('?&', '?').replace(/\?$/, '');
  18. }
  19.  
  20. function normalizeLnk(a) {
  21. if (/^\/(list|name|title)\/[^\/]+$/.test(a.pathname)) a.pathname+='/';
  22. if (/^(\/title\/.*\/fullcredits)\/cast\/?$/.test(a.pathname)) {
  23. a.pathname=RegExp.$1;
  24. a.hash='cast';
  25. }
  26. if (/^(\/title\/[^\/]+\/.*)\/$/.test(a.pathname)) {
  27. a.pathname=RegExp.$1;
  28. }
  29. }
  30.  
  31. if (t=location.search) {
  32. let s=fix(t);
  33. if (s!=t) history.replaceState(null, null, (s || location.pathname)+location.hash);
  34. }
  35.  
  36. var num=1, stop=0;
  37. function fixL(L, j=0, n=num++) {
  38. if (stop && (stop > n)) return;
  39. let i=0, a, S=Date.now()+200;
  40. for (;a=L[j];j++) {
  41. if (a.LnkFixed || !a.href) continue;
  42. if (Date.now() > S) {
  43. if ( !(stop && (stop > n)) ) setTimeout(function(){fixL(L,j,n)},0);
  44. return;
  45. }
  46. if (a.protocol && a.protocol.startsWith('http') && a.host.endsWith('imdb.com') && a.search) {
  47. let s=fix(a.search);
  48. if (a.search != s) a.search=s;
  49. normalizeLnk(a);
  50. }
  51. if (a.pathname!=='/') a.LnkFixed=1;
  52. }
  53. }
  54.  
  55. var SI=0;
  56. function init() {
  57. new MutationObserver(function(mutL){
  58. for (let m of mutL) {
  59. if (m.addedNodes) {
  60. let e=m.target.querySelectorAll(':scope a');
  61. if (e.length) fixL(e);
  62. }
  63. }
  64. }).observe(document, {childList:true, subtree:true});
  65.  
  66. clearInterval(SI);
  67. SI=-1;
  68. stop=num;
  69. fixL(document.links);
  70. }
  71.  
  72. window.addEventListener('load', function(){fixL(document.links)});
  73. if (document.readyState != 'loading') init();
  74. else document.addEventListener('DOMContentLoaded', init);
  75.  
  76. if (SI<0) return;
  77. setTimeout(function(){fixL(document.links);},0);
  78. SI=setInterval(function(){
  79. stop=num;
  80. fixL(document.links);
  81. },250);
  82. setTimeout(function(){stop=num;clearInterval(SI);},2000);
  83.  
  84. })();