MediaWiki:Gadget-Wordcount.js: Difference between revisions
Tag: Rollback |
No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
function getWordCount(html) { | function getWordCount(html) { | ||
const text = html.innerHTML.replace(/(<([^>]+)>)/ig, "").trim(); | |||
const words = text.match(/[\u3040-\u309F]|[\u30A0-\u30FF]|[\u4E00-\u9FFF\uF900-\uFAFF\u3400-\u4DBF]|\S+/g); | |||
return words ? words.length : 0; | |||
} | } | ||
function getContentDiv() { | function getContentDiv() { | ||
return mw.config.get('wgAction') === 'edit' | |||
? document.getElementById('wikiPreview') | |||
: document.getElementById('mw-content-text'); | |||
} | } | ||
function | function isValidNode(node, conditions) { | ||
const bodyContent = getContentDiv(); | |||
let currentNode = node.parentElement; | |||
while (currentNode && currentNode !== bodyContent) { | |||
if (conditions.some(cls => currentNode.classList.contains(cls))) { | |||
return false; | |||
} | |||
currentNode = currentNode.parentElement; | |||
} | |||
return true; | |||
} | } | ||
function toggleWordCount() { | function toggleWordCount() { | ||
const bodyContent = getContentDiv(); | |||
let output = document.getElementById("word-count-stats"); | |||
if (output) { | |||
// Clean up | |||
output.remove(); | |||
document.getElementById("word-count-header")?.remove(); | |||
} else { | |||
generateStatistics(bodyContent); | |||
} | |||
} | } | ||
function generateStatistics(bodyContent) { | |||
const output = document.createElement("div"); | |||
output.id = "word-count-stats"; | |||
function | output.innerHTML = ` | ||
<div id="word-count-header"><b>شمارشگر کلمات</b></div> | |||
<table class="word-count-table"> | |||
<tr><td>محتوای اصلی:</td><td id="main-body-stat"></td></tr> | |||
<tr><td>ارجاعات:</td><td id="ref-stat"></td></tr> | |||
<tr><td>جمع کل:</td><td id="total-stat"></td></tr> | |||
</table>`; | |||
bodyContent.prepend(output); | |||
let mainBodyCount = 0, refCount = 0; | |||
const processNodes = (nodes, isReference) => { | |||
nodes.forEach(node => { | |||
const count = getWordCount(node); | |||
if (count > 0) { | |||
if (isReference(node)) { | |||
refCount += count; | |||
node.style.backgroundColor = "cyan"; | |||
} else { | |||
mainBodyCount += count; | |||
node.style.backgroundColor = "yellow"; | |||
} | |||
} | |||
}); | |||
}; | |||
const paragraphs = Array.from(bodyContent.getElementsByTagName("p")); | |||
processNodes(paragraphs, node => false); | |||
document.getElementById("main-body-stat").innerText = `${mainBodyCount} واژه`; | |||
document.getElementById("ref-stat").innerText = `${refCount} واژه`; | |||
document.getElementById("total-stat").innerText = `${mainBodyCount + refCount} واژه`; | |||
} | } | ||
Revision as of 09:06, 16 November 2024
function getWordCount(html) {
const text = html.innerHTML.replace(/(<([^>]+)>)/ig, "").trim();
const words = text.match(/[\u3040-\u309F]|[\u30A0-\u30FF]|[\u4E00-\u9FFF\uF900-\uFAFF\u3400-\u4DBF]|\S+/g);
return words ? words.length : 0;
}
function getContentDiv() {
return mw.config.get('wgAction') === 'edit'
? document.getElementById('wikiPreview')
: document.getElementById('mw-content-text');
}
function isValidNode(node, conditions) {
const bodyContent = getContentDiv();
let currentNode = node.parentElement;
while (currentNode && currentNode !== bodyContent) {
if (conditions.some(cls => currentNode.classList.contains(cls))) {
return false;
}
currentNode = currentNode.parentElement;
}
return true;
}
function toggleWordCount() {
const bodyContent = getContentDiv();
let output = document.getElementById("word-count-stats");
if (output) {
// Clean up
output.remove();
document.getElementById("word-count-header")?.remove();
} else {
generateStatistics(bodyContent);
}
}
function generateStatistics(bodyContent) {
const output = document.createElement("div");
output.id = "word-count-stats";
output.innerHTML = `
<div id="word-count-header"><b>شمارشگر کلمات</b></div>
<table class="word-count-table">
<tr><td>محتوای اصلی:</td><td id="main-body-stat"></td></tr>
<tr><td>ارجاعات:</td><td id="ref-stat"></td></tr>
<tr><td>جمع کل:</td><td id="total-stat"></td></tr>
</table>`;
bodyContent.prepend(output);
let mainBodyCount = 0, refCount = 0;
const processNodes = (nodes, isReference) => {
nodes.forEach(node => {
const count = getWordCount(node);
if (count > 0) {
if (isReference(node)) {
refCount += count;
node.style.backgroundColor = "cyan";
} else {
mainBodyCount += count;
node.style.backgroundColor = "yellow";
}
}
});
};
const paragraphs = Array.from(bodyContent.getElementsByTagName("p"));
processNodes(paragraphs, node => false);
document.getElementById("main-body-stat").innerText = `${mainBodyCount} واژه`;
document.getElementById("ref-stat").innerText = `${refCount} واژه`;
document.getElementById("total-stat").innerText = `${mainBodyCount + refCount} واژه`;
}