IMDb - stop scripts

Prevent unnecessary scripts from loading

目前为 2021-08-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IMDb - stop scripts
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.6.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\.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.documentElement) {
  40. setTimeout(startObs,0);
  41. return;
  42. }
  43. obs.observe(document.documentElement, {childList:true, subtree:true});
  44. let st=document.createElement('style');
  45. (document.head || document.documentElement).appendChild(st);
  46. st.innerText='.ipc-loader__circle {animation: unset !important;}';
  47. }
  48.  
  49. startObs();
  50.  
  51. // load rest of the script after DOM is ready
  52. if (document.readyState != 'loading') init();
  53. else document.addEventListener('DOMContentLoaded', init);
  54.  
  55. function init() {
  56.  
  57. var search=document.getElementById('suggestion-search');
  58. if (!search && !/^https:\/\/www\.imdb\.com\/.*\/mediaviewer\//.test(location.href) ) return;
  59.  
  60. if (search) search.oninput=fix;
  61.  
  62. function addJS(u, ol) {
  63. if (!u) return;
  64. var el=document.createElement('script');
  65. el.src=u;
  66. if (ol) el.onload=ol;
  67. try {
  68. let r=document.head || document.documentElement;
  69. r.insertBefore(el,r.firstChild);
  70. }catch(e){}
  71. if (el.parentNode) el.parentNode.removeChild(el);
  72. }
  73.  
  74. var a, all=[], uniq={};
  75.  
  76. // load blocked scripts
  77. function fix() {
  78. if (search) search.oninput=null;
  79. a=document.querySelectorAll('script[src*="m.media-amazon.com/images/I/"], script[src*="d1zcggttmijv1z.cloudfront.net/_next/static/"]');
  80. a.forEach(function(e,i,a){
  81. if (!uniq[e.src]) all.push(e.src);
  82. uniq[e.src]=1;
  83. });
  84. loadJS();
  85. }
  86.  
  87. function fixPage() {
  88. if (location.href.startsWith('https://www.imdb.com/title/')) {
  89. var a=document.querySelectorAll('a[href="/"]');
  90. a.forEach(function(e){e.href='javascript:;'});
  91. }
  92. }
  93.  
  94. fixPage();
  95.  
  96. function loadJS() {
  97. if (all.length==0) return;
  98. addJS(all.shift()+'?', loadJS);
  99. }
  100.  
  101. if (location.pathname=='/') fix();
  102. if (/^https:\/\/www\.imdb\.com\/.*\/mediaviewer\//.test(location.href)) fix();
  103. }
  104.  
  105. })();