-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShowUserGender.js
59 lines (48 loc) · 1.39 KB
/
ShowUserGender.js
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
/** Show User Gender **/
// Show the gender of users
// [[en:w:User:BrandonXLF/ShowUserGender]]
// By [[en:w:User:BrandonXLF]]
window.SHOW_USER_GENDER = $.extend({
male: ' [he]',
female: ' [she]',
unknown: ' [?]',
}, window.SHOW_USER_GENDER);
mw.hook('wikipage.content').add(function(content) {
var USER_NS = mw.config.get('wgFormattedNamespaces')[2] + ':';
function getUser(el) {
return decodeURIComponent(el.href).match(/^.*\/(.*?)$/)[1].replace(/_/g, ' ').match(/(index\.php\?title=|)([^&]*).*?/)[2];
}
var elements = content.find('.userlink').filter(function() {
return getUser(this).startsWith(USER_NS);
}),
users = elements.map(function() {
return getUser(this).substr(USER_NS.length);
}).toArray().filter(function(val, i, a) {
return i === a.indexOf(val);
}),
genders = {},
requests = 0,
done = 0;
for (var i = 0; i < users.length; i += 50) {
var reqUsers = users.slice(i, i + 50);
requests++;
new mw.Api().get({
action: 'query',
list: 'users',
usprop: 'gender',
ususers: reqUsers.join('|')
}).then(function(res) {
done++;
res = res.query.users;
for (var i = 0; i < res.length; i++) {
genders[res[i].name] = res[i].gender;
}
if (requests == done) {
elements.each(function() {
var user = getUser(this).substr(USER_NS.length);
$(this).after(window.SHOW_USER_GENDER[genders[user] || 'unknown']);
});
}
});
}
});