MediaWiki:Common.js
Da WikiLectio.
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.
/* Il codice JavaScript inserito qui viene caricato da ciascuna pagina, per tutti gli utenti. */
/* ===== Auto evidenziazione "Punti oscuri" ===== */
(function() {
if (mw.config.get('wgAction') !== 'view') return;
function getMarkers() {
var q = document.getElementById('po-queue');
if (!q) return [];
return Array.prototype.slice.call(q.querySelectorAll('.po-item'))
.map(function(el){
var t = (el.getAttribute('data-po-text') || '').trim();
var title = (el.getAttribute('data-po-title') || '').trim();
return t ? {text: t, title: title} : null;
})
.filter(Boolean);
}
// Trova e avvolge la prima occorrenza testuale di "needle" in un container con class po-highlight
function highlightFirst(container, needle) {
if (!needle || needle.length < 3) return false; // evita match troppo corti
// Normalizza spazi multipli
needle = needle.replace(/\s+/g, ' ').trim();
var walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT, {
acceptNode: function(node) {
if (!node.nodeValue) return NodeFilter.FILTER_REJECT;
// ignora testo troppo corto
if (node.nodeValue.trim().length < 3) return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
}
});
var node;
while ((node = walker.nextNode())) {
var hay = node.nodeValue.replace(/\s+/g, ' ');
var idx = hay.indexOf(needle);
if (idx !== -1) {
var range = document.createRange();
// Per calcolare offset originali, rimappiamo gli indici ignorando la normalizzazione
// Approccio semplice: usiamo la stringa originale e cerchiamo senza normalizzare
var raw = node.nodeValue;
var rawIdx = raw.indexOf(needle);
if (rawIdx === -1) {
// fallback: prova una ricerca case-insensitive
var lowerIdx = raw.toLowerCase().indexOf(needle.toLowerCase());
if (lowerIdx === -1) continue;
rawIdx = lowerIdx;
}
range.setStart(node, rawIdx);
range.setEnd(node, rawIdx + needle.length);
var span = document.createElement('span');
span.className = 'po-highlight';
range.surroundContents(span);
return true;
}
}
return false;
}
function addToggle() {
var content = document.getElementById('mw-content-text');
if (!content) return;
var bar = document.createElement('div');
bar.style = 'margin:0 0 .6rem 0; display:flex; gap:.5rem; align-items:center; font-size:.9em;';
var btn = document.createElement('button');
btn.textContent = 'Mostra/Nascondi evidenziazioni';
btn.className = 'po-toggle';
btn.onclick = function() {
var on = document.body.getAttribute('data-po-on') === '1';
document.body.setAttribute('data-po-on', on ? '0' : '1');
};
bar.appendChild(btn);
// Inserisci prima del primo paragrafo
var first = content.querySelector(':scope > *');
content.insertBefore(bar, first);
}
function run() {
var markers = getMarkers();
if (!markers.length) return; // niente da evidenziare
var container = document.getElementById('mw-content-text');
if (!container) return;
// Applica evidenziazioni
markers.forEach(function(m) {
highlightFirst(container, m.text);
});
// Attiva toggle
addToggle();
document.body.setAttribute('data-po-on', '1');
}
// Esegui dopo caricamento DOM
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run);
} else {
run();
}
})();