Jump to content

User:SD0001/userscript-load-caching.min.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Enable caching for resource loads, see [[User:SD0001/Making_user_scripts_load_faster]], @revision 8
if(!/\bnocache=\b/.test(location.href)){let e=mw.config.values,t="text/javascript",o="text/css",n=(n,r,i)=>(n=n.replace(/special:mypage/i,"User:"+e.wgUserName),$.get("https://"+r+"/w/api.php?titles="+n+"&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400&action=query&prop=revisions|info&rvprop=content&rvlimit=1&inprop=protection").then((e=>{let a=e.query.pages[0];if(!a.missing){if(2!==a.ns&&8!==a.ns&&!a.protection.find((e=>"edit"===e.type&&"sysop"===e.level)))return $.Deferred().reject('Refused to load "'+n+'"@'+r+": unprotected page");let e=a.revisions[0].content;if(i&&i!==t||"javascript"!==a.contentmodel){if(i!==o||"css"!==a.contentmodel)return $.Deferred().reject('Refused to load "'+n+'"@'+r+": content type mismatch");mw.loader.addStyleTag(e)}else{let t=document.createElement("script");t.innerHTML=e,document.head.appendChild(t)}}}))),r=e.wgServerName,i=e=>{let t=/^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(e),o=/\btitle=([^=?&]*)/.exec(e);return t&&o&&/\baction=raw\b/.test(e)&&/\bctype=/.test(e)?[o[1],t[1]||r]:null};window.importScript=e=>{n(encodeURIComponent(e),r,t)},window.importStyleSheet=e=>{n(encodeURIComponent(e),r,o)};let a=mw.loader.load;mw.loader.load=function(e,t){let o=i(e);o?n(o[0],o[1],t):a.apply(mw.loader,[...arguments])};let l=mw.loader.getScript;mw.loader.getScript=function(e){let o=i(e);return o?n(o[0],o[1],t):l.apply(mw.loader,[...arguments])}}