User:HueSatLum/pageInfo.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:HueSatLum/pageInfo. |
( function ( $, mw ) {
var $siteSub = $( '#siteSub' );
var articleId = mw.config.get( 'wgArticleId' );
if ( $siteSub.length === 0 ||
articleId === 0 ||
mw.util.getParamValue( 'printable' ) === 'yes' ||
mw.config.get( 'wgIsMainPage' ) ) {
return;
}
function plural ( number, word ) {
number = Math.round( number );
return number + ' ' + word + ( number === 1 ? '' : 's');
}
function formatDate( timestamp ) {
var then = new Date ( timestamp );
var now = new Date();
var age = now.getTime() - then.getTime();
var conversions = [
[1e3, 'second'],
[60, 'minute'],
[60, 'hour'],
[24, 'day'],
[365/12, 'month'],
[12, 'year']
];
if ( age < 1e3 * 60 ) {
return 'just now';
}
for( var i = 0; i < conversions.length; i++ ) {
age = age / conversions[ i ][ 0 ];
if( conversions[ i + 1 ] === undefined || age < conversions[ i + 1 ][ 0 ] ) {
return plural( age, conversions[ i ][ 1 ] ) + ' ago';
}
}
}
var pageInfo = {};
pageInfo.pageType = $( '[id^="ca-nstab-"].selected' ).text() || 'page';
pageInfo.pageType = pageInfo.pageType.toLowerCase();
if ( mw.config.get( 'wgNamespaceNumber' ) % 2 === 1) {
// All talk namespace numbers are odd
pageInfo.pageType = 'talk page';
}
var api = new mw.Api();
api.get( {
action: 'query',
prop: [ 'info', 'revisions' ],
inprop: [ 'protection', 'watchers' ],
rvprop: [ 'ids', 'timestamp', 'user' ],
rvlimit: 1,
pageids: articleId,
rawcontinue: ''
} ).done( function ( data ) {
var rawData = data.query.pages[ articleId ];
pageInfo.lastEditTimestamp = rawData.revisions[ 0 ].timestamp;
pageInfo.lastEditAgo = formatDate( pageInfo.lastEditTimestamp );
pageInfo.lastEditUser = rawData.revisions[ 0 ].user;
pageInfo.lastEditLink = mw.config.get( 'wgScript' ) +
'?title=' + mw.config.get( 'wgPageName' ) +
'&diff=' + rawData.revisions[ 0 ].revid +
'&oldid=' + rawData.revisions[ 0 ].parentid;
var userLinkPrefix = ( rawData.revisions[ 0 ].anon === undefined ) ? '/wiki/User:' : '/wiki/Special:Contributions/';
pageInfo.lastEditUserLink = userLinkPrefix + rawData.revisions[ 0 ].user;
pageInfo.lastEditTimestamp = new Date(pageInfo.lastEditTimestamp).toUTCString();
pageInfo.watchers = rawData.watchers || '<30';
api.get( {
action: 'query',
prop: 'revisions',
rvprop: [ 'ids', 'timestamp', 'user' ],
rvlimit: 1,
rvdir: 'newer',
pageids: articleId,
rawcontinue: ''
} ).done( function ( data ) {
var rawOldestData = data.query.pages[ articleId ].revisions[ 0 ];
pageInfo.firstEditTimestamp = rawOldestData.timestamp;
pageInfo.firstEditAgo = formatDate( pageInfo.firstEditTimestamp );
pageInfo.firstEditUser = rawOldestData.user;
pageInfo.firstEditLink = mw.config.get( 'wgScript' ) +
'?title=' + mw.config.get( 'wgPageName' ) +
'&oldid=' + rawOldestData.revid;
var userLinkPrefix = ( rawOldestData.anon === undefined ) ? '/wiki/User:' : '/wiki/Special:Contributions/';
pageInfo.firstEditUserLink = userLinkPrefix + rawOldestData.user;
pageInfo.firstEditTimestamp = new Date(pageInfo.firstEditTimestamp).toUTCString();
} ).fail( function ( error ) {
throw error;
} ).always( addInfo );
} ).fail( function ( error ) {
throw error;
} );
function addInfo () {
var infoTemplate = ' This ${pageType} was created <a href="${firstEditLink}" title="${firstEditTimestamp}" class="nopopups">${firstEditAgo}</a> ' +
'by <a href="${firstEditUserLink}">${firstEditUser}</a> and last edited ' +
'<a href="${lastEditLink}" title="${lastEditTimestamp}" class="nopopups">${lastEditAgo}</a> by ' +
'<a href="${lastEditUserLink}">${lastEditUser}</a>, with ${watchers} watchers.';
if ( $siteSub.text().slice( -1 ) !== '.' ) {
// Add a period after "From Wikipedia, the free encyclopedia"
// if there's not already one there from another script
$siteSub.append( '.' );
}
infoTemplate = infoTemplate.replace( /\${(\w+)}/g, function ( _match, key ) {
return pageInfo[key];
} );
$siteSub.append( infoTemplate );
}
} ) ( jQuery, mediaWiki );