User:Primaler/foreign titles.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
	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;
}