Jump to content

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

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 21:29, 15 June 2024 (restore namespace check but allow protected pages in other namespaces). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 7
if(!/\bnocache=\b/.test(location.href)){let e=(e,t,r)=>(e=e.replace(/special:mypage/i,"User:"+mw.config.get("wgUserName")),$.get("https://"+t+"/w/api.php?titles="+e+"&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400&action=query&prop=revisions|info&rvprop=content&rvlimit=1&inprop=protection").then(n=>{let i=n.query.pages[0];if(i.missing)return;if(2!==i.ns&&8!==i.ns&&!i.protection.find(e=>"edit"===e.type&&"sysop"===e.level))return $.Deferred().reject('Refused to load "'+e+'"@'+t+": unprotected page");let o=i.revisions[0].content;if(r&&"text/javascript"!==r||"javascript"!==i.contentmodel){if("text/css"!==r||"css"!==i.contentmodel)return $.Deferred().reject('Refused to load "'+e+'"@'+t+": content type mismatch");mw.loader.addStyleTag(o)}else{let a=document.createElement("script");a.innerHTML=o,document.head.appendChild(a)}})),t=e=>{let t=/^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(e),r=/\btitle=([^=?&]*)/.exec(e);return t&&r&&/\baction=raw\b/.test(e)&&/\bctype=/.test(e)?[r[1],t[1]||mw.config.get("wgServerName")]:null};window.importScript=t=>{e(encodeURIComponent(t),mw.config.get("wgServerName"),"text/javascript")},window.importStyleSheet=t=>{e(encodeURIComponent(t),mw.config.get("wgServerName"),"text/css")};let r=mw.loader.load;mw.loader.load=function(n,i){let o=t(n);o?e(o[0],o[1],i):r.apply(mw.loader,[...arguments])};let n=mw.loader.getScript;mw.loader.getScript=function(r){let i=t(r);return i?e(i[0],i[1],"text/javascript"):n.apply(mw.loader,[...arguments])}}