Skip to content

Instantly share code, notes, and snippets.

@maripo
Created October 21, 2012 02:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maripo/3925496 to your computer and use it in GitHub Desktop.
Save maripo/3925496 to your computer and use it in GitHub Desktop.
Enhance links to other languages on Wikipedia
// ==UserScript==
// @name Wikipedia Language Labels
// @namespace org.maripo.neta
// @description Enhance links to other languages (e.g. Change "Français" to "Étoile (Français)" on http://en.wikipedia.org/wiki/Star )
// @include http://*.wikipedia.org/wiki/*
// @grant none
// @version 1
// ==/UserScript==
(function () {
/*
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"
*/
var LABEL_STYLE = 'LABEL (LANG_NAME)';
var _REPLACEMENT_LABEL = 'LABEL';
var _REPLACEMENT_LANG_CODE = 'LANG_CODE';
var _REPLACEMENT_LANG_NAME = 'LANG_NAME';
var _REGEXP_LINK = new RegExp('http:\/\/([a-z\-]+)\.wikipedia\.org\/wiki\/([^#]+)');
var _REGEXP_SPACES = new RegExp('_', 'g');
var links = document.getElementById('p-lang').getElementsByTagName('A');
for (var i=0; i<links.length; i++) {
var link = links[i];
if (link.hreflang && link.href.match(_REGEXP_LINK)) {
var langCode = RegExp.$1;
var langName = link.innerHTML;
var label = decodeURI(RegExp.$2).replace(_REGEXP_SPACES, ' ');
link.innerHTML = LABEL_STYLE
.replace(_REPLACEMENT_LABEL, label)
.replace(_REPLACEMENT_LANG_CODE, langCode)
.replace(_REPLACEMENT_LANG_NAME, langName);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment