Jump to content
Wikimedia Meta-Wiki

User:CMelo (WMF)/common.js

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by CMelo (WMF) (talk | contribs) at 12:05, 11 May 2022. It may differ significantly from the current version .

Note: After publishing, 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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
 /* jshint esversion: 8 */
 mw.loader.using(['moment'],async(require)=>{
 constpage=mw.config.get('wgPageName');

 if(page!=='Community_Wishlist_Survey_2022/Bots_and_gadgets/Gadget:_Who_is_active'){
 return;
 }

 constcontainer=document.getElementById('mw-content-text');

 constlinks=Array.from(container.getElementsByTagName('a'));

 // Turns the array of link elements to filtered object of user page => link element
 constuserLinks=links.reduce((users,link)=>{
 consthref=link.getAttribute('href');

 // Extracts from `User:` up to `?`, `\`, or end of string
 constuserRegex=/User:(.*?)(?=&|\/|$)/g;
 constuser=href.match(userRegex);
 if(user!=null){
 users[user[0]]=link;
 }
 returnusers;
 },{});

 console.log(userLinks);

 constmoment=require('moment');

 // Gets last edit and asynchronously injects timestamp next to username on page
 for(const[userPage,userLink]ofObject.entries(userLinks)){
 getUser(userPage)
 .then(timestamp=>{
 consttoInsert=document.createElement('time');
 constlastActive=moment(timestamp).fromNow();
 toInsert.innerHTML=` (Last active ${lastActive})`;
 toInsert.setAttribute('datetime',timestamp);
 userLink.insertAdjacentElement('beforeend',toInsert)
 },err=>{
 console.log(err)
 });
 }

 asyncfunctiongetUser(user){
 // const api = new mw.Api();
 // await api.get( {
 // action: 'query',
 // list: 'usercontribs',
 // uclimit: 1,
 // ucuser: user,
 // ucprop: 'timestamp'
 // } ).then( function ( data ) {
 // console.log( data );
 // } );
 constres=awaitfetch(
 `https://meta.wikimedia.org/w/api.php?action=query&format=json&list=usercontribs&uclimit=1&ucuser=${user}&ucprop=ids%7Ctitle%7Ctimestamp`
 );
 constresJson=awaitres.json();
 returnresJson.query.usercontribs[0]?.timestamp??null;
 }
 })

AltStyle によって変換されたページ (->オリジナル) /