@@ -21,14 +21,17 @@ class UserHighlighterSimple {
2121 const $links = this . $ ( '#article a, #bodyContent a, #mw_contentholder a' ) ;
2222 $links . each ( ( index , element ) => {
2323 this . $link = this . $ ( element ) ;
24- if ( ! this . linksToAUser ( ) ) {
24+ const url = this . $link . attr ( 'href' ) ;
25+ if ( ! this . linksToAUser ( url ) ) {
2526 return ;
2627 }
28+
2729 this . user = this . getUserName ( ) ;
2830 const isUserSubpage = this . user . includes ( '/' ) ;
2931 if ( isUserSubpage ) {
3032 return ;
3133 }
34+
3235 this . hasAdvancedPermissions = false ;
3336 this . addClassesAndHoverTextToLinkIfNeeded ( ) ;
3437 // If the user has any advanced perms, they are likely to have a signature, so be aggressive about overriding the background and foreground color. That way there's no risk their signature is unreadable due to background color and foreground color being too similar. Don't do this for users without advanced perms... being able to see a redlinked username is useful.
@@ -38,42 +41,6 @@ class UserHighlighterSimple {
3841 } ) ;
3942 }
4043
41- addCSS ( htmlClass , cssDeclaration ) {
42- // .plainlinks is for Wikipedia Signpost articles
43- // To support additional custom signature edge cases, add to the selectors here.
44- this . mw . util . addCSS ( `
45- .plainlinks .${ htmlClass } .external,
46- .${ htmlClass } ,
47- .${ htmlClass } b,
48- .${ htmlClass } big,
49- .${ htmlClass } font,
50- .${ htmlClass } kbd,
51- .${ htmlClass } small,
52- .${ htmlClass } span {
53- ${ cssDeclaration }
54- }
55- ` ) ;
56- }
57-
58- async getWikitextFromCache ( title ) {
59- const api = new this . mw . ForeignApi ( 'https://en.wikipedia.org/w/api.php' ) ;
60- let wikitext = '' ;
61- await api . get ( {
62- action : 'query' ,
63- prop : 'revisions' ,
64- titles : title ,
65- rvslots : '*' ,
66- rvprop : 'content' ,
67- formatversion : '2' ,
68- uselang : 'content' , // needed for caching
69- smaxage : '86400' , // cache for 1 day
70- maxage : '86400' // cache for 1 day
71- } ) . then ( ( data ) => {
72- wikitext = data . query . pages [ 0 ] . revisions [ 0 ] . slots . main . content ;
73- } ) ;
74- return wikitext ;
75- }
76-
7744 async getUsernames ( ) {
7845 const dataString = await this . getWikitextFromCache ( 'User:NovemBot/userlist.js' ) ;
7946 const dataJSON = JSON . parse ( dataString ) ;
@@ -103,51 +70,65 @@ class UserHighlighterSimple {
10370 } ;
10471 }
10572
106- hasHref ( url ) {
107- return Boolean ( url ) ;
108- }
109-
110- isAnchor ( url ) {
111- return url . charAt ( 0 ) === '#' ;
112- }
113-
114- isHttpOrHttps ( url ) {
115- return url . startsWith ( 'http://' , 0 ) ||
116- url . startsWith ( 'https://' , 0 ) ||
117- url . startsWith ( '/' , 0 ) ;
73+ async getWikitextFromCache ( title ) {
74+ const api = new this . mw . ForeignApi ( 'https://en.wikipedia.org/w/api.php' ) ;
75+ let wikitext = '' ;
76+ await api . get ( {
77+ action : 'query' ,
78+ prop : 'revisions' ,
79+ titles : title ,
80+ rvslots : '*' ,
81+ rvprop : 'content' ,
82+ formatversion : '2' ,
83+ uselang : 'content' , // needed for caching
84+ smaxage : '86400' , // cache for 1 day
85+ maxage : '86400' // cache for 1 day
86+ } ) . then ( ( data ) => {
87+ wikitext = data . query . pages [ 0 ] . revisions [ 0 ] . slots . main . content ;
88+ } ) ;
89+ return wikitext ;
11890 }
11991
120- /**
121- * Figure out the wikipedia article title of the link
122- *
123- * @param {string } url
124- * @param {mw.Uri } urlHelper
125- * @return {string }
126- */
127- getTitle ( url , urlHelper ) {
128- // for links in the format /w/index.php?title=Blah
129- const titleParameterOfUrl = this . mw . util . getParamValue ( 'title' , url ) ;
130- if ( titleParameterOfUrl ) {
131- return titleParameterOfUrl ;
132- }
92+ setHighlightColors ( ) {
93+ // Highest specificity goes on bottom. So if you want an admin+steward to be highlighted steward, place the steward CSS below the admin CSS in this section.
94+ this . addCSS ( 'UHS-override-signature-colors' , `
95+ color: #0645ad !important;
96+ background-color: transparent !important;
97+ background: unset !important;
98+ ` ) ;
13399
134- // for links in the format /wiki/PageName. Slice off the /wiki/ (first 6 characters)
135- if ( urlHelper . path . startsWith ( '/wiki/' ) ) {
136- return decodeURIComponent ( urlHelper . path . slice ( 6 ) ) ;
137- }
100+ this . mw . util . addCSS ( '.UHS-no-permissions { border: 1px solid black !important; }' ) ;
138101
139- return '' ;
102+ // TODO: grab the order from an array, so I can keep checkForPermission and addCSS in the same order easily, lowering the risk of the HTML title="" being one thing, and the color being another
103+ this . addCSS ( 'UHS-500edits-bot-trustedIP' , 'background-color: lightgray !important;' ) ;
104+ this . addCSS ( 'UHS-10000edits' , 'background-color: #9c9 !important;' ) ;
105+ this . addCSS ( 'UHS-new-page-reviewer' , 'background-color: #99f !important;' ) ;
106+ this . addCSS ( 'UHS-former-administrator' , 'background-color: #D3AC8B !important;' ) ;
107+ this . addCSS ( 'UHS-administrator' , 'background-color: #9ff !important;' ) ;
108+ this . addCSS ( 'UHS-bureaucrat' , 'background-color: orange !important; color: #0645ad !important;' ) ;
109+ this . addCSS ( 'UHS-arbitration-committee' , 'background-color: #FF3F3F !important; color: white !important;' ) ;
110+ this . addCSS ( 'UHS-steward' , 'background-color: #00FF00 !important;' ) ;
111+ this . addCSS ( 'UHS-wmf' , 'background-color: hotpink !important; color: #0645ad !important;' ) ;
140112 }
141113
142- notInUserOrUserTalkNamespace ( ) {
143- const namespace = this . titleHelper . getNamespaceId ( ) ;
144- const notInSpecialUserOrUserTalkNamespace = this . $ . inArray ( namespace , [ 2 , 3 ] ) === - 1 ;
145- return notInSpecialUserOrUserTalkNamespace ;
114+ addCSS ( htmlClass , cssDeclaration ) {
115+ // .plainlinks is for Wikipedia Signpost articles
116+ // To support additional custom signature edge cases, add to the selectors here.
117+ this . mw . util . addCSS ( `
118+ .plainlinks .${ htmlClass } .external,
119+ .${ htmlClass } ,
120+ .${ htmlClass } b,
121+ .${ htmlClass } big,
122+ .${ htmlClass } font,
123+ .${ htmlClass } kbd,
124+ .${ htmlClass } small,
125+ .${ htmlClass } span {
126+ ${ cssDeclaration }
127+ }
128+ ` ) ;
146129 }
147130
148- linksToAUser ( ) {
149- let url = this . $link . attr ( 'href' ) ;
150-
131+ linksToAUser ( url ) {
151132 if ( ! this . hasHref ( url ) || this . isAnchor ( url ) || ! this . isHttpOrHttps ( url ) ) {
152133 return false ;
153134 }
@@ -199,11 +180,18 @@ class UserHighlighterSimple {
199180 return true ;
200181 }
201182
202- /**
203- * @copyright Brandon Frohbieter, CC BY-SA 4.0, https://stackoverflow.com/a/4009771/3480193
204- */
205- countInstances ( string , word ) {
206- return string . split ( word ) . length - 1 ;
183+ hasHref ( url ) {
184+ return Boolean ( url ) ;
185+ }
186+
187+ isAnchor ( url ) {
188+ return url . charAt ( 0 ) === '#' ;
189+ }
190+
191+ isHttpOrHttps ( url ) {
192+ return url . startsWith ( 'http://' , 0 ) ||
193+ url . startsWith ( 'https://' , 0 ) ||
194+ url . startsWith ( '/' , 0 ) ;
207195 }
208196
209197 /**
@@ -220,28 +208,39 @@ class UserHighlighterSimple {
220208 }
221209
222210 /**
211+ * Figure out the wikipedia article title of the link
212+ *
213+ * @param {string } url
214+ * @param {mw.Uri } urlHelper
223215 * @return {string }
224216 */
225- getUserName ( ) {
226- const user = this . titleHelper . getMain ( ) . replace ( / _ / g, ' ' ) ;
227- return user ;
228- }
217+ getTitle ( url , urlHelper ) {
218+ // for links in the format /w/index.php?title=Blah
219+ const titleParameterOfUrl = this . mw . util . getParamValue ( 'title' , url ) ;
220+ if ( titleParameterOfUrl ) {
221+ return titleParameterOfUrl ;
222+ }
229223
230- checkForPermission ( listOfUsernames , className , descriptionForHover ) {
231- if ( listOfUsernames [ this . user ] === 1 ) {
232- this . addClassAndHoverText ( className , descriptionForHover ) ;
224+ // for links in the format /wiki/PageName. Slice off the /wiki/ (first 6 characters)
225+ if ( urlHelper . path . startsWith ( '/wiki/' ) ) {
226+ return decodeURIComponent ( urlHelper . path . slice ( 6 ) ) ;
233227 }
234- }
235228
236- addClassAndHoverText ( className , descriptionForHover ) {
237- this . $link . addClass ( className ) ;
229+ return '' ;
230+ }
238231
239- const title = this . $link . attr ( 'title' ) ;
240- if ( ! title || title . startsWith ( 'User:' ) ) {
241- this . $link . attr ( 'title' , descriptionForHover ) ;
242- }
232+ notInUserOrUserTalkNamespace ( ) {
233+ const namespace = this . titleHelper . getNamespaceId ( ) ;
234+ const notInSpecialUserOrUserTalkNamespace = this . $ . inArray ( namespace , [ 2 , 3 ] ) === - 1 ;
235+ return notInSpecialUserOrUserTalkNamespace ;
236+ }
243237
244- this . hasAdvancedPermissions = true ;
238+ /**
239+ * @return {string }
240+ */
241+ getUserName ( ) {
242+ const user = this . titleHelper . getMain ( ) . replace ( / _ / g, ' ' ) ;
243+ return user ;
245244 }
246245
247246 addClassesAndHoverTextToLinkIfNeeded ( ) {
@@ -271,26 +270,21 @@ class UserHighlighterSimple {
271270 }
272271 }
273272
274- setHighlightColors ( ) {
275- // Highest specificity goes on bottom. So if you want an admin+steward to be highlighted steward, place the steward CSS below the admin CSS in this section.
276- this . addCSS ( 'UHS-override-signature-colors' , `
277- color: #0645ad !important;
278- background-color: transparent !important;
279- background: unset !important;
280- ` ) ;
273+ checkForPermission ( listOfUsernames , className , descriptionForHover ) {
274+ if ( listOfUsernames [ this . user ] === 1 ) {
275+ this . addClassAndHoverText ( className , descriptionForHover ) ;
276+ }
277+ }
281278
282- this . mw . util . addCSS ( '.UHS-no-permissions { border: 1px solid black !important; }' ) ;
279+ addClassAndHoverText ( className , descriptionForHover ) {
280+ this . $link . addClass ( className ) ;
283281
284- // TODO: grab the order from an array, so I can keep checkForPermission and addCSS in the same order easily, lowering the risk of the HTML title="" being one thing, and the color being another
285- this . addCSS ( 'UHS-500edits-bot-trustedIP' , 'background-color: lightgray !important;' ) ;
286- this . addCSS ( 'UHS-10000edits' , 'background-color: #9c9 !important;' ) ;
287- this . addCSS ( 'UHS-new-page-reviewer' , 'background-color: #99f !important;' ) ;
288- this . addCSS ( 'UHS-former-administrator' , 'background-color: #D3AC8B !important;' ) ;
289- this . addCSS ( 'UHS-administrator' , 'background-color: #9ff !important;' ) ;
290- this . addCSS ( 'UHS-bureaucrat' , 'background-color: orange !important; color: #0645ad !important;' ) ;
291- this . addCSS ( 'UHS-arbitration-committee' , 'background-color: #FF3F3F !important; color: white !important;' ) ;
292- this . addCSS ( 'UHS-steward' , 'background-color: #00FF00 !important;' ) ;
293- this . addCSS ( 'UHS-wmf' , 'background-color: hotpink !important; color: #0645ad !important;' ) ;
282+ const title = this . $link . attr ( 'title' ) ;
283+ if ( ! title || title . startsWith ( 'User:' ) ) {
284+ this . $link . attr ( 'title' , descriptionForHover ) ;
285+ }
286+
287+ this . hasAdvancedPermissions = true ;
294288 }
295289}
296290
0 commit comments