User:Ahecht/previewtemplatelastmod.js

/* If you want to use this script, simply add the following lines to your monobook.js:

importScript('User:Anomie/previewtemplatelastmod.js'); // Linkback: User:Anomie/previewtemplatelastmod.js


 * (Please keep the comment so I can see how many people use this).

var PreviewTemplateLastMod={ node:null, LIs:[],

callback:function(r, sts, xhr){ if(!r.query){ if(typeof(window.console)=='undefined' || typeof(window.console.error)!='function') throw new Error('Bad response'); window.console.error("Bad response", r); return; }       r=r.query;

var node=PreviewTemplateLastMod.node; if(!node) return; var a=node.getElementsByTagName('A'); if(a.length==0) return;

var pages={}; if(r.pages) for(var i in r.pages){ pages[r.pages[i].title]=r.pages[i]; }       for(var j=a.length-1; j>=0; j--){ if(typeof(a[j].wikipage)=='undefined') continue; if(a[j].href.indexOf('action=edit')>=0) continue; if(typeof(pages[a[j].wikipage])=='undefined') continue; var p=pages[a[j].wikipage];

var li; for(li=a[j]; li && li.nodeName!='LI'; li=li.parentNode); if(!li) continue;

if(typeof(p.watched)!='undefined'){ a[j].className+=' mw-watched'; li.className+=' mw-watched'; }

if(typeof(p.revisions)=='undefined') continue; var r=p.revisions[0]; 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.util.wikiScript+'?title=User:'+encodeURIComponent(r.user); x.className='PreviewTemplateLastMod_user'; x.appendChild(document.createTextNode(r.user)); li.appendChild(x); li.appendChild(document.createTextNode(': ')); var s=document.createElement('SPAN'); if(typeof(r.comment) == 'undefined') { s.className='PreviewTemplateLastMod_no_comment'; s.appendChild(document.createTextNode('(no summary)')); } else { s.className='PreviewTemplateLastMod_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=$('.posteditwindowhelplinks','div.templatesUsed'); var a=node.find('A:first'); if(a.length==0) {return;} a.replaceWith("sort"); },   onClick:function { var node=$('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){ $.ajax({               url:mw.util.wikiScript('api'),                dataType:'json',                type:'POST',                data:{                    format:'json',                    action:'query',                    titles:a.splice(0,50).join('|'),                    prop:'info|revisions',                    rvprop:'timestamp|user|comment',                    inprop:'watched'                },                success:PreviewTemplateLastMod.callback,                error:function(xhr,textStatus,errorThrown){                    throw new Error('AJAX error: '+textStatus+' '+errorThrown);                }            }); }   } };

mw.loader.using('mediawiki.util', function{   $(document).ready(PreviewTemplateLastMod.onLoad); });