User:Yair rand/superwatchlist.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// Interwiki watchlist
// Enable by selecting "Enable interwiki watchlists." in [[WT:PREFS]].
mw.config.get('wgPageName') == "Special:Watchlist" && 'localStorage' in window && jQuery(document).ready(function(){
  var v=document.getElementById("mw-watchlist-options");
  v = v.parentNode.insertBefore(newNode('div'), v.nextSibling)
  
  for(var k = localStorage['importedwatchlists'+mw.config.get('wgUserName')]?localStorage['importedwatchlists'+mw.config.get('wgUserName')].split("|"):[], ii=0; ii < k.length; ii++){
    function st(rt){
      var tkn = localStorage['importedwatchlist'+mw.config.get('wgUserName')+rt+'token'], project = "http://"+rt+".org/";
      JsMwApi(project + "w/api.php")({action:'query',list:'watchlist',wlowner:mw.config.get('wgUserName'),wltoken:tkn,
       wlexcludeuser:mw.config.get('wgUserName'),wlprop:'title|flags|user|parsedcomment|timestamp|ids'},function(r){t=r
        var b=newNode('ul',{'style':'display:'+
          (localStorage['importedwatchlist'+mw.config.get('wgUserName')+rt+'hidden']?'none;':'block;')
          });
        v.parentNode.insertBefore(b, v.nextSibling);
        v.parentNode.insertBefore(newNode('h3',project+" watchlist", newNode('span',{'style':'font-size:12px;'},' [',newNode('a','Remove',{style:'cursor:pointer;',click:function(){
          localStorage.removeItem('importedwatchlist'+mw.config.get('wgUserName')+rt+'token');
          localStorage.removeItem('importedwatchlist'+mw.config.get('wgUserName')+rt+'hidden');
          localStorage['importedwatchlists'+mw.config.get('wgUserName')] = localStorage['importedwatchlists'+mw.config.get('wgUserName')].replace(rt,'').replace(/(^\||\|\||\|$)/,'');
          location.reload()
          }}),'] [',newNode('a',localStorage['importedwatchlist'+mw.config.get('wgUserName')+rt+'hidden']?'Expand ▼':'Collapse ▲',{'style':'cursor:pointer;','click':function(){
            if(b.style.display=='none'){
              b.style.display='block';
              this.innerHTML="Collapse ▲"
              localStorage['importedwatchlist'+mw.config.get('wgUserName')+rt+'hidden'] = ""
              }
            else{
              b.style.display='none';
              this.innerHTML="Expand ▼"
              localStorage['importedwatchlist'+mw.config.get('wgUserName')+rt+'hidden'] = true
              }
            }}),']')), v.nextSibling);
        var g=r.query.watchlist;
        for(var i = 0; i < g.length; i++){
          var zx = newNode('span',{class:'comment'}); zx.innerHTML = ' ('+g[i].parsedcomment.replace(/\ href\=\"\//g,' href="'+project)+')';
          b.appendChild(newNode('li','(',
            newNode('a','diff',{href:project+"w/index.php?title="+g[i].title+"&curid="+g[i].pageid+"&diff="+g[i].revid}),
            ' | ',
            newNode('a','hist',{href:project+"w/index.php?title="+g[i].title+"&curid="+g[i].pageid+"&action=history"}),
            ') . . ',
            newNode('a',g[i].title,{href:project+"wiki/"+g[i].title}),
            '; '+g[i].timestamp.match(/\d\d\:\d\d/)[0]+' . . ',
            g[i].user,
            zx
            ))
          }
        })
      }
    st(k[ii])
    }
  var qw, er=['Wikipedia','Wiktionary','Wikibooks','Wikisource','Wikiquote','Wikiversity','Wikinews','Meta-Wiki','Commons','Wikispecies','Mediawiki'], cv, bn, sd;
  document.getElementById('firstHeading').appendChild(newNode('a',"Import watchlist",{'style':'padding-left:10px;cursor:pointer; float:right; font-size: 13px;',click:function(){
    v.innerHTML='';
    v.appendChild(newNode('form',
      {'style':'display:inline;'},
      'Language: ',cv=newNode('input',{size:3}),
      ' Project: ', qw=newNode('select'), newNode('br'), 
      'Watchlist token ',newNode('small','(can be found be found at ',sd=newNode('a','Special:Preferences',{'href':'http://en.wikipedia.org/wiki/Special:Preferences#preftab-5'}),' in the Watchlist section)'),': ',bn=newNode('input'),
      newNode('input',{'type':'submit','value':'Import watchlist'}),newNode('span',{style:'color:red;'}))).onsubmit = function(){
        if(!/^[a-z]{2,3}(-?[a-z]{2,3})?$/.test(cv.value) && qw.value <=6 || !bn.value){
          bn.parentNode.lastChild.innerHTML = bn.value?"Choose a valid language code.":"Enter watchlist token."
          return false
          }
        var importedurl=(qw.value > 6?(qw.value==7?"meta.wikimedia":qw.value==8?'commons.wikimedia':qw.value==9?'species.wikimedia':'mediawiki'):cv.value+'.'+er[qw.value].toLowerCase())
        localStorage['importedwatchlists'+mw.config.get('wgUserName')] = (localStorage['importedwatchlists'+mw.config.get('wgUserName')]?localStorage['importedwatchlists'+mw.config.get('wgUserName')]+"|":"")+importedurl;
        localStorage['importedwatchlist'+mw.config.get('wgUserName')+importedurl+'token']=bn.value;
        location.reload()
        return false}
    for(var i = 0; i < er.length; i++){qw.appendChild(newNode('option',{'value':i},er[i]))};
    function df(){
      if(/^[a-z]{2,3}(-?[a-z]{2,3})?$/.test(cv.value) || qw.value >6){
        sd.href = "http://"+(qw.value > 6?(qw.value==7?"meta.wikimedia":qw.value==8?'commons.wikimedia':qw.value==9?'species.wikimedia':'mediawiki'):cv.value+'.'+er[qw.value].toLowerCase())+".org/wiki/Special:Preferences#preftab-5"
        }
      }
    cv.onchange=qw.onchange=df
    }}))
  })