User:Happy-melon/previewtemplatelastmod.js

/* Based on User:Anomie/previewtemplatelastmod.js */ importScript('User:Anomie/util.js'); var PreviewTemplateLastMod={ node:null, LIs:[], callback:function(r, cc){ if(!r.query) throw new Error('Bad response'); r=r.query; var node=PreviewTemplateLastMod.node; if(!node) return; a=node.getElementsByTagName('A'); if(a.length==0) return; var pages={}; if(r.pages) for(var i in r.pages){ if(typeof(r.pages[i].revisions)!='undefined') pages[r.pages[i].title]=r.pages[i].revisions[0]; }       for(var j=a.length-1; j>=0; j--){ if(typeof(a[j].wikipage)=='undefined') continue; if(typeof(pages[a[j].wikipage])=='undefined') continue; var r=pages[a[j].wikipage]; var li; for(li=a[j]; li && li.nodeName!='LI'; li=li.parentNode); if(!li) continue; li.timestamp=r.timestamp; PreviewTemplateLastMod.LIs.push(li); var b=li.firstChild; var dt=r.timestamp.match(/^(\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d):\d\dZ$/); if(!dt) throw new Error('Invalid timestamp: '+r.timestamp); var s=document.createElement('SPAN'); s.className='PreviewTemplateLastMod_timestamp'; s.appendChild(document.createTextNode(dt[1]+' '+dt[2]+' '), li.firstChild); li.insertBefore(s, b); li.insertBefore(document.createTextNode('('), b);           var x=document.createElement('A');            x.href=a[j].href+((a[j].href.indexOf('?')<0)?'?':'&')+'action=history';            x.className='PreviewTemplateLastMod_history';            x.appendChild(document.createTextNode('hist'));            li.insertBefore(x, b);            li.insertBefore(document.createTextNode(') '), b); li.appendChild(document.createTextNode(' ')); x=document.createElement('A'); x.href=mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=User:'+escape(r.user); x.className='PreviewTemplateLastMod_user'; x.appendChild(document.createTextNode(r.user)); li.appendChild(x); li.appendChild(document.createTextNode(': ')); var s=document.createElement('SPAN'); s.className='PreviewTemplateLastMod_comment'; if (!r.comment) r.comment = ''; s.appendChild(document.createTextNode(r.comment)); li.appendChild(s); }       PreviewTemplateLastMod.LIs.sort(function(a,b){ return (a.timestampb.timestamp)?1:0; }); var cur=PreviewTemplateLastMod.LIs[0].parentNode.firstChild; for(var j=PreviewTemplateLastMod.LIs.length-1; j>=0; j--){ if(PreviewTemplateLastMod.LIs[j]===cur){ cur=cur.nextSibling; } else { cur.parentNode.insertBefore(PreviewTemplateLastMod.LIs[j],cur); }       }    },    onLoad:function{ var node=getElementsByClassName(document.body, 'DIV', 'templatesUsed'); if(!node || node.length!=1) return; node=node[0]; PreviewTemplateLastMod.node=node; var a=node.getElementsByTagName('A'); if(a.length==0) return; a=Array.prototype.map.call(a, function(a){           a.wikipage=a.title;            return a.wikipage;        }).sort.filter(function(e,i,a){            return e!=='' && (i==0 || a[i-1]!==e);        }); while(a.length>0){ api({action:'query', prop:'revisions', rvprop:'timestamp|user|comment', titles:a.splice(0,50)}, PreviewTemplateLastMod.callback); }   } }; if(doneOnloadHook) PreviewTemplateLastMod.onLoad; //if imported dynamically else addOnloadHook(PreviewTemplateLastMod.onLoad);