MediaWiki:Gadget-ErroriComuni.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() {
function showError(html) {
if ($('.boxErroriComuni').length === 0) {
$('.ns-108 .pagetext').before('<div class="boxErroriComuni"></div>');
$('.boxErroriComuni').html('<span class="erroriComuniTitle">Attenzione: la pagina contiene degli errori</span>');
}
$('.boxErroriComuni').append('<br>' + html);
}
function templateRegex(tlName) {
return "\\{\\{[" + tlName[0].toUpperCase() + tlName[0].toLowerCase() + "]" + tlName.slice(1) + "\\|";
}
function capitalize(tlName) {
return tlName[0].toUpperCase() + tlName.slice(1).toLowerCase();
}
function checkTemplates() {
templatesNotFound = $('.pagetext .new:contains("Template:")');
if (templatesNotFound.length) {
templateNames = '';
templatesNotFound.each(function(i) {
if (templateNames !== '')
templateNames += ', ';
templateNames += $(this).text().replace('Template:', '');
});
showError('I seguenti template non esistono: <b>' + templateNames + '</b>');
}
// se la pagina già esiste
if (mw.config.get('wgArticleId') != 0) {
wt = acchiappaWikicode(mw.config.get("wgPageName")).join();
internal = ['smaller', 'x-smaller', 'larger', 'x-larger', 'xx-larger', 'sc', 'type'];
external = ['centrato', 'a destra', 'ct'];
internal.forEach(function(int) {
external.forEach(function(ext) {
regex = new RegExp(templateRegex(int) + templateRegex(ext), "gm");
if (regex.test(wt)) {
showError('Il template <b>' + capitalize(int) + '</b> va messo dentro a <b>' + capitalize(ext) + '</b>, non viceversa.'
+ '<br>In alternativa valuta se usare <b>' + capitalize(int) + ' block</b>');
}
});
});
}
}
var maxWords = 5;
var maxChars = 25;
var ellipsis = '…';
var marker = '|||';
function getWordsBefore(textArray, p) {
start = p - maxWords;
if (start < 0)
start = 0;
text = textArray.slice(start, p).join('_');
if (text.length > maxChars)
text = ellipsis + text.substring(text.length - maxChars);
text = text.replace(/_/g, ' ').replace(marker, '');
return text;
}
function getWordsAfter(textArray, p) {
end = p + maxWords + 1;
if (end > pureText.length)
end = pureText.length;
text = textArray.slice(p + 1, end).join('_');
if (text.length > maxChars)
text = text.substring(0, maxChars) + ellipsis;
text = text.replace(/_/g, ' ').replace(marker, '');
return text;
}
function linkToPage(str, num) {
return '<a href="#pag' + num + '">' + str + '</a>';
}
function checkPageBreaks() {
if ( (mw.config.get("wgAction") != "view" && mw.config.get("wgAction") != 'edit') || mw.config.get("wgCanonicalNamespace") != "")
return;
pureText = $('.prp-pages-output')
.clone()
.find('style').remove().end().text()
.replace(/\[p\.\s+(\S+.*?)\s+modifica\]/g, ' ' + marker + '$1 ')
.replace(/ /g, '_').replace(/\s/gm, ' ')
.split('_');
//console.log(pureText);
// remove first page break
if (pureText[0].trim() == '')
pureText.splice(0, 1);
if (pureText[0].startsWith(marker))
pureText.splice(0, 1);
pagenums = [];
textBefore = [];
textAfter = [];
for (i = 0; i < pureText.length; i++) {
if (pureText[i].startsWith(marker)) {
pagenum = pureText[i].replace(marker, '');
textBefore.push(getWordsBefore(pureText, i));
textAfter.push(getWordsAfter(pureText, i));
pagenums.push(pagenum);
// console.log(pagenum + ': ' + textBefore[textBefore.length-1] + "|" + textAfter[textAfter.length-1]);
}
}
if (pagenums.length > 0) {
$('#mw-content-text').prepend('<div class="box-check-pagebreak box">'
+ '<div class="box-check-pagebreak-title box-title">Controlla interruzioni di pagina'
+ '<span class="icon-close" id="box-check-pagebreak-close"></span></div>'
+ '<div class="box-check-pagebreak-body box-main"><table></table></div></div>');
for (i = 0; i < pagenums.length; i++) {
$('.box-check-pagebreak table').append('<tr><td>' + linkToPage(textBefore[i], pagenums[i])
+ '</td><td>' + linkToPage(textAfter[i], pagenums[i]) + '</td></tr>');
}
$('#box-check-pagebreak-close').click(function(e) {
localStorage.checkPageBreaks = "false";
$('.box-check-pagebreak').remove();
});
$('.box-check-pagebreak').draggable({
create: function( event, ui ) {
$(this).css({
right: "auto",
bottom: "auto",
top: $(this).position().top,
left: $(this).position().left
});
}
});
}
}
function checkMissingImages() {
var missingImg = $('a.new[title^="File:"]');
var num = missingImg.length;
if (num > 0) {
missingImg.parent().css('border', '3px solid red');
if (num == 1) {
showError("C'è un'immagine che non si carica, forse il link è sbagliato?");
} else if (num > 1) {
showError("Ci sono " + num + " immagini che non si caricano, forse il link è sbagliato?");
}
}
}
if ($('.numeropagina').length > 1) {
mw.util.addPortletLink('p-tb', '#', 'Controlla interruzioni di pagina', 'option-check-pagebreaks', "Verifica che le parole spezzate tra una pagina e l'altra siano ricomposte correttamente");
isActive = localStorage.checkPageBreaks;
if (isActive != undefined && isActive == "true")
checkPageBreaks();
$('#option-check-pagebreaks a').click(function(e) {
e.preventDefault();
isActive = localStorage.checkPageBreaks;
if (isActive != undefined && isActive == "true") {
$('#box-check-pagebreak-close').click();
} else {
localStorage.checkPageBreaks = "true";
checkPageBreaks();
}
});
}
// controlla che i link alla precedente e successiva in ns0 funzionino
function controllaPrecSucc() {
if (mw.config.get("wgNamespaceNumber") == 110 && $('.indice-sommario-titolo a.new').length == 0) {
var msg = [];
var calls = 0;
checkPrecSucc = function(title) {
console.log('Check prec/succ di pagina: ' + title);
calls++;
$.ajax({
url: "/w/index.php?&title=" + title
}).done(function(response) {
calls--;
if ($('.capitoloPrec .new', response).length) {
msg.push('La pagina ' + title + ' non punta alla precedente');
}
if ($('.capitoloSucc .new', response).length) {
msg.push('La pagina ' + title + ' non punta alla successiva');
}
if (calls == 0) {
if (msg.length > 0) {
alert(msg.join('\n'));
} else {
$('.top_icon_cont').append('<div class="top_icon_raw"><img title="I link a precedente e successiva in ns0 risultano funzionanti" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/fb/Yes_check.svg/15px-Yes_check.svg.png" width="15" height="15"></div>');
}
}
});
};
$('.indice-sommario-titolo a').not('.new').each(function(i) {
title = $(this).attr('href').replace('/wiki/', '');
if (title.indexOf('/') != -1) {
title = title.substring(0, title.indexOf('#'));
checkPrecSucc(title);
}
});
}
}
if (mw.config.get("wgAction") == "view") {
checkTemplates();
checkMissingImages();
controllaPrecSucc();
}
});