MediaWiki:Gadget-espandiCategoria.js
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.
(function($,mw) {
if (mw.config.get('wgPageName') == 'Categoria:Da_cancellare_subito')
return true;
mw.loader.using("mediawiki.api", function() {
var api = new mw.Api();
//function that puts the page titles in a wikitext and parses it
parseText = function(titles, tlName, parent, temp, i, anchor) {
j = i;
textToParse = '';
while (j < titles.length) {
if (titles[j].indexOf('/') != -1) {
basePage = titles[j].substring(0, titles[j].indexOf('/'));
title = titles[j].substring(titles[j].lastIndexOf('/') + 1);
textToParse += "\n* ''[[" + titles[j];
if (anchor) {
textToParse += '#' + anchor;
}
textToParse += '|' + title + "]]'', in {" + '{' + tlName + '|' + basePage + '|' + basePage.replace(/\s*\([0-9A-Z].*\)/, '') + '}}';
} else {
textToParse += '\n* {' + '{' + tlName + '|' + titles[j] + '|' + titles[j].replace(/\s*\([0-9A-Z].*\)/, '');
if (anchor) {
textToParse += '|' + anchor;
}
textToParse += '}}';
}
j++;
}
//console.log("Text to parse: " + textToParse);
console.log('Gadget EspandiCategoria - loading items ' + (i+1) + '-' + j + ' (from \"' + titles[i] + '\" to \"' + titles[j-1] + '\")');
//parse wikitext into an html fragment
api.post({
action: 'parse', contentmodel: 'wikitext', prop: 'text', disablelimitreport: true, text: textToParse
}).done(function (data) {
if (data && data.parse && data.parse.text) {
//put the html in the temporary container, then replace every list element with the one generated by the parsing
temp.append(data.parse.text["*"]);
temp.find('ul li').each(function(index) {
newLink = $(this).find('a');
if (newLink.length > 0) {
link = newLink.attr('title').replace(/"/g, '\\"');
parent.find('li a[title="' + link + '"]').parent('li').empty().append($(this).html());
}
});
temp.remove();
}
});
};
getAnchor = function() {
catPage = mw.config.get("wgPageName");
pattern1 = "in_cui_è_citato_il_testo_";
index1 = catPage.indexOf(pattern1);
if (index1 > -1) {
return catPage.substring(index1 + pattern1.length);
}
pattern2 = "in_cui_è_citato_";
index2 = catPage.indexOf(pattern2);
if (index2 > -1) {
return catPage.substring(index2 + pattern2.length);
}
return undefined;
};
//search for titles to be "expanded"
if (mw.config.get("wgCanonicalNamespace") == "Category") {
// trova la prima pagina ordinando per titolo
startPrefix = $($('#mw-pages ul li a').sort(function(a, b) {
return ($(b).attr('title')) < ($(a).attr('title')) ? 1 : -1;
})[0]).attr('title');
//get list of ns0 pages in the category we're in
api.get({
action: 'query',
format: 'json',
list: 'categorymembers',
cmnamespace: '0',
cmtype: 'page',
cmlimit: 200,
cmtitle: mw.config.get("wgPageName"),
cmstartsortkeyprefix: startPrefix
}).done(function (data) {
if (data.query && data.query.categorymembers) {
titles = [];
for (i = 0; i < data.query.categorymembers.length; i++) {
thisTitle = data.query.categorymembers[i].title;
titles.push(thisTitle);
}
console.log('EspandiCategoria - found ' + titles.length + ' ns0 pages');
if (titles.length > 0) {
//temporary container for the generated html
$('#mw-pages').after('<div id="espandiCategoria" style="display:none">');
parseText(titles, 'TAA', $('#mw-pages'), $('#espandiCategoria'), 0, getAnchor());
$('.cancellaultraRapidaButton').remove();
}
}
});
}
else {
$('.espandiLista').each(function(index) {
var titles = [];
var tlName = $(this).data('tlname');
var parent = $(this).next('ol, ul');
parent.find('li a').each(function(index) {
titles.push($(this).attr('title'));
});
console.log('EspandiLista - found ' + titles.length + ' pages');
if (titles.length > 0) {
//temporary container for the generated html
$(this).after('<div id="espandiLista' + index + '" style="display:none">');
parseText(titles, tlName, parent, $('#espandiLista' + index), 0);
}
});
}
});
})($,mw);