User:GeneralNotability/arcwordcount.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:GeneralNotability/arcwordcount. |
// <nowiki>
const awc_SKIP_SECTION_RE = /(:?Involved parties|.*: Clerk notes|.*: Arbitrators' opinion.*|{Non-party})/i;
const awc_DIFF_RE = /(:?\[\[(:?Special:Diff|Special:Permalink).*?\]\]|oldid=)/ig;
async function awc_getSectionWordCount(sectionId) {
'use strict';
const api = new mw.Api();
const getRequest = {
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
indexpageids: true,
titles: mw.config.get( "wgPageName" ),
rvsection: sectionId
};
let sectionText = '';
try {
const response = await api.get(getRequest);
const pageid = response.query.pageids[0];
sectionText = response.query.pages[pageid].revisions[0].slots.main['*'];
} catch (error) {
console.error('Error fetching text for section ' + sectionId + ': ' + error);
return 0;
}
sectionText = sectionText.replace(/={3,}.*={3,}\n/, '');
const splitSectionText = sectionText.split(/\s+/);
return splitSectionText.length;
}
async function awc_getSectionDiffCount(sectionId) {
'use strict';
const api = new mw.Api();
const getRequest = {
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
indexpageids: true,
titles: mw.config.get( "wgPageName" ),
rvsection: sectionId
};
let sectionText = '';
try {
const response = await api.get(getRequest);
const pageid = response.query.pageids[0];
sectionText = response.query.pages[pageid].revisions[0].slots.main['*'];
} catch (error) {
console.error('Error fetching text for section ' + sectionId + ': ' + error);
return 0;
}
return (sectionText.match(awc_DIFF_RE) || []).length;
}
$(async function () {
if( mw.config.get( "wgPageName" ) === "Wikipedia:Arbitration/Requests/Case" ) {
const api = new mw.Api();
const response = await api.get({
action: 'parse',
prop: 'sections',
page: mw.config.get( "wgPageName" )
});
response.parse.sections.forEach(async(section) => {
if (section.level === '3') {
if (section.line.match(awc_SKIP_SECTION_RE)) {
// Don't care about these administrative sections
return;
}
const sectionLength = await awc_getSectionWordCount(section.index);
const diffCount = await awc_getSectionDiffCount(section.index);
const selector = 'span#' + $.escapeSelector(section.anchor) + '.mw-headline';
$element = $(selector, document)[0];
$element.after(' (' + sectionLength + ' words, ' + diffCount + ' diffs)');
}
});
} else if( mw.config.get( "wgPageName" ) === "Wikipedia:Arbitration/Requests/Enforcement") {
const api = new mw.Api();
const response = await api.get({
action: 'parse',
prop: 'sections',
page: mw.config.get( "wgPageName" )
});
response.parse.sections.forEach(async(section) => {
if (section.level === '4') {
if (section.line.match(awc_SKIP_SECTION_RE)) {
// Don't care about these administrative sections
return;
}
const sectionLength = await awc_getSectionWordCount(section.index);
const diffCount = await awc_getSectionDiffCount(section.index);
const selector = 'span#' + $.escapeSelector(section.anchor) + '.mw-headline';
$element = $(selector, document)[0];
$element.after(' (' + sectionLength + ' words, ' + diffCount + ' diffs)');
}
});
} else if (mw.config.get("wgPageName").match(/Wikipedia:Arbitration\/Requests\/Case\/.*?\/Evidence/)) {
const api = new mw.Api();
const response = await api.get({
action: 'parse',
prop: 'sections',
page: mw.config.get( "wgPageName" )
});
const caseSections = [];
response.parse.sections.forEach(async(section) => {
if (section.level === '2' && section.line.match(/Evidence presented by .*/i)) {
const sectionLength = await awc_getSectionWordCount(section.index);
const diffCount = await awc_getSectionDiffCount(section.index);
const selector = 'span#' + $.escapeSelector(section.anchor) + '.mw-headline';
$element = $(selector, document)[0];
$element.after(' (' + sectionLength + ' words, ' + diffCount + ' diffs)');
}
});
}
});
// </nowiki>