User:Primaler/foreign titles.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:Primaler/foreign titles. |
/*
FOREIGN TITLES
Description: Shows titles of corresponding articles in user-selected languages when available.
foreign_languages is an array of language codes
foreign_language_separator defines the separator used for in ahref titles interwiki links (as seen in popups).
(Optional, default is "–" as in en many other wikis; but notin all, e.g. ru has "–")
place_titles_horizontally controls the placement (optional, default is false)
Example:
foreign_languages = ["ru", "bg"];
foreign_language_separator = "–";
place_titles_horizontally = false;
importScript("User:Primaler/foreign titles.js"); // Backlink: [[User:Primaler/foreign titles.js]]
*/
//$(document).ready(foreign_titles);
$(foreign_titles);
function foreign_titles()
{
if ((typeof(foreign_languages) === "undefined") || (foreign_languages === null) ||
(mw.config.get('wgNamespaceNumber') !== 0) || // only works in mainspace
(mw.config.get("wgPageName") === "Main_Page")) // not on the main page
{
return;
}
else
{
if (typeof(foreign_language_separator) === "undefined") foreign_language_separator = '–';
if (typeof(place_titles_horizontally) === "undefined") place_titles_horizontally = false;
var new_paragraph = document.createElement("p");
new_paragraph.style.fontSize = "11px";
var first = true;
for (var i = 0; i < foreign_languages.length; i++)
{
var lang = foreign_languages[i],
lang_blocks = document.getElementsByClassName("interlanguage-link interwiki-" + lang);
if (lang_blocks.length > 0) // interwiki link found
{
var lang_block = lang_blocks[0],
lang_ahref = lang_block.firstChild,
lang_url = lang_ahref.href,
lang_title_raw = lang_ahref.title;
// format: "article_name separator language"
var separator_string = " " + foreign_language_separator + " ",
title_end_ind = lang_title_raw.lastIndexOf(separator_string),
lang_title = lang_title_raw.substring(0, title_end_ind);
var new_brake = document.createElement("br"),
new_tab = document.createTextNode(" , "),
new_text = document.createTextNode(lang + ": "),
new_ahref = create_ahref(lang_url, lang_title);
if (!first) new_paragraph.appendChild(place_titles_horizontally ? new_tab : new_brake);
new_paragraph.appendChild(new_text);
new_paragraph.appendChild(new_ahref);
first = false;
}
}
var title_block = document.getElementById("firstHeading"),
parent = title_block.parentNode;
parent.insertBefore(new_paragraph, title_block.nextSibling); // insert new paragraph after title
}
}
function create_ahref(url, text)
{
var ahref = document.createElement("a"),
text_node = document.createTextNode(text);
ahref.setAttribute("href", url);
ahref.appendChild(text_node);
return ahref;
}