IMDb - stop scripts

Prevent unnecessary scripts from loading

当前为 2021-10-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IMDb - stop scripts
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.6.5
  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\.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) {
  71. if (!u) return;
  72. var el=document.createElement('script');
  73. el.src=u;
  74. if (ol) el.onload=ol;
  75. try {
  76. let r=document.head || document.documentElement;
  77. r.insertBefore(el,r.firstChild);
  78. }catch(e){}
  79. if (el.parentNode) el.parentNode.removeChild(el);
  80. }
  81.  
  82. var a, all=[], uniq={};
  83.  
  84. // load blocked scripts
  85. function fix() {
  86. if (search) search.oninput=null;
  87. a=document.querySelectorAll('script[src*="m.media-amazon.com/images/I/"], script[src*="d1zcggttmijv1z.cloudfront.net/_next/static/"]');
  88. a.forEach(function(e,i,a){
  89. if (!uniq[e.src]) all.push(e.src);
  90. uniq[e.src]=1;
  91. });
  92. loadJS();
  93. }
  94.  
  95. function fixPage() {
  96. if (location.href.startsWith('https://www.imdb.com/title/')) {
  97. var a=document.querySelectorAll('a[href="/"]');
  98. a.forEach(function(e){e.href='javascript:;'});
  99. }
  100. }
  101.  
  102. fixPage();
  103.  
  104. function loadJS() {
  105. if (all.length==0) return;
  106. addJS(all.shift()+'?', loadJS);
  107. }
  108.  
  109. if (location.pathname=='/') fix();
  110. if (/^https:\/\/www\.imdb\.com\/.*\/mediaviewer\//.test(location.href)) fix();
  111. }
  112.  
  113. })();