User:Anomie/lastmod.js

This is an old revision of this page, as edited by Anomie (talk | contribs) at 14:31, 1 November 2009 (add relative mode). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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/lastmod.js'); // Linkback: [[User:Anomie/lastmod.js]]
 
* (Please keep the comment so I can see how many people use this).
*/

importStylesheet('User:Anomie/lastmod.css');

/// Configuration:

// Recognized values are "dmy" or "mdy"; anything else results in YYYY-MM-DD
// format output.
if(typeof(window.LastModDateFormat)=='undefined') LastModDateFormat="dmy";

if(typeof(window.LastModRelative)=='undefined') LastModRelative=false;

if(typeof(window.LastModUseUTC)=='undefined') LastModUseUTC=false;

if(typeof(window.LastModMonths)=='undefined') LastModMonths=['January','February','March','April','May','June','July','August','September','October','November','December'];


addOnloadHook(function(){
    if(wgNamespaceNumber<0) return;
    var ins=document.getElementById('siteSub');
    if(!ins) return;

    var x = sajax_init_object();
    if(!x) return;
    x.open('POST', wgServer+wgScriptPath+'/api.php', true);
    x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    x.onreadystatechange=function(){
        if(x.readyState!=4) return;
        var r;
        if(typeof(window.JSON)!='undefined'){
            r=JSON.parse(x.responseText);
        } else {
            r=x.responseText;
            r=!(/[^,:{}\[\]0-9.\-+Eaeflnr-u\n\r\t]/.test(r.replace(/"(\\.|[^\x22\\])*"/g,''))) && eval('('+r+')');
        }
        if(typeof(r.query.pages[wgArticleId].revisions[0].timestamp)=='undefined') return;
        m=r.query.pages[wgArticleId].revisions[0].timestamp.match(/^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/);
        var dt;
        if(LastModRelative){
            dt=[];
            var n=new Date();
            var showhours=true, showminutes=true;
            var d=n.getUTCFullYear()-m[1]; if(d!=0){ dt.push(d+' year'+((d==1)?'':'s')); showhours=false; }
            d=n.getUTCMonth()+1-m[2]; if(d!=0){ dt.push(d+' month'+((d==1)?'':'s')); showhours=false; }
            d=n.getUTCDate()-m[3];
            if(d!=0){
                dt.push(d+' day'+((d==1)?'':'s'));
                if(d>=7) showhours=false;
                if(d>1) showminutes=false;
            }
            if(showhours){
                d=n.getUTCHours()-m[4]; if(d!=0) dt.push(d+' hours'+((d==1)?'':'s'));
                if(showminutes) d=n.getUTCMinutes()-m[5]; if(d!=0) dt.push(d+' minutes'+((d==1)?'':'s'));
            }
            dt='about '+dt.join(', ')+' ago';
        } else {
            if(LastModUseUTC){
                m[2]-=1;
            } else {
                dt=new Date(Date.UTC(m[1],m[2]-1,m[3],m[4],m[5],m[6]));
                m[1]=dt.getFullYear();
                m[2]=dt.getMonth();
                m[3]=dt.getDate();
                m[4]=dt.getHours().toString(); if(m[4].length==1) m[4]='0'+m[4];
                m[5]=dt.getMinutes().toString(); if(m[5].length==1) m[5]='0'+m[5];
                m[6]=dt.getSeconds().toString(); if(m[6].length==1) m[6]='0'+m[6];
            }

            if(LastModDateFormat=='dmy'){
                dt=m[3]+' '+LastModMonths[m[2]]+' '+m[1];
            } else if(LastModDateFormat=='dmy'){
                dt=LastModMonths[m[2]]+' '+m[3]+', '+m[1];
            } else {
                m[2]++; if(m[2]<10) m[2]='0'+m[2];
                m[3]=m[3].toString(); if(m[3].length==1) m[3]='0'+m[3];
                dt=m[1]+'-'+m[2]+'-'+m[3];
            } 
            dt+=' '+m[4]+':'+m[5];
        }

        var s=document.createElement('SPAN');
        s.className='lastmodheader';
        s.appendChild(document.createTextNode('Last modified '+dt));
        ins.parentNode.insertBefore(s,ins);
    };
    x.send('action=query&format=json&titles='+encodeURIComponent(wgPageName)+'&prop=revisions&rvprop=timestamp');
});