User:SD0001/userscript-load-caching.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:SD0001/userscript-load-caching. |
// Enable caching for resource loads, see [[User:SD0001/Making_user_scripts_load_faster]], @revision 3
if (!/\bnocache=\b/.test(location.href)) { // Don't enable if nocache=1 url parameter is given
var loadResource = function(page, sitename, ctype) {
page = page.replace(/special:mypage/i, 'User:' + mw.config.get('wgUserName'));
return $.get(
'https://' + sitename + '/w/api.php?titles=' + encodeURIComponent(page) +
'&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400' +
'&action=query&prop=revisions|info&rvprop=content&rvlimit=1'
).then(function(apiResponse) {
var apiPage = apiResponse.query.pages[0];
if (apiPage.missing) {
return;
}
var content = apiPage.revisions[0].content;
if ((!ctype || ctype === 'text/javascript') && apiPage.contentmodel === 'javascript') {
var scriptTag = document.createElement('script');
scriptTag.innerHTML = content;
document.head.appendChild(scriptTag);
} else if (ctype === 'text/css' && apiPage.contentmodel === 'css') {
mw.loader.addStyleTag(content);
} else {
return $.Deferred().reject('Refused to load "' + page + '"@' + sitename + ': content type mismatch');
}
});
};
var getSiteTitle = function(url) {
var siteRgx = /^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(url),
titleRgx = /\btitle=([^=?&]*)/.exec(url);
if (siteRgx && titleRgx && /\baction=raw\b/.test(url) && /\bctype=/.test(url)) {
return [titleRgx[1], siteRgx[1] || mw.config.get('wgServerName')];
} else return null;
};
window.importScript = function(page) {
loadResource(page, mw.config.get('wgServerName'), 'text/javascript');
};
window.importStyleSheet = function(page) {
loadResource(page, mw.config.get('wgServerName'), 'text/css');
};
var oldMwLoaderLoad = mw.loader.load;
mw.loader.load = function(url, type) {
var linkParts = getSiteTitle(url);
if (linkParts) {
loadResource(linkParts[0], linkParts[1], type);
} else {
oldMwLoaderLoad.apply(mw.loader, Array.prototype.slice.call(arguments));
}
};
var oldMwLoaderGetScript = mw.loader.getScript;
mw.loader.getScript = function(url) {
var linkParts = getSiteTitle(url);
if (linkParts) {
return loadResource(linkParts[0], linkParts[1], 'text/javascript');
} else {
return oldMwLoaderGetScript.apply(mw.loader, Array.prototype.slice.call(arguments));
}
};
}