Utente:Sanjilops/Scorreggitore.user.js
Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.
Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti, e comunque qualche minuto di preghiera non guasta. Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menu Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.
// ==UserScript==
// @name Scorreggitore
// @namespace Nonciclopedia
// @exclude http://riciclopedia.wiki-site.com/index.php?title=Speciale:*
// @exclude http://nonciclopedia.wikia.com/index.php?title=Speciale:*
// @include http://riciclopedia.wiki-site.com/index.php?title=*&action=edit*
// @include http://riciclopedia.wiki-site.com/index.php?title=*&action=submit*
// @include http://nonciclopedia.wikia.com/index.php?title=*&action=edit*
// @include http://nonciclopedia.wikia.com/index.php?title=*&action=submit*
// @version 0.1
// ==/UserScript==
var scriptname = "Scorreggitore";
/* Configurazione */
var Livello_Auto = 3; /* max 3; 0 = disabilitato; */
var Reg_Restauro = /\{\{\s*?(restauro|inrestauro|darestaurare|formattazione|form|accusa|apu|controlla|cont)/i;
/* ----- Non toccare ----- */
var Auto = new Array();
var Man = new Array();
var page, title, namespace;
function Scorrezioni () {
var set = Man;
/****************************/
/* Sostituzioni manuali */
/* Fine sostituzioni manuali */
/* ----- Non toccare ----- */
if(!Livello_Auto) return;
set = Auto;
/****************************/
/* Sostituzioni automatiche */
/* Liv. 1*/
/* Fine liv. 1 */
if (Livello_Auto < 2) return;
/* Liv. 2 */
add("\'Apici\' -> \"Virgolette\"", /([^\w\'])\'(.+?[^\'])\'(?![\'\w])/g, "$1\"$2\"");
/* Fine liv. 2 */
if (Livello_Auto < 3) return;
/* Liv. 3 */
/* Fine liv. 3 */
/* ----- Fine della configurazione ----- */
function add (name, exp, patt, sugg, enable, L, S) {
if (enable == undefined) enable = true;
if (!enable) return;
if (L != undefined && L > Livello_Auto) return; // L come argomento opzionale permette di cambiare il livello senza spostare il rigo
if (S == undefined) S = set;
if (sugg == (undefined || 0)) sugg = false;
S.push(new Reg (name, exp, patt, sugg));
}
return;
}
function Restauro() {
var text = document.getElementById("wpTextbox1").value;
return Reg_Restauro.test(text);
}
function Reg (name, exp, patt, sugg) {
this.name = name;
this.exp = exp;
this.patt = patt;
this.sugg = sugg;
this.Suggest = function (t) {
if (!this.sugg || !(/%RAND%/.test(this.patt))) return t.replace(this.exp, this.patt);
var n = Math.round(Math.random() * (this.sugg.length - 1));
var s = this.patt.replace(/%RAND%/g, this.sugg[n]);
return t.replace(this.exp, s);
}
}
var textbox, originaltext;
var header, inputBox, suggestMenu;
function menuReset() {
var o;
for each (o in suggestMenu.options) suggestMenu.remove(o); // anche "annulla" va rimosso e riaggiunto per essere aggiornato
return;
}
function menuSubmit () {
var o = this.options[this.selectedIndex];
if (o.id == scriptname + "-menu-null") return;
var s = o.value;
if (inputBox.selectionStart < inputBox.selectionEnd && o.id != scriptname + "-menu-cancel") {
var t = inputBox.value;
inputBox.value = t.slice(0, inputBox.selectionStart) + s + t.slice(inputBox.selectionEnd);
}
else inputBox.value = s;
this.selectedIndex = this.length - 2;
return;
}
function Cancel() {
if(confirm("Annullare tutte le modifiche?"))
textbox.value = originaltext;
return;
}
var currentReg = -1;
var currentMatch = -1;
var currentPos = 0;
var matches;
function DisableButtons () {
suggestMenu.removeEventListener('change', menuSubmit, false);
function dis_butt(id, f) {
var b = document.getElementById(scriptname + id);
b.removeEventListener('click', f, false);
b.disabled = true;
}
dis_butt("-submit", Submit);
dis_butt("-skipmatch", nextMatch);
dis_butt("-skipreg", nextReg);
dis_butt("-cancel", Cancel);
return;
}
function Close () {
DisableButtons();
document.getElementById('bodyContent').removeChild(document.getElementById(scriptname + "-wrapper"));
document.getElementById(scriptname + "-loadbutton").disabled = false;
}
function startReg () {
currentMatch = -1;
currentPos = 0;
var r = Man[currentReg];
var m = textbox.value.match(r.exp);
if (!m) nextReg();
else {
matches = m;
nextMatch();
}
return;
}
function nextReg() {
++currentReg;
if (currentReg == Man.length) {
alert("Finito!");
menuReset();
inputBox.value = '';
DisableButtons();
}
else {
header.textContent = Man[currentReg].name;
startReg()
}
return;
}
function nextMatch () {
var r = Man[currentReg];
var s, i;
++currentMatch;
menuReset();
if (currentMatch == matches.length) startReg();
else {
var M = matches[currentMatch];
var index = textbox.value.indexOf(M, currentPos);
textbox.focus();
textbox.select();
textbox.selectionStart = index;
textbox.selectionEnd = index + M.length;
inputBox.value = r.Suggest(M);
currentPos = index + M.length;
for each (i in r.sugg) {
var o = document.createElement('option');
if (/%RAND%/.test(r.patt)) {
s = r.patt.replace(/%RAND%/g, i);
s = M.replace(r.exp, s);
}
else {
s = i;
o.disabled = true;
}
o.value = s;
o.text = s.replace('\n', '\\n');
suggestMenu.add(o, null);
}
o = document.createElement('option');
o.text = '';
o.id = scriptname + "-menu-null";
suggestMenu.add(o, null);
o = document.createElement('option');
o.value = M;
o.text = "annulla";
o.id = scriptname + "-menu-cancel";
suggestMenu.add(o, null);
}
return;
}
function Submit () {
var t = textbox.value;
var s = inputBox.value;
var len1 = t.length;
textbox.value = t.slice(0, textbox.selectionStart) + s + t.slice(textbox.selectionEnd);
var len2 = textbox.value.length;
if ( len1 > len2) currentPos -= len1 - len2;
else currentPos += len2 - len1;
nextMatch();
return;
}
var stop_auto;
function AutoSubmit(e) {
function stop () {
stop_auto = true;
document.removeEventListener('keydown', AutoSubmit, true);
return;
}
switch (e.which) {
case 145: // scroll lock
alert("Interrotto");
stop();
return;
case 19: // pause
textbox.disabled = false;
if (!confirm("Pausa. Premi OK per riprendere o Annulla per interrompere")) stop();
return;
case 224: // meta
break;
default:
return;
}
var t = textbox.value;
var s = inputBox.value;
textbox.value = t.slice(0, textbox.selectionStart) + s + t.slice(textbox.selectionEnd);
}
function Run () {
this.disabled = true;
textbox = document.getElementById("wpTextbox1");
originaltext = textbox.value;
Scorrezioni();
var wrapper = document.createElement('table');
document.getElementById("bodyContent").insertBefore(wrapper, document.getElementById("editform"));
wrapper.id = scriptname + "-wrapper";
var trA = document.createElement('tr');
wrapper.appendChild(trA);
header = document.createElement('th');
header.id = scriptname + "-header";
trA.appendChild(header);
var header_right = document.createElement('td');
header_right.id = scriptname + "-buttons-wrap";
trA.appendChild(header_right);
var skipregButton = document.createElement('input');
skipregButton.type = 'button';
skipregButton.value = "Filtro successivo";
skipregButton.id = scriptname + "-skipreg";
skipregButton.disabled = true;
header_right.appendChild(skipregButton);
var cancelButton = document.createElement('input');
cancelButton.type = 'button';
cancelButton.value = "Annulla tutto";
cancelButton.id = scriptname + "-cancel";
cancelButton.disabled = true;
header_right.appendChild(cancelButton);
var closeButton = document.createElement('input');
closeButton.type = 'button';
closeButton.value = "Chiudi";
closeButton.id = scriptname + "-close";
closeButton.disabled = true;
header_right.appendChild(closeButton);
var trB = document.createElement('tr');
wrapper.appendChild(trB);
var td1 = document.createElement('td');
td1.id = scriptname + "-input-wrap";
trB.appendChild(td1);
inputBox = document.createElement('textarea');
inputBox.cols = "60";
inputBox.rows="3";
inputBox.id = scriptname + "-input";
inputBox.disabled = true;
td1.appendChild(inputBox);
var td2 = document.createElement('td');
td2.id = scriptname + "-form-wrap";
trB.appendChild(td2);
var submitButton = document.createElement('input');
submitButton.type = 'button';
submitButton.value = "Sostituisci";
submitButton.id = scriptname + "-submit";
submitButton.disabled = true;
td2.appendChild(submitButton);
var skipmatchButton = document.createElement('input');
skipmatchButton.type = 'button';
skipmatchButton.value = "Salta";
skipmatchButton.id = scriptname + "-skipmatch";
skipmatchButton.disabled = true;
td2.appendChild(skipmatchButton);
suggestMenu = document.createElement('select');
suggestMenu.id = scriptname + "-menu";
suggestMenu.multiple = false;
suggestMenu.disabled = true;
td2.appendChild(suggestMenu);
var conf = false;
if (Auto.length) conf = confirm("Avviare le sostituzioni automatiche?");
if (conf) {
var r, m, t, e;
document.addEventListener('keydown', AutoSubmit, true);
textbox.disabled = true;
for each (r in Auto) {
header.textContent = r.name;
do {
t = textbox.value;
m = r.exp.exec(t);
if (!m) continue;
textbox.focus();
textbox.select();
textbox.selectionEnd = r.exp.lastIndex;
textbox.selectionStart = textbox.selectionEnd - m[0].length;
inputBox.value = r.Suggest(m[0]);
e = document.createEvent('KeyEvents');
e.initKeyEvent('keydown', false, false, null, false, false, false, false, 224, 0);
document.dispatchEvent(e);
} while (m && !stop_auto);
if (stop_auto) break;
}
textbox.disabled = false;
}
/*
conf = false;
if (Man) conf = confirm("Avviare la procedura manuale?"));
if (!conf) Close();
*/
function en_butt(b, f) {
b.addEventListener('click', f, false);
b.disabled = false;
}
en_butt(closeButton, Close);
currentReg = -1;
currentMatch = -1;
currentPos = 0;
suggestMenu.addEventListener('change', menuSubmit, false);
suggestMenu.disabled = false;
inputBox.disabled = false;
en_butt(submitButton, Submit);
en_butt(skipmatchButton, nextMatch);
en_butt(skipregButton, nextReg);
en_butt(cancelButton, Cancel);
nextReg();
return;
}
function Main () {
var bar = document.getElementById("toolbar");
if (!bar || (unsafeWindow.wgAction != "edit" && unsafeWindow.wgAction !="submit")) return false;
Pagina = unsafeWindow.wgPageName;
Titolo = unsafeWindow.wgTitle;
Namespace = unsafeWindow.wgCanonicalNamespace;
var button = document.createElement('input');
button.type = 'button';
button.id = scriptname + "-loadbutton";
var buttonwrap = document.createElement('table');
buttonwrap.align = 'right';
buttonwrap.appendChild(button);
bar.appendChild(buttonwrap);
button.value = scriptname;
button.addEventListener('click', Run, false);
return true;
}
var css = "#SN-wrapper {border: 2px solid #999; padding: 2px;} #SN-header {text-align: center;} #SN-buttons-wrap {margin: 2px; padding: 1px; float: right;} #SN-input-wrap {margin: 2px; padding: 1px;} #SN-input {margin: 3px; background-color: #eaeaff; float: left;} #SN-form-wrap {margin: 2px; padding: 1px;} #SN-form-wrap input {float: center;} #SN-menu {padding-left: 2px; width: 200px; margin: 3px; float: center;} #SN-menu-cancel {text-align: center; font-style: italic; background-color: #f2f2f2; border-top: 2px solid black;} #SN-loadbutton {margin-bottom: 2px;}";
GM_addStyle(css.replace(/SN/g, scriptname));
Main();