MediaWiki:Gadget-DeluxeHistory.js

(function(mw, $){

var op = window.deluxehistory ? (window.deluxehistory.options ? window.deluxehistory.options : window.deluxehistory) : ['sysop', 'bot']; var cs = window.deluxehistory ? (window.deluxehistory.css ? window.deluxehistory.css : ) : ; var ls = window.deluxehistory ? (window.deluxehistory.cache ? window.deluxehistory.cache : 7) : 7; // 7 days cache window.deluxehistory = { loaded: false, options: typeof op == 'string' ? [op] : op, css: cs, cache: parseInt(ls) };

// message strings var strDeluxeHistoryMenuText = 'Purgar historial'; var strDeluxeHistoryMenuInfo = 'Purgar la caché del historial'; var strDeluxeHistoryMsgError = 'DeluxeHistory: Error en la recuperación de datos de la API';

// localStorage cache strings var lsDateName = 'mw-deluxehistory-userlist-date'; var lsListName = 'mw-deluxehistory-userlist-list'; var lsDelParam = 'deluxehistory'; var lsDelValue = 'nocache'; var lsDelCache = '&' + lsDelParam + '=' + lsDelValue;

// className strings var cnListUserLink = 'mw-listuserlink'; var cnSelfUserLink = 'mw-selfuserlink';

function addClassName(item, name) { item.className = (' ' + item.className + ' ').split(' ' + name + ' ').join(' ').trim + ' ' + name; }

function readCache { var cache = {}; if( localStorage[lsListName] ) {  try {   cache = JSON.parse(localStorage[lsListName]); }  catch(e) {   cache = {}; } }  return cache; } function saveCache(data) { localStorage[lsListName] = JSON.stringify(data); }

function checkUserlist { var opts = window.deluxehistory.options; if( !localStorage[lsDateName] || localStorage[lsDateName] < Date.now ) {  var valid_groups = [ 'abusefilter', 'accountcreator', 'autopatrolled', 'bot', 'bureaucrat', 'checkuser', 'confirmed', 'epcampus', 'epcoordinator', 'epinstructor', 'eponline', 'extendedmover', 'filemover', 'flood', 'flow-bot', 'founder', 'import', 'ipblock-exempt', 'massmessage-sender', 'oversight', 'patroller', 'researcher', 'reviewer', 'rollbacker', 'sysop', 'templateeditor', 'transwiki' /* 'autoreviewer': obsolete type replaced by 'autopatrolled' */ /* 'extendedconfirmed': too big // en:Wikipedia:User_access_levels (31632 users) */ /* 'steward': only listed on m:Template:StewardsList, CORS */ ];  var file = []; opts.forEach(function(group){   if( ~$.inArray(group, valid_groups) )    {     file.push( $.get( mw.util.wikiScript, {title: 'Special:Listusers', group: group, limit: 10000 } ) // en:wiki reviewer (6721 users) (27-jan-2017) .done(function(data){       var list = readCache;        // HOTFIX:        // var html = nodes = $.parseXML(data);        var html = $.parseHTML(data);        Array.prototype.forEach.call(html, function(e){ if(e.id && e.id === 'content') { nodes = e; } });         // HOTFIX.        Array.prototype.forEach.call(nodes.querySelectorAll('#mw-content-text > ul > li'), function(li){ var a = li.querySelector('a.mw-userlink'); var user = a.querySelector('bdi'); user = user.innerText || user.textContent; list[user] = list[user] || {className: ''}; addClassName(list[user], cnListUserLink + '-' + group); });       saveCache(list);       }) );   }    else    {     if( group == 'steward' )     {      /* CORS load from meta.wikimedia.org using MW API */      mw.loader.using( 'mediawiki.ForeignApi' ).done(function{ var api = new mw.ForeignApi( 'https://meta.wikimedia.org/w/api.php' ); api.get({       action: 'query',        prop: 'links',        plnamespace: 2,        pllimit: 500,        titles: 'Template:StewardsList'       }).done(function(data){        var list = readCache;        var stub = function(link)        {         var user = link.title.replace('User:', );         list[user] = list[user] || {className: };         addClassName(list[user], cnListUserLink + '-' + group);        };        for(var page in data.query.pages)        {         data.query.pages[page].links.forEach(stub);        }        saveCache(list);       }); });    }     if( group == 'mostactive-eswiki' && mw.config.get('wgServerName') == 'es.wikipedia.org' )     {      file.push( $.get( mw.util.wikiScript, {title: 'Wikipedia:Usuarios_activos', action: 'raw' } ) .done(function(data){        var list = readCache;         data.replace(/\/([^\|]+)\|\{\{formatnum:(\d+)\}\}\]\]$/gm, function($0, $1, $2){ var user = $1; if(parseInt($2) >= 70) {          list[user] = list[user] || {className: ''}; addClassName(list[user], 'mw-userlink-' + group); }         return $0; });        saveCache(list);        }) );    }    }   });   return $.when.apply($, file) .done(function(d){           localStorage[lsDateName] = Date.now + window.deluxehistory.cache * 24 * 60 * 60 * 1000;           }) .fail(function(e){           mw.notify(strDeluxeHistoryMsgError);           }); } return $.when(true); }

function markHistory(ul) { checkUserlist.done(function{   if(ul)   {    var list = readCache;    Array.prototype.forEach.call(document.querySelectorAll(ul), function(li){ var user, a = li.querySelector('a.mw-userlink, .wikibase-edit ~ .mw-changeslist-separator + a.extiw'); if(a) {     user = a.querySelector('bdi') || a;      user = user.innerText || user.textContent; if( !li.querySelector('a.mw-userlink') ) addClassName(a, 'mw-userlink'); if( /(\d{1,3}\.){3}\d{1,3}|([\da-f]{1,4}:){7}[\da-f]{1,4}/i.test(user) ) addClassName(a, 'mw-anonuserlink'); if( li.querySelector('a.mw-anonuserlink') ) addClassName(li, 'mw-anonuserlink'); if( list[user] && list[user].className ) {      addClassName(li, list[user].className); addClassName(a, list[user].className); }     if( user == mw.config.get('wgUserName') ) {      addClassName(li, cnSelfUserLink); addClassName(a, cnSelfUserLink); }    } // else ignore hidden username });   window.deluxehistory.loaded = true;   }  }); }

mw.loader.using('mediawiki.util').done(function{ if( mw.util.getParamValue(lsDelParam) == lsDelValue )  {   localStorage.removeItem(lsDateName);   localStorage.removeItem(lsListName);   location.href = location.href.replace(lsDelCache, '');  } });

function init { var scCSS = '.mw-userlink{background-color:#ff8;}\r\n' + '.mw-userlink.mw-anonuserlink{background-color:#f88;}\r\n' + '.mw-userlink[class*="' + cnListUserLink + '-"]{background-color:#8cf;}\r\n' + '.mw-userlink.' + cnListUserLink + '-sysop{background-color:#8f8;}\r\n' + '.mw-userlink.' + cnListUserLink + '-bot{background-color:#8ff;}\r\n' + window.deluxehistory.css; mw.util.addCSS(scCSS); if( mw.config.get('wgAction') == 'history' ) {  if( !window.deluxehistory.loaded ) {   mw.util.addPortletLink(     'p-cactions',     location.href + lsDelCache,     strDeluxeHistoryMenuText,     'ca-purge-deluxehistory',     strDeluxeHistoryMenuInfo    ); } }  exec; }

function exec { if( mw.config.get('wgAction') == 'history' ) {  markHistory('#pagehistory > li'); } else if( mw.config.get('wgCanonicalSpecialPageName') == 'Recentchanges' ||           mw.config.get('wgCanonicalSpecialPageName') == 'Watchlist' ) {  markHistory('.mw-changeslist > ul.special > li'); } }

mw.loader.using('mediawiki.util').done(function{ init;  mw.hook('wikipage.content').add( exec ); });

})(mediaWiki, jQuery);