forked from NovemLinguae/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectPromo.js
More file actions
222 lines (196 loc) · 4.73 KB
/
Copy pathDetectPromo.js
File metadata and controls
222 lines (196 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// <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
% growth
6-figure
7-figure
8-figure
9-figure
a record
around the world
best available
bestselling
comprehensive
countless hours
create a revolution
critical acclaim
disrupt
drastically
elevate
excelled
expert
expertise
extensive
fast growing
fast-growing
fastest-growing
fastest growing
growing popularity
highlights
highly praised
historic
honored with
hypnotic
impressive
inexhaustible
influential
innovation
innovative
leverag
massive
mastermind
more than
most highly
most important
most impressive
mystical
organically
outstanding
perfect
pioneer
prestigious
prominent
promulgator
ranked
renowned
reinvent
rising star
sensual
several offers
striking
transcend
transform
very first
wide selection
widely used
worldwide
B2B
B2C
inspired by
ventured into
globally
integrate
evangelist
legendary
zero to hero
are a necessity
philanthropist
entrepreneur
dynamic
engaging
save millions
pioneering
world-class
world class
respected
numerous
noteworthy
promising
signature
leading
underpin
exemplify
exemplified
fully integrated
fully-integrated
highly specialized
award-winning
leader in
leading
`;
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']);
}
}
let MAX_DISPLAYED_RESULTS = 20;
if ( searchResults.length > MAX_DISPLAYED_RESULTS ) {
searchResults = searchResults.slice(0, MAX_DISPLAYED_RESULTS);
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>