User:Evesiesta/WhoIsActive.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$( function () {
const filteredLinks = [];
const localizedUserNamespace = mw.config.get( 'wgFormattedNamespaces' )[2];
$( '.mw-body-content' ).
find( 'a[title^="User:"], a[title^="' + localizedUserNamespace + ':"]' ).
each( function() {
const link = $( this );
const href = decodeURI( link.attr( 'href' ) );
const userRegex = new RegExp(
'((User)|(' + localizedUserNamespace + ')):(.*?)(?=&|$)'
);
const username = href.match(userRegex);
const index = username[0].indexOf( '/' );
if ( username[0].indexOf( '/' ) === -1 ) {
filteredLinks.push( {
username: username[0],
element: link
} );
}
} );
if ( !filteredLinks.length ) {
return;
}
const RECENT = 'recent';
const THISYEAR = 'thisyear';
const OVERAYEAR = 'overayear';
const messages = {
en: {
recent: 'Edited recently',
thisyear: 'Edited this year',
overayear: 'Edited over a year ago'
},
de: {
recent: 'kürzlich bearbeitet',
thisyear: 'in diesem Jahr bearbeitet',
overayear: 'vor über einem Jahr bearbeitet'
},
cs: {
recent: 'editoval nedávno',
thisyear: 'editoval tento rok',
overayear: 'editoval před rokem'
},
zh-cn: {
recent: '最近编辑过',
thisyear: '今年编辑过',
overayear: '去年编辑过'
},
zh-hans: {
recent: '最近编辑过',
thisyear: '一年内编辑过',
overayear: '一年前编辑过'
},
zh-hant: {
recent: '最近編輯過',
thisyear: '一年內編輯過',
overayear: '一年前編輯過'
},
zh-tw: {
recent: '最近編輯過',
thisyear: '一年內編輯過',
overayear: '一年前編輯過'
}
};
const localizedMessages = function () {
const lang = mw.config.get( 'wgUserLanguage' );
if ( lang in messages ) {
return messages[lang];
}
return messages.en;
}();
const getLastActiveMarker = function ( timestamp ) {
const date = Date.parse( timestamp );
const now = Date.now();
const diff = Math.floor( ( now - date ) / ( 1000 * 60 * 60 * 24 ) );
var timespan = RECENT;
if ( diff > 365 ) {
timespan = OVERAYEAR;
} else if ( diff > 30 ) {
timespan = THISYEAR;
}
const iconPath = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) +
'/resources/lib/ooui/themes/wikimediaui/images/icons/userContributions-ltr.svg';
const marker =
"<span class='mw-whoisactivegadget-span mw-whoisactivegadget-" + timespan + "'>" +
"<img src='" + iconPath + "' class='mw-whoisactivegadget-filter-" + timespan + "'/> " +
localizedMessages[timespan] + "<span>";
return $( marker );
};
mw.loader.using( [ 'mediawiki.api' ], function () {
filteredLinks.forEach( function( item ) {
const username = item.username;
const element = item.element;
const api = new mw.Api();
api.get( {
format: 'json',
action: 'query',
list: 'usercontribs',
uclimit: '1',
ucuser: username
} ).then( function ( result ) {
if ( result.query.usercontribs.length ) {
const timestamp = result.query.usercontribs[0].timestamp;
getLastActiveMarker( timestamp ).insertAfter( element );
}
} );
} );
} );
} );