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 16:52, 13 June 2021 (use <script> element instead of eval: improves consistency with index.php-based loading, such as that functions defined like in User:Timotheus Canens/displaymessage.js get attached globally.). 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 3
if(!/\bnocache=\b/.test(location.href)){var e=function(e,t,r){return e=e.replace(/special:mypage/i,"User:"+mw.config.get("wgUserName")),$.get("https://"+t+"/w/api.php?titles="+encodeURIComponent(e)+"&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400&action=query&prop=revisions|info&rvprop=content&rvlimit=1").then((function(n){var a=n.query.pages[0];if(!a.missing){var o=a.revisions[0].content;if(r&&"text/javascript"!==r||"javascript"!==a.contentmodel){if("text/css"!==r||"css"!==a.contentmodel)return $.Deferred().reject('Refused to load "'+e+'"@'+t+": content type mismatch");mw.loader.addStyleTag(o)}else{var i=document.createElement("script");i.innerHTML=o,document.head.appendChild(i)}}}))},t=function(e){var 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=function(t){e(t,mw.config.get("wgServerName"),"text/javascript")},window.importStyleSheet=function(t){e(t,mw.config.get("wgServerName"),"text/css")};var r=mw.loader.load;mw.loader.load=function(n,a){var o=t(n);o?e(o[0],o[1],a):r.apply(mw.loader,Array.prototype.slice.call(arguments))};var n=mw.loader.getScript;mw.loader.getScript=function(r){var a=t(r);return a?e(a[0],a[1],"text/javascript"):n.apply(mw.loader,Array.prototype.slice.call(arguments))}}