-
Notifications
You must be signed in to change notification settings - Fork 0
/
userbadge.js
138 lines (105 loc) · 3.88 KB
/
userbadge.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
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
class UserBadges {
constructor() {
this._userEndpoint = 'https://psychonautwiki.global.ssl.fastly.net/w/api.php?action=query&list=allusers&auprop=groups&augroup=bureaucrat|sysop|scholar|donor&aulimit=100&format=json';
this._userMapping = {
/*userName: bureaucrat|sysop|scholar*/
};
this._levelDisplayText = {
medium: 'System Operator',
admin: 'Administrator',
sentinel: 'Moderator',
scholar: 'Scholar',
donor: 'Donor'
};
this._retrieveUsers();
}
_ingestError (err) {
if ('Raven' in window && 'captureException' in window['Raven']) {
/* send to sentry */
window['Raven']['captureException'](err);
}
}
_retrieveUsers () {
try {
const xhr = new XMLHttpRequest();
const self = this;
xhr.addEventListener('load', function () {
try {
self._processUsers(JSON.parse(this.responseText));
} catch(e) {
self._ingestError(e);
}
});
xhr.open('GET', this._userEndpoint);
xhr.send();
} catch(e) {
this._ingestError(e);
}
}
_getGroupFromUser(user) {
if (user['groups'].indexOf('medium') !== -1) {
return 'medium';
}
if (user['groups'].indexOf('admin') !== -1) {
return 'admin';
}
if (user['groups'].indexOf('sentinel') !== -1) {
return 'sentinel';
}
if (user['groups'].indexOf('scholar') !== -1) {
return 'scholar';
}
if (user['groups'].indexOf('donor') !== -1) {
return 'donor';
}
}
_processUsers(userData) {
const users = userData['query']['allusers'];
users.forEach(user => {
this._userMapping[user['name']] = this._getGroupFromUser(user);
});
this._rewriteUserLinks();
this._addUserLevelToProfile();
}
_addUserLevelToProfile() {
const pageName = window['mw']['config']['get']('wgPageName');
const userName = pageName && /^User:(.*?)$/.exec(pageName);
if (!userName || !userName[1]) {
return;
}
const profileUserLevel = this._userMapping[userName[1]];
const levelDisplayText = this._levelDisplayText[profileUserLevel];
const actionsContainer = document.getElementsByClassName('profile-actions')[0];
if (profileUserLevel && levelDisplayText && actionsContainer) {
const prevActions = actionsContainer.innerHTML;
const profileLevelClass = `profile-special profile-${profileUserLevel}`;
actionsContainer.innerHTML =
`<div class='${profileLevelClass}'>
${levelDisplayText}
</div> | ${prevActions}`;
}
}
_rewriteUserLinks() {
Array.prototype.slice.call(document.getElementsByTagName('a')).forEach(item =>
this._doUserRewriteForElement(item)
);
}
_itemAddClassIfNeeded(item, newClass) {
if (item.className.indexOf(newClass) === -1) {
item.className = [item.className, newClass].join(' ');
}
}
_doUserRewriteForElement(item){
// Is the first child an image? bail.
if (item['firstChild'] instanceof (window['HTMLImageElement'] || {})) {
return false;
}
const userMatch = /User\:([a-zA-Z0-9\_]+)?$/.exec(item.href);
const assumedUser = userMatch ? (userMatch[1] || '').replace(/\_/, ' ') : null;
if (!this._userMapping[assumedUser]) return false;
this._itemAddClassIfNeeded(item, 'mw-userlink');
const userClass = `mw-ug-${this._userMapping[assumedUser]}`;
this._itemAddClassIfNeeded(item, userClass);
}
};
new UserBadges();