User:Novem Linguae/Scripts/DetectPromo.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:Novem Linguae/Scripts/DetectPromo. |
// <nowiki>
/*
- Let reviewer know when certain promotional and POV keywords are detected.
- Displays an orange bar at the top of the article, listing the detected keywords.
*/
$(async function() {
async function getWikicode(title) {
if ( ! mw.config.get('wgCurRevisionId') ) return ''; // if page is deleted, return blank
var wikicode = '';
title = encodeURIComponent(title);
await $.ajax({
url: 'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json',
success: function (result) {
wikicode = result['parse']['wikitext'];
},
dataType: "json",
async: false
});
return wikicode;
}
function eliminateDuplicates(array) {
return uniq = [...new Set(array)];
}
/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
function getArticleName() {
return mw.config.get('wgPageName');
}
function hasDiacritics(str) {
let str2 = str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
return str != str2;
}
function normalizeDiacritics(str) {
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
function cloneArray(arr) {
return JSON.parse(JSON.stringify(arr));
}
function empty(arr) {
if ( arr === undefined ) return true;
if ( arr.length == 0 ) return true;
return false;
}
function escapeRegEx(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
// don't run when not viewing articles
let action = mw.config.get('wgAction');
if ( action != 'view' ) return;
// don't run when viewing diffs
let isDiff = mw.config.get('wgDiffNewId');
if ( isDiff ) return;
let isDeletedPage = ( ! mw.config.get('wgCurRevisionId') );
if ( isDeletedPage ) return;
// Only run in mainspace and draftspace
let namespace = mw.config.get('wgNamespaceNumber');
let title = getArticleName();
if ( ! [0, 118].includes(namespace) && title != 'User:Novem_Linguae/sandbox' ) return;
let wordString = `
// An impressive amount of promo in this draft: https://en.wikipedia.org/w/index.php?title=Draft:Imre_Van_opstal&oldid=1060259849
critical acclaim
highly praised
hypnotic
inexhaustible
most impressive
mystical
perfect
prestigious
prominent
renowned
sensual
striking
transcends
rising star
growing popularity
create a revolution
comprehensive
several offers
countless hours
drastically
comprehensive
wide selection
mastermind
bestselling
expertise
6-figure
7-figure
8-figure
9-figure
highlights
extensive
`;
wordString = wordString.replace(/^\/\/.*$/gm, ''); // replace comment lines with blank lines. using this approach fixes a bug involving // and comma on the same line
let wordArray = wordString.replace(/, /g, "\n")
.trim()
.split("\n")
.map(v => v.trim(v))
.filter(v => v != '')
.filter(v => ! v.startsWith('//'));
wordArray = eliminateDuplicates(wordArray);
// convert from 1 level array with just text, to 2 level array with text and regex
let wordObject = [];
for ( let key in wordArray ) {
wordObject.push({
'text': wordArray[key],
'regex': escapeRegEx(wordArray[key])
});
}
let wikicode = await getWikicode(title);
// eliminate [[ ]], so that phrases with wikilink syntax in the middle don't mess up our search
wikicode = wikicode.replace(/\[\[/g, '')
.replace(/\]\]/g, '');
let searchResults = [];
for ( let word of wordObject ) {
// can't use \b here because \)\b doesn't work correctly. using lookarounds instead
let regEx = new RegExp('(?<!\\w)' + word['regex'] + '(?!\\w)', "i");
if ( wikicode.match(regEx) ) {
searchResults.push(word['text']);
}
}
if ( searchResults.length > 10 ) {
searchResults = searchResults.slice(0, 10);
searchResults.push('...... and more.');
}
if ( ! empty(searchResults) ) {
let html = searchResults.join(', ');
html = '<div id="DetectPromo" style="background-color: orange"><span style="font-weight: bold;">Promotional words:</span> ' + html + '</div>';
$('#contentSub').before(html);
}
});
// </nowiki>