IMDb - stop scripts

Prevent unnecessary scripts from loading

  1. // ==UserScript==
  2. // @name IMDb - stop scripts
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.7.1
  5. // @description Prevent unnecessary scripts from loading
  6. // @author Achernar
  7. // @match https://www.imdb.com/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14.  
  15. // which scripts to block
  16. function match(s) {
  17. return /^https?:\/\/(m\.media-amazon\.com\/images\/I\/|(d1zcggttmijv1z|dqpnq362acqdi)\.cloudfront\.net\/_next\/static\/).*[^?]$/.test(s);
  18. }
  19.  
  20. // catch scripts before they are loaded
  21. var obs=new MutationObserver(function(muts){
  22. for (let mut of muts) {
  23. for (let n of mut.addedNodes) {
  24. if ((n.nodeType == 1) && (n.tagName == 'SCRIPT')) {
  25. let src=n.src;
  26. if (!src) continue;
  27. if (match(src)) {
  28. n.type='not/javascript';
  29. n.addEventListener('beforescriptexecute', function(e){e.preventDefault();}, true);
  30. }
  31. }
  32. }
  33. }
  34. });
  35.  
  36. var c=0;
  37. function startObs() {
  38. c++;
  39. if (!document) {
  40. setTimeout(startObs,0);
  41. return;
  42. }
  43. obs.observe(document, {childList:true, subtree:true});
  44. injSt();
  45. }
  46. startObs();
  47.  
  48. function injSt() {
  49. var r=document.head || document.documentElement;
  50. if (!r) {
  51. setTimeout(injSt,0);
  52. return;
  53. }
  54. var st=document.createElement('style');
  55. r.appendChild(st);
  56. st.innerText='.ipc-loader__circle, .ipc-loader__dot {animation: unset !important;} section.ipc-page-section[class*="Hero__HeroParent-"] .ipc-chip-list {display: block;} [data-testid="plot"]:not(:last-child) {display: inline-block; margin-right: -2em;} [data-testid="plot"] ~ .ipc-button__text {display: inline;}[data-testid="delayed-loader-test-id"]{display:none;}';
  57. }
  58.  
  59. // load rest of the script after DOM is ready
  60. if (document.readyState != 'loading') init();
  61. else document.addEventListener('DOMContentLoaded', init);
  62.  
  63. function init() {
  64.  
  65. var search=document.getElementById('suggestion-search');
  66. if (!search && !/^https:\/\/www\.imdb\.com\/.*\/mediaviewer\//.test(location.href) ) return;
  67.  
  68. if (search) search.oninput=fix;
  69.  
  70. function addJS(u, ol, c=2) {
  71. if (!u) return;
  72. var el=document.createElement('script');
  73. el.src=u;
  74. if (ol) el.onload=ol;
  75. el.onerror=function(){
  76. if (c) addJS(u,ol,--c);
  77. }
  78. try {
  79. let r=document.head || document.documentElement;
  80. r.insertBefore(el,r.firstChild);
  81. }catch(e){}
  82. if (el.parentNode) el.parentNode.removeChild(el);
  83. }
  84.  
  85. var a, all=[], uniq={}, tot=0, done=true;
  86.  
  87. // load blocked scripts
  88. function fix(repeat=false) {
  89. if (search) search.oninput=null;
  90. a=document.querySelectorAll('script[src*="m.media-amazon.com/images/I/"], script[src*="d1zcggttmijv1z.cloudfront.net/_next/static/"], script[src*="dqpnq362acqdi.cloudfront.net/_next/static/"]');
  91. a.forEach(function(e,i,a){
  92. if (!uniq[e.src]) all.push(e.src);
  93. uniq[e.src]=1;
  94. });
  95. let uTot=Object.keys(uniq).length;
  96. if (tot == uTot) return;
  97. tot=uTot;
  98. if (done) {
  99. done=false;
  100. loadJS();
  101. }
  102. if (repeat) {
  103. setTimeout(function(){fix(true)},1000);
  104. setTimeout(function(){fix(true)},3000);
  105. setTimeout(function(){fix()},8000);
  106. }
  107. }
  108.  
  109. function fixPage() {
  110. if (location.href.startsWith('https://www.imdb.com/title/')) {
  111. var a=document.querySelectorAll('a[href="/"]');
  112. a.forEach(function(e){e.href='javascript:;'});
  113. }
  114. }
  115.  
  116. fixPage();
  117.  
  118. function loadJS() {
  119. if (all.length==0) {
  120. done=true;
  121. return;
  122. }
  123. addJS(all.shift()+'?', loadJS);
  124. }
  125.  
  126. if (location.pathname=='/') fix(true);
  127. if (/^https:\/\/www\.imdb\.com\/(video|.*\/mediaviewer)\//.test(location.href)) fix(true);
  128. }
  129.  
  130. })();