IMDb - stop scripts

Prevent unecessary scripts from loading

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

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