Jump to content

User:Anomie/previewtemplatelastmod.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anomie (talk | contribs) at 01:02, 22 July 2008 (try a new script to show last modified timestamp for templates in the preview page). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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={
     callback:function(r, cc){
        if(!r.query) throw new Error('Bad response');
        r=r.query;

        var node=document.getElementById('templatesUsed');
        if(!node) throw new Error('Huh? No body content?');
        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];
        }
        Array.forEach(a, function(a){
            if(typeof(a.wikipage)=='undefined') return;
            if(typeof(pages[a.wikipage])=='undefined') return;
            var r=pages[a.wikipage];
            var li;
            for(li=a; li && li.nodeName!='LI'; li=li.parentNode);
            if(!li) return;
            li.appendChild(document.createTextNode(' '));
            var s=document.createElement('SMALL');
            li.appendChild(s);
            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);
            s.appendChild(document.createTextNode('(last modified '+dt[1]+' '+dt[2]+' by '));
            var x=document.createElement('A');
            x.href=wgServer+wgScript+'?title=User:'+escape(r.user);
            x.appendChild(document.createTextNode(r.user));
            s.appendChild(document.createTextNode(': "'+r.comment+'")'));
        });
    },

    onLoad:function(){
        var node=document.getElementById('templatesUsed');
        if(!node) return;
        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){
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Template:!|Template:-|Template:Cite_book&rvprop=timestamp|user|comment
            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);