MediaWiki:Mobile.js

From imedwiki
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* All JavaScript here will be loaded for users of the mobile site */

/* dealing with interwikilinks */

var interwikiSuffixes = Array();
const interwikiSuffixTemplates = {"wikipedia":"{{Template:InterwikilinkWikipedia}}", "wikipedia-de":"{{Template:InterwikilinkWikipedia}}"};
var interwikiLinksSorted = {};

/* this function returns all interwikilinks 
return jQuery results */
function getAllInterwikiLinks() {
	interwikilinks = $("#content a.extiw");
	return interwikilinks;
}

/* return destination of interwikilink

extract information from link title (i.e. "wikipedia:Hauptseite"). 
Title gets split by : and the first part is returned */
function getInterwikiDest(link) {
	return link.title.split(":")[0];
}

function mySuccess (data, textStatus, jqXHR) {
   // console.log("data", data, "textStatus", textStatus, "jqXHR", jqXHR, "this", this);
   interwikiLinkDest = this.interwikiLinkDest;
   text = $(data.parse.text["*"]).find("p").html(); 
   // console.log("text:", text);
   
   currentInterwikiLinks = interwikiLinksSorted[interwikiLinkDest];
   for (var i = 0; i < currentInterwikiLinks.length; i++) {
  	    currentInterwikiLink = currentInterwikiLinks[i];
  	    // console.log("add suffix", currentInterwikiLink)
  	    dest = getInterwikiDest(currentInterwikiLink);
		currentInterwikiLink.innerHTML += text;
	}
}

/* add interwiki destination to sorted array interwikiLinksSorted */
function addInterwikiDest(interwikiLink) {
	dest = getInterwikiDest(interwikiLink);
	if (!(dest in interwikiLinksSorted)) {
		interwikiLinksSorted[dest] = [interwikiLink];
	} else {
		interwikiLinksSorted[dest].push(interwikiLink);
	}
}

/* get all interwikilinks and sort them into array interwikiLinksSorted */
function sortAllInterwikiLinks() {
	interwikiLinks = getAllInterwikiLinks();
	for (var i = 0; i < interwikiLinks.length; i++) {
		interwikiLink = interwikiLinks[i];
		addInterwikiDest(interwikiLink);
	}
}

sortAllInterwikiLinks();

/* 
1) Get all interwikilinks. 
2) Sort them by destinations. 
3) Make ajax call for every destination defined by templates in interwikiSuffixTemplates.
Ajax success function appends return value to all interwikilinks found in first step. */
function appendInterwikiLinks() { 
  interwikiLinkDests = Object.keys(interwikiLinksSorted);
  for (var i = 0; i < interwikiLinkDests.length; i++) {
  	interwikiLinkDest = interwikiLinkDests[i];
  	// console.log("fetch", interwikiLinkDest)
	$.ajax({
		interwikiLinkDest: interwikiLinkDest,
        url: mw.util.wikiScript('api'),
        data: { action: 'parse', text: interwikiSuffixTemplates[interwikiLinkDest], format: 'json', contentmodel: 'wikitext'},
        async: true,
        success: mySuccess
	});
  }
}

appendInterwikiLinks();