User:SD0001/skin-pref.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/skin-pref. |
// Use timeless skin while on mobile
// Make skin in url persist when links are clicked, useful for testing scripts across different skins
var onMobile = /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
var useskin = location.search.match(/useskin=([^&]*)/) && location.search.match(/useskin=([^&]*)/)[1];
if (useskin || onMobile) {
var skin = useskin || 'timeless';
// Insert useskin= param into every link
var fixAnchor = function fixAnchor(a) {
var href = a.href;
var baseurl = '', params = '', hash = '';
var indexOfQ = href.indexOf('?');
var indexOfH = href.indexOf('#');
if (indexOfH === -1) {
indexOfH = undefined;
}
if (indexOfQ === -1 || (indexOfH && indexOfQ > indexOfH)) {
indexOfQ = undefined;
}
baseurl = href.slice(0, indexOfQ || indexOfH);
if (indexOfH) {
hash = href.slice(indexOfH);
}
if (indexOfQ) {
params = href.slice(indexOfQ, indexOfH);
}
if (baseurl && !/(?:&|\?)useskin=/.test(params)) {
var param = (params ? '&' : '?') + 'useskin=' + skin;
a.href = hash ? a.href.replace('#', param + '#') : a.href + param;
}
};
// This adds &useskin= param to the destination url on submitting a form
// Doesn't work for Search and the edit page and many other places
var fixForm = function fixForm(form) {
$(form).append(
$('<input>', { name: 'useskin', value: skin, type: 'hidden' })
);
};
var observer = new MutationObserver(function(mutationsList, observer) {
for (let mutation of mutationsList) {
// console.log(mutation);
//console.log('mutation observed');
for (let addednode of mutation.addedNodes) {
// console.log('added node ' + addednode.tagName);
if (addednode.tagName === 'A') {
fixAnchor(addednode);
// console.log('a with id ' + addednode.id + ' fixed');
} else if (addednode.tagName === 'FORM' &&
$(addednode).attr('action') &&
$(addednode).attr('action').startsWith('/w/index.php')) {
fixForm(addednode);
// console.log('form with id ' + addednode.id + ' fixed');
}
}
}
});
observer.observe(document, { childList: true, subtree: true });
mw.hook('wikipage.content').add(function() {
$('a').each(function() {
fixAnchor(this);
});
$("form[action^='/w/index.php']").each(function() {
fixForm(this);
});
});
}