Ir al contenido

Diferencia entre revisiones de «Usuario:Leoncastro/Taller/MediaWiki:Gadget-DeluxeHistory.js»

De Wikipedia, la enciclopedia libre
Contenido eliminado Contenido añadido
Soporte para transclusión de listas de seguimiento
Add interface-admin user group and Dark Mode styles
Línea 60: Línea 60:
'abusefilter', 'accountcreator', 'autopatrolled', 'bot', 'bureaucrat', 'checkuser', 'confirmed',
'abusefilter', 'accountcreator', 'autopatrolled', 'bot', 'bureaucrat', 'checkuser', 'confirmed',
'epcampus', 'epcoordinator', 'epinstructor', 'eponline', 'extendedmover', 'filemover',
'epcampus', 'epcoordinator', 'epinstructor', 'eponline', 'extendedmover', 'filemover',
'flood', 'flow-bot', 'founder', 'import', 'ipblock-exempt', 'massmessage-sender', 'suppress',
'flood', 'flow-bot', 'founder', 'import', 'interface-admin', 'ipblock-exempt', 'massmessage-sender',
'patroller', 'researcher', 'reviewer', 'rollbacker', 'sysop', 'templateeditor', 'transwiki'
'patroller', 'researcher', 'reviewer', 'rollbacker', 'suppress', 'sysop', 'templateeditor', 'transwiki'
/* 'autoreviewer': obsolete type replaced by 'autopatrolled' */
/* 'autoreviewer': obsolete type replaced by 'autopatrolled' */
/* 'extendedconfirmed': too big // [[:en:Wikipedia:User_access_levels#Extended_confirmed_users]] (31632 users) */
/* 'extendedconfirmed': too big // [[:en:Wikipedia:User_access_levels#Extended_confirmed_users]] (31632 users) */
Línea 204: Línea 204:
'.mw-userlink.' + cnListUserLink + '-sysop{background-color:#8f8;}\r\n' +
'.mw-userlink.' + cnListUserLink + '-sysop{background-color:#8f8;}\r\n' +
'.mw-userlink.' + cnListUserLink + '-bot{background-color:#8ff;}\r\n' +
'.mw-userlink.' + cnListUserLink + '-bot{background-color:#8ff;}\r\n' +
'@media screen {\r\n' +
' .skin-theme-clientpref-night .mw-userlink{background-color:#220;}\r\n' +
' .skin-theme-clientpref-night .mw-userlink.mw-anonuserlink{background-color:#200;}\r\n' +
' .skin-theme-clientpref-night .mw-userlink[class*="' + cnListUserLink + '-"]{background-color:#012;}\r\n' +
' .skin-theme-clientpref-night .mw-userlink.' + cnListUserLink + '-sysop{background-color:#020;}\r\n' +
' .skin-theme-clientpref-night .mw-userlink.' + cnListUserLink + '-bot{background-color:#022;}\r\n' +
'}\r\n' +
'@media screen and (prefers-color-scheme: dark) {\r\n' +
' .skin-theme-clientpref-os .mw-userlink{background-color:#220;}\r\n' +
' .skin-theme-clientpref-os .mw-userlink.mw-anonuserlink{background-color:#200;}\r\n' +
' .skin-theme-clientpref-os .mw-userlink[class*="' + cnListUserLink + '-"]{background-color:#012;}\r\n' +
' .skin-theme-clientpref-os .mw-userlink.' + cnListUserLink + '-sysop{background-color:#020;}\r\n' +
' .skin-theme-clientpref-os .mw-userlink.' + cnListUserLink + '-bot{background-color:#022;}\r\n' +
'}\r\n' +
window.deluxehistory.css;
window.deluxehistory.css;
mw.util.addCSS(scCSS);
mw.util.addCSS(scCSS);

Revisión del 23:12 18 ago 2024

(function(mw, $){

 var default_config = {
  loaded: false,
  options: ['sysop', 'bot'],
  css: '',
  cache: 7,
  i18n: {
   // message strings
   strMenuText: 'Purgar historial',
   strMenuInfo: 'Purgar la caché del historial',
   strMsgError: 'DeluxeHistory: Error en la recuperación de datos de la API',
  },
 };
 window.deluxehistory = $.extend( default_config, window.deluxehistory );

 // 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', 'interface-admin', 'ipblock-exempt', 'massmessage-sender',
    'patroller', 'researcher', 'reviewer', 'rollbacker', 'suppress', 'sysop', 'templateeditor', 'transwiki'
    /* 'autoreviewer': obsolete type replaced by 'autopatrolled' */
    /* 'extendedconfirmed': too big // [[:en:Wikipedia:User_access_levels#Extended_confirmed_users]] (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();
        var nodes = new DOMParser().parseFromString(data, "text/html").getElementById('content');
        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], cnListUserLink + '-' + 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( window.deluxehistory.i18n.strMsgError );
           });
  }
  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);
      }
      if( user )
      {
       user = user.replaceAll(' ', '_');
       addClassName(li, 'mw-userlink' + '-' + user);
       addClassName(a, 'mw-userlink' + '-' + user);
      }
     } // 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' +
              '@media screen {\r\n' +
              '	.skin-theme-clientpref-night .mw-userlink{background-color:#220;}\r\n' +
              '	.skin-theme-clientpref-night .mw-userlink.mw-anonuserlink{background-color:#200;}\r\n' +
              '	.skin-theme-clientpref-night .mw-userlink[class*="' + cnListUserLink + '-"]{background-color:#012;}\r\n' +
              '	.skin-theme-clientpref-night .mw-userlink.' + cnListUserLink + '-sysop{background-color:#020;}\r\n' +
              '	.skin-theme-clientpref-night .mw-userlink.' + cnListUserLink + '-bot{background-color:#022;}\r\n' +
              '}\r\n' +
              '@media screen and (prefers-color-scheme: dark) {\r\n' +
              '	.skin-theme-clientpref-os .mw-userlink{background-color:#220;}\r\n' +
              '	.skin-theme-clientpref-os .mw-userlink.mw-anonuserlink{background-color:#200;}\r\n' +
              '	.skin-theme-clientpref-os .mw-userlink[class*="' + cnListUserLink + '-"]{background-color:#012;}\r\n' +
              '	.skin-theme-clientpref-os .mw-userlink.' + cnListUserLink + '-sysop{background-color:#020;}\r\n' +
              '	.skin-theme-clientpref-os .mw-userlink.' + cnListUserLink + '-bot{background-color:#022;}\r\n' +
              '}\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,
     window.deluxehistory.i18n.strMenuText,
     'ca-purge-deluxehistory',
     window.deluxehistory.i18n.strMenuInfo
    );
   }
  }
  exec();
 }

 function exec()
 {
  if( mw.config.get('wgAction') == 'history' )
  {
   markHistory('#pagehistory li');
  }
  else if( mw.config.get('wgCanonicalSpecialPageName') == 'Recentchanges' ||
           mw.config.get('wgCanonicalSpecialPageName') == 'Watchlist' ||
           document.querySelector('.mw-changeslist') )
  {
   markHistory('.mw-changeslist > ul.special > li');
  }
  else if( mw.config.get('wgCanonicalSpecialPageName') == 'Log' )
  {
   markHistory('.mw-logevent-loglines li');
  }
  else if( mw.config.get('wgAction') == 'view' && document.querySelector('.diff') )
  {
   markHistory('#mw-diff-otitle2, #mw-diff-ntitle2');
  }
 }

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

})(mediaWiki, jQuery);