Utente:Alex brollo/getPage.js
Aspetto
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)
- Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5.
oggetto = "";
datiAjax = "";
function getPage(title, site) {
$.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) {
datiAjax = data;
lista = "<ul><li>La pagina non esiste</li></ul>";
oggetto = wikitext(data);
if (oggetto != "-1") lista = makeLista();
show(2);
});
}
function wikitext(oggetto) {
l = [];
for (i in oggetto.query.pages) {
l.push(i);
}
if (l[0] == "-1") return "-1";
testo = oggetto.query.pages[l[0]].revisions[0]["*"];
testo = testo.replace("{{Information", "{{information").replace("{{Book", "{{book").replace("{{Creator", "{{creator").replace("{{Bio", "{{bio");
dati = {
information: parseTemplate("information", testo),
book: parseTemplate("book", testo),
creator: parseTemplate("creator", testo),
bio: parseTemplate("bio", testo),
};
return dati;
}
function makeLista(data) {
lista = $("<ul></ul>");
$.each(oggetto["bio"][1], function (index, key) {
value = oggetto["bio"][0][key];
keyDisp = key;
if (key == "0") {
keyDisp = "Nome template:";
}
if (value != "") $("<li><b>" + keyDisp + ": </b>" + value + "</li>\n").appendTo(lista);
});
$.each(oggetto["creator"][1], function (index, key) {
value = oggetto["creator"][0][key];
keyDisp = key;
if (key == "0") {
keyDisp = "Nome template:";
}
if (value != "") $("<li><b>" + keyDisp + ": </b>" + value + "</li>\n").appendTo(lista);
});
$.each(oggetto["book"][1], function (index, key) {
value = oggetto["book"][0][key];
keyDisp = key;
if (key == "0") {
keyDisp = "Nome template:";
}
if (value != "") $("<li><b>" + keyDisp + ": </b>" + value + "</li>\n").appendTo(lista);
});
$.each(oggetto["information"][1], function (index, key) {
value = oggetto["information"][0][key];
keyDisp = key;
if (key == "0") {
keyDisp = "Nome template";
}
if (value != "") $("<li><b>" + keyDisp + ": </b>" + value + "</li>\n").appendTo(lista);
});
return lista;
}
function wikitext1(oggetto) {
l = [];
for (i in oggetto.query.pages) {
l.push(i);
}
testo = oggetto.query.pages[l[0]].revisions[0]["*"];
return testo;
}
/* questa funzione recupera via ajax l'html di qualsiasi pagina title di qualsiasi progetto site.
Restituisce un oggetto in cui l'HTML è contenuto in datiAjax.parse.text["*"]
*/
function getHtml(title, site) {
$.ajax({
url: 'https://' + site + '/w/api.php',
async: false,
data: {
format: 'json',
action: 'parse',
page: title,
prop: 'text'
},
dataType: 'jsonp' // this is the important one!
}).done(function (data) {
datiAjax = data;
});
}
/*
Nuova funzione interrogazione ajax interprogetto
title:titolo pagina (es: Giacomo_Leopardi)
site: sito (es: it.wikipedia.org)
query: tipo di interrogazione (valori ammessi: text|html , recupera wikitesto o html pagina)
callback: funzione callback da eseguire sui dati (opzionale)
le azioni di default sono il lancio di un'interrogazione ajax utilizzando API e:
l'intero oggetto json viene salvato in $("body").data("outputAjax")
i dati veri e propri (text o html) vengono salvati in $("body").data("bareData")
*/
function getIwPage(title, site, query, callback) {
if (query == undefined) query = "text";
if (query == "html" || query == undefined) data = {
format: 'json',
action: 'parse',
page: title,
prop: 'text'
};
if (query == "text") data = {
format: 'json',
action: 'query',
titles: title,
prop: 'revisions',
rvlimit: '1',
rvprop: 'content'
};
$.ajax({
url: 'https://' + site + '/w/api.php',
async: false,
data: data,
dataType: 'jsonp' // this is the important one!
}).done(function (data) {
$("body").data("outputAjax", data);
if (query === "text") {
if (data.query.pages[-1] == undefined) {
l = [];
for (i in data.query.pages) {
l.push(i);
};
bareData = data.query.pages[l[0]].revisions[0]["*"];
} else {
bareData = "Pagina non esistente";
}
if (query === "html") {
if (data.error == undefined) {
bareData = data.parse.text["*"];
} else {
bareData = "Pagina non esistente";
}
}
}
$("body").data("bareData", bareData);
if (callback !== undefined) {
callback();
}
});
}