How to use:
Step 1: Install a script manager if you don't have one already. I use TamperMonkey, the most popular script manager on Firefox. I will be assuming you're using that script manager, too. You may want to give it permission to run in private windows.
Step 2: Click on TamperMonkey in your list of browser extensions, make sure it's enabled, then click "create a new script".
Step 3: Copy-paste the following script.
script
// ==UserScript==
// @name Wikipedia Language Redirector
// @version v3
// @match https://en.wikipedia.org/*
// @description Redirects wikipedia pages to alternate language pages in order of predefined preference
// @author Enjoyer_of_Games
// @author Edie
// @run-at document-idle
// @license AGPL 3+
// ==/UserScript==
const langBtn = document.querySelector('#p-lang-btn');
if (langBtn) {
let LanguageList = ['nb', 'nn', 'da', 'sv', 'eo', 'tok', 'ru', 'ja', 'zh'];
for (const langCode of LanguageList) {
let linktarget = document.querySelector(`a.interlanguage-link-target[lang="${langCode}"]`);
if(linktarget){
let langLink = linktarget.href;
if (langLink){
console.log(window.location);
console.log(`Navigating to language: ${langCode}`);
window.location.href = langLink;
break;
}
else {
console.warn(`Could not find language link for "${langCode}"`);
}
}
}
}
else {
console.log('Could not find the #p-lang-btn element');
}
Step 4: Modify the script at the following points:
Point 1:
// @match https://en.wikipedia.org/*
Replace the "en" in https://en.wikipedia.org/* with the Wikipedia language code of whatever your first language is.
Point 2:
let LanguageList = ['nb', 'nn', 'da', 'sv', 'eo', 'tok', 'ru', 'ja', 'zh'];
Replace 'nb', 'nn', 'da', 'sv', 'eo', 'tok', 'ru', 'ja', 'zh' with the Wikipedia language codes of your target languages in order of priority, using the same syntax. Note that you need to use "nb" as the language code for Norwegian Bokmål, not "no". Edie says to use ISO 639 codes instead of Wikipedia language codes, because of the nb/no issue, but I'm not sure if this is accurate.
Step 5: Click "file" → "save", then under TamperMonkey's "installed userscripts" tab make sure that the Wikipedia Language Redirector script is enabled.
Step 6: Test it out and see if it works!
hang on... wait a minute, why are we doing a while loop? What need is there to do it again after we have looped through it once? If we don't find a link from one of the languages, we shouldn't try again.
Here's v3, with the loop removed
ⓘ This user is suspected of being a cat. Please report any suspicious behavior.
I'm glad I've apparently given you a fun challenge. I've updated the post.
Regarding the language codes,
let LanguageList = ['be-tarask', 'nb', 'nn', 'da', 'sv', 'eo', 'tok', 'ru', 'ja', 'zh'];makes the script redirect me to Belarusian Taraškievica Wikipedia. So it seems to be a bit of a mix of Wikipedia and ISO 639 codes. In the cases where the codes differ, I think it's best to try one code and see if it works, and if the first code you tried doesn't work, to try the other code.So it's actually a BCP 47 language tag, per this MDN article: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/lang
ⓘ This user is suspected of being a cat. Please report any suspicious behavior.
You seem to have highlighted my code while clicking edit on the post, as it has quoted it at the end of the post.
ⓘ This user is suspected of being a cat. Please report any suspicious behavior.
D'oh!