Utente:Alex brollo/GetPageNew.js

Da Wikisource.

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

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
// Initializes a div.ourDiv in it doesn't exist to save data; gets wikitext of a page from site, titled title; parses the template template, and save result into div.ourDiv; optionally runs callback().

function getPageNew(title, site, template, callback) {
	if ($(".ourDiv").length == 0) $(body).append($('<div style="display:none"></div>'));
	$.ajax({
		url: 'https://' + site + '/w/api.php',
		async: false,
		data: {
			format: 'json',
			action: 'query',
			titles: title,
			prop: 'revisions',
			rvlimit: '1',
			rvprop: 'content'
		},
		dataType: 'jsonp' // this is the important one!
	}).done(function (data) {
		var testo = getText(data);
		if (testo == "-1") {
			$(".ourDiv").data(site + "." + template.toLocaleLowerCase(), "-1");
		} else {
			var templateData = parseTemplate(template, testo).concat(data);
			$(".ourDiv").data(site + "." + template.toLocaleLowerCase(), templateData);
		}
	}


	);
}

// Gets text content from API objects stored into div.ourDiv
function getText(dato) {
	var l = "";
	for (i in dato.query.pages) {
		l = i;
		break;
	}
	if (l == "-1") return testo;
	var testo = dato.query.pages[l].revisions[0]["*"];
	return testo;
}