Jump to content

User:Ronhjones/strikeblocked.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function markBlocked() {
  mw.util.addCSS('\
.user-blocked-temp{'   + (window.mbTempStyle||'opacity: 0.7; text-decoration: line-through') + '}\
.user-blocked-indef{'  + (window.mbIndefStyle||'opacity: 0.4; font-style: italic; text-decoration: line-through') + '}\
.user-blocked-tipbox{' + (window.mbTipBoxStyle||'font-size:smaller; background:#FFFFF0; border:1px solid #FEA; padding:0 0.3em; color:#AAA') + '}\
')

 var mbTooltip = 
   window.mbTooltip
   || {
     dsb: '(blokěrowany wót $2 za traśe wót $1: $3)',
     de: '(von $2 für die Zeitdauer $1 gesperrt: $3)',
     eo: '(forbarita de $2 por daŭro de $1: $3)',
     hsb: '(zablokowany wot $2 za traće wot $1: $3)',
     ja: '($2によるブロック。期限:$1 理由:$3)',
     ko: '(차단: $2, 기한: $1, 이유: $3)',
     pt: '(bloqueado por $2 até $1: $3)',
     ru: '(блокирован $2 на срок $1: $3)',
     sv: '(har blockerats av $2 till $1: $3)',
     uk: '(блокування $2 на термін $1: $3)'
   } [mw.config.get('wgUserLanguage')]
   || '(blocked by $2 with an expiry time of $1: $3)' //en 
  

 //get local alias for 'Contributions' from "my contribs" link on top or user-defined
 var mwCont = /:([^\/]+)\//.exec( $('#pt-mycontris').find('a').attr('href') )
 if( mwCont ) mwCont = mwCont[1]; else mwCont = window.mbLocalContribsName || 'Contributions'
  
 //RegExp  for all links thast are  User: |  User_talk: |  Special:Contributions/ 
 var userTitleRX = new RegExp('^'
  + '(' 
   + '(' + mw.config.get('wgFormattedNamespaces')[2] + '|' + mw.config.get('wgFormattedNamespaces')[3] + '):'
   + '|' 
   + mw.config.get('wgFormattedNamespaces')[-1] + ':' + mwCont + '\\/'
  + ')'
  + '([^\\/#]*)$')   
 
 //RegExp for links
 var articleRX = new RegExp( '^' + mw.config.get('wgArticlePath').replace('$1', '') + '([^#]+)' )
 var scriptRX =  new RegExp( '^' + mw.config.get('wgScript') + '\\?title=([^#&]+)' )

 var url, ma, pgTitle
 var userLinks = {}

 //find all "user" links and save them in userLinks : { 'users': [<link1>, <link2>, ...], 'user2': [<link3>, <link3>, ...], ... }
 mw.util.$content
 .find('a')
 .each(function(i, lnk){
    url = $(lnk).attr('href')
    if( url.charAt(0) != '/' ) return
    else if( ma = articleRX.exec(url) ) pgTitle = ma[1]
    else if( ma =  scriptRX.exec(url) ) pgTitle = ma[1]
    else return
    pgTitle = decodeURIComponent(pgTitle).replace(/_/g, ' ')
    user = userTitleRX.exec(pgTitle)
    if( !user ) return
    user = user[3]
    $(lnk).addClass('userlink')
    if ( !userLinks[user] ) userLinks[user] = []
    userLinks[user].push(lnk)
})

 //convert users into array
 var users = []
 for (var u in userLinks) users.push(u)
 if( users.length == 0 ) return

 //do API request
 var waitingCSS = mw.util.addCSS('a.userlink {opacity:' + (window.mbLoadingOpacity||0.85) + '}')
 $.ajax({
   url: '/w/api.php?format=json&action=query&list=blocks',
   dataType: 'json',
   type: 'POST', //too many users will exceed GET URL length
   data: { bklimit: 500, bkusers: users.join('|'), bkprop: 'user|by|timestamp|expiry|reason' }, //no need for 'id|flags'
   success: markLinks
 })

 //receive data and mark links
 function  markLinks(resp){
   waitingCSS.disabled = true
   var list, blk, tip, links
   if( !resp || !(list=resp.query) || !(list=list.blocks) ) return
   for( var i=0; i<list.length; i++){
     blk = list[i]
     tip = mbTooltip
           .replace('$1',blk.expiry.replace(/(.*)T(.*)Z/, '$1 $2 UTC'))
           .replace('$2', blk.by)
           .replace('$3', blk.reason)
     clss = 'user-blocked-' + ( /^in/.test(blk.expiry) ? 'indef' : 'temp' )
     links = userLinks[blk.user]
     for (var k=0; k<links.length; k++)
        $(links[k]).addClass(clss).attr( 'title',  $(links[k]).attr('title') + ' ' + tip )
   }
   if( resp.warnings ) jsMsg( 'markBlocked: API <span class=error>warning</span>: ' + resp.warnings.blocks['*'])
 }

 $('#ca-showblocks').parent().remove() // remove added portlet link
  
}

switch( mw.config.get('wgAction') ){
case 'edit':
case 'submit':
  break
case 'view':
  if( mw.config.get('wgNamespaceNumber') % 2 == 0 && mw.config.get('wgNamespaceNumber') != 4 ) break
  //otherwise continue with default
default: //  'history', 'purge'
 $(function(){
    if( window.mbNoAutoStart )
      addPortletLink('p-cactions', 'javascript:markBlocked()', 'XX', 'ca-showblocks')
    else
      setTimeout(markBlocked, 200)
 })
}