Skip to content

Commit f449d03

Browse files
committed
UserScriptUserCount: create
1 parent cafcc57 commit f449d03

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

UserScriptUserCount.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// <nowiki>
2+
3+
/*
4+
Does 2 things:
5+
- When visiting a user script page, displays the # of users who have installed the script
6+
- When visiting https://en.wikipedia.org/wiki/User:Novem_Linguae/Templates/Scripts, displays the # of users who have installed each script listed there (helps Novem Linguae update the list)
7+
*/
8+
9+
class UserScriptUserCount {
10+
constructor( mw, $ ) {
11+
this.mw = mw;
12+
// eslint-disable-next-line no-jquery/variable-pattern
13+
this.$ = $;
14+
}
15+
16+
async execute() {
17+
await this.doTaskPlaceCountOnSingleUserScriptPage();
18+
await this.doTaskPlaceCountsOnNovemsUserScriptList();
19+
}
20+
21+
async doTaskPlaceCountOnSingleUserScriptPage() {
22+
const isUserspace = this.mw.config.get( 'wgNamespaceNumber' ) === 2;
23+
const title = this.mw.config.get( 'wgTitle' );
24+
const titleEndsWithJs = title.endsWith( '.js' );
25+
if ( !isUserspace || !titleEndsWithJs ) {
26+
return;
27+
}
28+
29+
const count = await this.getUserCount( title );
30+
this.displayCount( count, title );
31+
}
32+
33+
async doTaskPlaceCountsOnNovemsUserScriptList() {
34+
const isUserspace = this.mw.config.get( 'wgNamespaceNumber' ) === 2;
35+
const title = this.mw.config.get( 'wgTitle' );
36+
if ( !isUserspace || title !== 'Novem Linguae/Templates/Scripts' ) {
37+
return;
38+
}
39+
40+
const $scriptLinks = this.$( 'a[href^="/wiki/User:Novem_Linguae/Scripts/"]' );
41+
for ( let i = 0; i < $scriptLinks.length; i++ ) {
42+
const $link = this.$( $scriptLinks[ i ] );
43+
const href = $link.attr( 'href' );
44+
const scriptTitle = decodeURIComponent( href.replace( '/wiki/', '' ) );
45+
const count = await this.getUserCount( scriptTitle );
46+
const countHtml = ` <span style="background-color: lightgray; padding: 2px 4px; border-radius: 4px; font-size: 90%; font-weight: normal !important;">${ count } ${ count === 1 ? 'user' : 'users' }</span>`;
47+
$link.after( countHtml );
48+
}
49+
}
50+
51+
async getUserCount( title ) {
52+
// remove .js from the end of the title
53+
title = title.replace( /\.js$/, '' );
54+
const api = new this.mw.Api();
55+
const response = await api.get( {
56+
action: 'query',
57+
format: 'json',
58+
list: 'search',
59+
formatversion: '2',
60+
srsearch: `"${ title }" intitle:"common.js"`,
61+
srnamespace: '2' // Userspace
62+
} );
63+
const count = response.query.searchinfo.totalhits;
64+
return count;
65+
}
66+
67+
displayCount( count, title ) {
68+
// remove .js from the end of the title
69+
title = title.replace( /\.js$/, '' );
70+
title = encodeURIComponent( title.replace( /_/g, ' ' ) );
71+
const html = `<b>${ count }</b> ${ count === 1 ? 'user has' : 'users have' } installed this script. <a href="https://en.wikipedia.org/w/index.php?search=%22${ title }%22+intitle%3A%22common.js%22&title=Special%3ASearch&profile=advanced&fulltext=1&ns2=1&limit=1000">See whom.</a>`;
72+
this.$( '#contentSub' ).before( `<div class="UserScriptUserCount" style="background-color: lightgray; margin-bottom: 5px;">${ html }</div>` );
73+
}
74+
}
75+
76+
$( async () => {
77+
await mw.loader.using( [ 'mediawiki.api' ], async () => {
78+
await ( new UserScriptUserCount( mw, $ ) ).execute();
79+
} );
80+
} );
81+
82+
// </nowiki>

0 commit comments

Comments
 (0)