User:Hans Adler/superwatchlist.js

/** * Create a new DOM node for the current document. *   Basic usage:  var mySpan = newNode('span', "Hello World!") *   Supports attributes and event handlers*: var mySpan = newNode('span', {style:"color: red", focus: function{alert(this)}, id:"hello"}, "World, Hello!") *   Also allows nesting to create trees: var myPar = newNode('p', newNode('b',{style:"color: blue"},"Hello"), mySpan) * * *event handlers, there are some issues with IE6 not registering event handlers on some nodes that are not yet attached to the DOM, * it may be safer to add event handlers later manually. function newNode(tagname){ var node = document.createElement(tagname); for( var i=1;i<arguments.length;i++ ){ if(typeof arguments[i] == 'string'){ //Text node.appendChild( document.createTextNode(arguments[i]) ); }else if(typeof arguments[i] == 'object'){ if(arguments[i].nodeName){ //If it is a DOM Node node.appendChild(arguments[i]); }else{ //Attributes (hopefully) for(var j in arguments[i]){ if(j == 'class'){ //Classname different because... node.className = arguments[i][j]; }else if(j == 'style'){ //Style is special node.style.cssText = arguments[i][j]; }else if(typeof arguments[i][j] == 'function'){ //Basic event handlers try{ node.addEventListener(j,arguments[i][j],false); //W3C }catch(e){try{ node.attachEvent('on'+j,arguments[i][j],"Language"); //MSIE }catch(e){ node['on'+j]=arguments[i][j]; }}; //Legacy }else{ node.setAttribute(j,arguments[i][j]); //Normal attributes }       }      }    }  }  return node; }

// interwiki watchlist (shamelessly stolen from http://en.wiktionary.org/wiki/User:Yair_rand/superwatchlist.js) 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:wgUserName,wltoken:tkn, wlexcludeuser: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;  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;','submit':function{        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}}, 'Language: ',cv=newNode('input',{size:3}), ' Project: ', qw=newNode('select'), newNode('br'), 'Watchlist token ',newNode('small','(can be found be found at ',newNode('a','Special:Preferences',{'href':'http://en.wikipedia.org/wiki/Special:Preferences#mw-prefsection-watchlist'}),' in the Watchlist section)'),': ',bn=newNode('input'), newNode('input',{'type':'submit','value':'Import watchlist'}))); for(var i = 0; i < er.length; i++){qw.appendChild(newNode('option',{'value':i},er[i]))}; }})) })