Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vzvu3k6k/a7677d90d4427ead6cae to your computer and use it in GitHub Desktop.
Save vzvu3k6k/a7677d90d4427ead6cae to your computer and use it in GitHub Desktop.
Enhance links to other languages on Wikipedia
// ==UserScript==
// @name Wikipedia Language Labels
// @description Enhance interlanguage links on the sidebar (e.g. Change "Français" to "Étoile (Français)" on https://en.wikipedia.org/wiki/Star )
// @match https://*.wikipedia.org/wiki/*
// @grant none
// @version 2
// @noframes
// ==/UserScript==
// Original: https://gist.github.com/maripo/3925496
/*
Customize labels by editing LABEL_STYLE.
"${LABEL}", "${LANG_CODE}" and "${LANG_NAME}" will be replaced.
e.g.
"${LABEL} (${LANG_NAME})" => "Étoile (Français)"
"[${LANG_CODE}]${LABEL}" => "[fr]Étoile"
*/
const LABEL_STYLE = '${LABEL} (${LANG_NAME})';
const links = document.querySelectorAll('#p-lang .interlanguage-link > a');
for (let link of links) {
link.textContent = LABEL_STYLE.replace(/\${(\w+)}/g, (body, key) => {
switch (key) {
case 'LABEL':
return decodeURIComponent(link.pathname.substring(6)).replace(/_/g, ' ');
case 'LANG_CODE':
return link.lang;
case 'LANG_NAME':
return link.textContent;
default:
return body;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment