跳转到内容

User:PhiLiP/advancedSiteNoticesTest.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$( function() {
  var $ = window.jQuery;
  var ln = $( '#localNotice' );
  if( !ln.length ) {
    return;
  }
  var ct = $( '<div id="advancedSiteNotices"/>' ).appendTo( ln );
  var nts = null;
  
  var getAttrs = function( nt ) {
    var only = {
      logged: nt.hasClass( 'only_logged' ),
      anon: nt.hasClass( 'only_anon' ),
      zh: nt.hasClass( 'only_zh' ),
    };
    only['usr'] = only['logged'] || only['anon'];
    only['vrt'] = only['zh'];
    var vrt = ['hans', 'hant', 'cn', 'hk', 'sg', 'tw']
    for ( var i = 0; i < 6; i ++ ) {
      v = 'zh-' + vrt[i];
      only[v] = nt.hasClass( 'only_zh_' + vrt[i] );
      only['vrt'] = only['vrt'] || only[v];
    }
    return only;    
  };
  
  var loadNotices = function( pos ) {
    var l = nts.length;
    var nt = null;
    var rt = 0;
    while ( !nt || nt.attr( 'class' ) ) {
      if ( nt ) {
        var only = getAttrs( nt );
        if ( ( !only['vrt'] || ( only['vrt'] && only[wgUserVariant] ) ) &&
             ( !only['usr'] || ( wgUserName && only['logged'] ) ||
             ( !wgUserName && only['anon'] ) ) ) {
          break;
        }
      }
      pos = pos % l;
      nt = $( nts[pos ++] );
      rt ++;
      if ( rt == l ) {
        return;
      }
    }
    nt = nt.html();
    if ( ct.html() ) {
      ct.stop().fadeOut( function() {
          ct.html( nt ).fadeIn();
        }
      );
    } else {
      ct.html( nt ).fadeIn();
    }
    setTimeout( function() { loadNotices( pos ) }, 15000 );
  };
  $.ajax( {
    type: 'GET',
    url: wgScriptPath + '/index.php',
    data: { title: 'Template:AdvancedSiteNotices',
      variant: wgUserVariant },
    dateType: 'html',
    beforeSend: function( xhr ) {
      var c = xhr.channel;
      xhr.setRequestHeader( 'Cookie', '' );
    },
    success: function( d ) {
      nts = $( 'ul.sitents li', d );
      var l = nts.length
      loadNotices( Math.ceil( Math.random() * l ) );
    } } );
} );