User:Pastelerohelper92/UserInfo.js
ShoutWiki — express yourself and be heard!
Jump to navigation
Jump to search
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/**
* Display user info on any User: page
* and Special:Contributions
*
* Version 1.0: 10 Jul 2016
* Original version for ShoutWiki use
* Version 1.1: 11 Jul 2016
* Move info text into heading
* Version 1.2: 6 Aug 2016
* Move info text into pop-up (#firstHeading)
* Only support MediaWiki-supplied skins
* Add Special:Contributions
*/
(function (mw, $) {
'use strict';
var
urlAPI = mw.config.get('wgScriptPath') + '/api.php',
wgCanonicalSpecialPageName = mw.config.get('wgCanonicalSpecialPageName'),
wgNamespaceNumber = mw.config.get('wgNamespaceNumber'),
wgPageName = mw.config.get('wgPageName'),
query = {
format: 'json',
action: 'query',
list: 'users|usercontribs',
usprop: 'blockinfo|editcount|groups|registration',
ususers: '',
uclimit: 1,
ucuser: '',
ucprop: 'timestamp'
},
groups = {
bot: { article: 'A', name: 'bot' },
bureaucrat: { article: 'A', name: 'bureaucrat' },
checkuser: { article: 'A', name: 'checkuser' },
globalbot: { article: 'A', name: 'global bot' },
intern: { article: 'An', name: 'intern' },
rollback: { article: 'A', name: 'rollbacker' },
sharedhelp: { article: 'A', name: 'help page editor' },
staff: { article: 'A', name: 'staff member' },
steward: { article: 'A', name: 'steward' },
sysop: { article: 'An', name: 'administrator' },
},
ipv4 = new RegExp (
'^(?:(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}' +
'(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
),
ipv6 = new RegExp( // MediaWiki always expands ::
'^(?:(?:[1-9a-f][0-9a-f]{0,3}|0):){7}' +
'(?:[1-9a-f][0-9a-f]{0,3}|0)$',
'i'
);
// return a friendly UTC time & date
function utc(iso) {
return new Date(iso).toUTCString()
.replace('GMT', '(UTC)')
.replace(/(\d{4})/, '$1,')
.replace(/ 0/g, ' ')
.replace(/([^,]) /g, '$1\xA0');
}
if ((wgNamespaceNumber === 2) || (wgNamespaceNumber === 3)) {
query.ususers = query.ucuser =
wgPageName.replace(/^.*?:/, '').split('/')[0].replace(/_/g, ' ');
} else if ((wgCanonicalSpecialPageName === 'Contributions') &&
$('#user').attr('checked')) {
query.ususers = query.ucuser =
$('.mw-autocomplete-user').val().replace(/_/g, ' ');
}
if (query.ususers) {
$.post(urlAPI, query, function (data) {
var
u, s, a, t, i;
if ((u = data.query) && (u = u.users) && u.length &&
(u = u[0]) && (u.missing === undefined)) {
if (u.invalid === undefined) {
a = [];
for ( i = 0 ; i < u.groups.length ; ++ i ) {
t = groups[u.groups[i]];
if (t) {
if (a.length) {
a.push(t.name);
} else {
a.push(t.article + ' ' + t.name);
}
}
}
t = a.length;
switch (t) {
case 0:
s = 'A registered user';
break;
case 1:
s = a[0];
break;
case 2:
s = a.join(' and ');
break;
default: // > 2
a[t - 1] = 'and ' + a[t - 1];
s = a.join(', ');
}
s += ' with ' +
u.editcount.toString().replace(/\d{1,3}(?=(\d{3})+$)/g, '$&,') +
' edit' + ((u.editcount === 1) ? '' : 's') +
', registered ' + utc(u.registration);
if (u.blockexpiry) {
s += ', blocked ' + ((u.blockexpiry === 'infinity') ?
'indefinitely' : 'until ' + utc(u.blockexpiry));
}
} else {
if (ipv4.test(u.name)) {
s = 'An anonymous IPv4 user';
} else if (ipv6.test(u.name)) {
s = 'An anonymous IPv6 user';
} else {
s = 'An invalid user';
}
}
if ((u = data.query) && (u = u.usercontribs) && u.length && (u = u[0])) {
s += ', last edited ' + utc(u.timestamp);
}
s += '.';
} else {
s = 'Not a registered user.';
}
$(function () {
$('#firstHeading').attr('title', s);
});
});
}
}(mediaWiki, jQuery));