Jump to content

User:Sun Creator/previewtemplatelastmod.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* If you want to use this script, simply add the following line 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).
*/

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';
            s.appendChild(document.createTextNode(r.comment));
            li.appendChild(s);
        }
        PreviewTemplateLastMod.LIs.sort(function(a,b){ return (a.timestamp<b.timestamp)?-1:(a.timestamp>b.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.map(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);