MediaWiki:Gadget-Iwt.js

Da Wikivoyage.

Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tenere premuto il tasto delle maiuscole e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer: tenere premuto il tasto Ctrl mentre si fa clic su Refresh, oppure premere Ctrl-F5
  • Opera: svuotare completamente la cache dal menu Strumenti → Preferenze
/*
Recupera l'html di una pagina in un sito
*/

function getHtmlNew(parametri) {
	$.ajax(parametri.ajax).done(function (data) {
		parametri.callback( parametri, data);
	});
}

function callback1(parametri, data) {
	// estraggo l'html dall'oggetto JSON
	var html = data.parse.text["*"]

	// rendo assoluti i link relativi
	html = html.replace(/href=\"\/wiki\//g, 'href=//' + parametri.sito + '/wiki/')
	html = html.replace(/href=\"\/w\//g, 'href=//' + parametri.sito + '/w/');

	// faccio comparire html in .iwt
	$(".iwtContent", $(".iwt").eq(parametri.indice)).html("").append($(html));
}

function getHtmlGo(titolo, sito, indice) {
	if (titolo == undefined) var titolo = $(".nomePagGet").text();
	if (sito == undefined) var sito = $(".sitoPagGet").text();
	sito = sito.split(":");
	sito = sito[0] + "." + sito[1] + ".org";
	var parametri = {
		sito: sito,
		titolo: titolo,
		indice: indice,
		ajax: {
			url: 'https://' + sito + '/w/api.php',
			async: false,
			data: {
				format: 'json',
				action: 'parse',
				page: titolo,
				prop: 'text'
			},
			dataType: 'jsonp'
		},
                callback: callback1
	}

	getHtmlNew(parametri, callback1);
	$(".itw .bottone").eq(indice).remove();
}


$(document).ready(function () {
	$(".iwt").each(function (index) {
		var titolo = $(".nomePagGet", $(this)).text();
		var sito = $(".sitoPagGet", $(this)).text();
		$(".bottone", $(this)).append($('<button onclick="getHtmlGo(&quot;' + titolo + '&quot;,&quot;' + sito + '&quot;,' + index + ')">Visualizza da ' + sito + '</button>'));
		// $(this).append($('<div class="iwtContent mw-collapsible-content" style="border:1px dotted blue"></div>'));
	})
});


/*
function g(title, lang, site) {
      $.ajax({
           url: 'https://'+lang+'.'+site+'/w/api.php',
           async:false,
           data: {
           format: 'json',
           action: 'parse',
           page: title,
           prop: 'langlinks|iwlinks', 
                 },
      dataType: 'jsonp' 
      }).done(function (data) {
       var l=[];
       $.each(data.parse.langlinks, function (index,value) {
          if (value.lang=="en" || value.lang=="it" || value.lang=="fr" || value.lang=="de" || value.lang=="el") {
              l.push([value["*"],value.lang]);
          }
       });
          $(".ourDiv").data("langlinks",l); 
          });
} */