Skip to content

Instantly share code, notes, and snippets.

@rmnmjw
Last active February 1, 2024 09:37
Show Gist options
  • Save rmnmjw/f721219f7afbbbeca55d1b0d012c1e51 to your computer and use it in GitHub Desktop.
Save rmnmjw/f721219f7afbbbeca55d1b0d012c1e51 to your computer and use it in GitHub Desktop.
Wikipedia Skin Changer
// ==UserScript==
// @name Wikipedia Skin Changer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Changes the skin of Wikipedia
// @match *://*/*
// @grant none
// ==/UserScript==
(() => {
'use strict';
const waitForElm = (selector) => {
return new Promise(resolve => {
const elements = document.querySelectorAll(selector);
if (elements && elements.length !== 0) {
return resolve(elements);
}
const observer = new MutationObserver((/*mutations*/) => {
const elements = document.querySelectorAll(selector);
if (!elements || elements.length === 0) {
return;
}
resolve(elements);
observer.disconnect();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
};
const debounce = (cb) => {
let t;
return () => {
clearTimeout(t);
t = setTimeout(() => {
cb();
setTimeout(cb, 500);
setTimeout(cb, 1000);
setTimeout(cb, 2000);
setTimeout(cb, 4000);
}, 250);
};
}
//const [FIND_CLASS, SKIN] = ['skin-vector-2022', 'vector-2022'];
const [FIND_CLASS, SKIN] = ['skin-vector-legacy', 'vector'];
const site = new URL(document.location.href);
const on_wiki = /^(?<subdomain>[a-zA-Z]{1,4})(?<mobile>\.m)?(?<domain>\.(wikipedia|wikimedia|wiktionary).org)$/g.exec(site.host);
if (site.pathname === '/') {
return;
}
if (on_wiki?.groups?.mobile) {
site.host = on_wiki.groups.subdomain + on_wiki.groups.domain;
document.location.href = site.href;
return;
}
if (on_wiki) {
if (!document.body.classList.contains(FIND_CLASS) && !document.location.href.includes('upload.')) {
site.searchParams.set('useskin', SKIN);
document.location.href = site.href;
return;
} else {
site.searchParams.delete('useskin');
window.history.replaceState(null, document.title, site.href);
setTimeout(() => localStorage.setItem("uls-previous-languages", '["en","ru","ja","nl","de"]'), 100);
}
}
const fix_urls = debounce(() => {
const treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
let c = treeWalker.currentNode;
while (c) {
if (c.nodeName !== 'A' || c.href.trim() === '') {
c = treeWalker.nextNode();
continue;
}
const url = new URL(c.href);
if (!url.hostname.endsWith('wikipedia.org') && !url.hostname.endsWith('wikimedia.org')) {
c = treeWalker.nextNode();
continue;
}
if (url.searchParams.get('useskin') === SKIN) {
c = treeWalker.nextNode();
continue;
}
if (url.hash !== '') {
c = treeWalker.nextNode();
continue;
}
url.searchParams.set('useskin', SKIN);
c.href = url.href;
c = treeWalker.nextNode();
}
}, 100);
setTimeout(async () => {
fix_urls();
await waitForElm('section li')
const section = await waitForElm('section');
const observer = new MutationObserver(fix_urls);
const config = { attributes: true, childList: true, subtree: true };
observer.observe(section, config);
}, 100);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment