User:Anomie/lastmod.js: Difference between revisions

Content deleted Content added
fix ymd format
maintenance: more info TypeError: Cannot read property '0' of undefined
 
(7 intermediate revisions by one other user not shown)
Line 5:
* (Please keep the comment so I can see how many people use this).
*/
 
// See talk for configuration instructions
 
importStylesheet('User:Anomie/lastmod.css');
 
if(typeof(window.LastModDateFormat)=='undefined') window.LastModDateFormat="dmy";
/// Configuration:
if(typeof(window.LastModUseUTCLastModRelative)=='undefined') LastModUseUTCwindow.LastModRelative=false;
// Recognized values are "dmy" or "mdy"; anything else results in YYYY-MM-DD
if(typeof(window.LastModUseUTC)=='undefined') window.LastModUseUTC=false;
// format output.
if(typeof(window.LastModMonths)=='undefined') window.LastModMonths=['January','February','March','April','May','June','July','August','September','October','November','December'];
if(typeof(window.LastModDateFormat)=='undefined') LastModDateFormat="dmy";
 
if(typeof(window.LastModUseUTC)=='undefined') LastModUseUTC=false;
if(mw.config.get('wgNamespaceNumber')>=0) mw.loader.using('mediawiki.util', function(){
if(typeof(window.LastModMonths)=='undefined') LastModMonths=['January','February','March','April','May','June','July','August','September','October','November','December'];
$(document).ready(function(){
var ins=document.getElementById('siteSub');
if(!ins) return;
 
var articleId=mw.config.get('wgArticleId');
addOnloadHook(function(){
} else $.ajax({
if(wgNamespaceNumber<0) return;
url:mw.util.wikiScript('api'),
var ins=document.getElementById('siteSub');
r=x.responseText;dataType:'json',
if(!ins) return;
type:'POST',
} else data:{
format:'json',
action:'query',
pageids:articleId,
prop:'revisions',
rvprop:'timestamp'
var r; },
success:function(r, sts, xhr){
try {
if(typeof( var ts = r.query.pages[wgArticleIdarticleId].revisions[0].timestamp)=='undefined') return;
} catch (e) {
return; // unexpected response e.g. TypeError: Cannot read property '0' of undefined
var dt; }
if(typeof(window.JSONts)!=='undefined'){ return;
m=r.query.pages[wgArticleIdarticleId].revisions[0].timestamp.match(/^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/);
var dt;
if(window.LastModRelative){
dt=[];
var n=new Date();
var dy=n.getUTCFullYear()-m[1];
var dm=n.getUTCMonth()+1-m[2];
var dd=n.getUTCDate()-m[3];
var dh=n.getUTCHours()-m[4];
var di=n.getUTCMinutes()-m[5];
 
if(di<0){ di+=60; dh--; }
var x = sajax_init_object();
if(!xdh<0){ returndh+=60; dd--; }
if(dd<0){ var nn=n; nn.setUTCDate(0); dd+=nn.getDate(); dm--; }
x.open('POST', wgServer+wgScriptPath+'/api.php', true);
if(dm<0){ dm+=12; dy--; }
x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
if(dy>=0){
x.onreadystatechange=function(){
if(dy!=0) dt.push(dy+' year'+((dy==1)?'':'s'));
if(x.readyState!=4) return;
if(dm!=0) dt.push(dm+' month'+((dm==1)?'':'s'));
var r;
if(dd!=0) dt.push(dd+' day'+((dd==1)?'':'s'));
if(typeof(window.JSON)!='undefined'){
r=JSON.parse if(x.responseTextdy==0 && dm==0 && dd<7);{
if(dh!=0) dt.push(dh+' hour'+((dh==1)?'':'s'));
} else {
if(dd<2 && di!=0) dt.push(di+' minute'+((di==1)?'':'s'));
r=x.responseText;
}
r=!(/[^,:{}\[\]0-9.\-+Eaeflnr-u\n\r\t]/.test(r.replace(/"(\\.|[^\x22\\])*"/g,''))) && eval('('+r+')');
}
if(dt.length==0) dt='less than a minute ago';
if(typeof(r.query.pages[wgArticleId].revisions[0].timestamp)=='undefined') return;
else dt='about '+dt.join(', ')+' ago';
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$/);
if(LastModUseUTC) } else {
m[2]-=1; if(window.LastModUseUTC){
} else { m[2]-=1;
var } 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];
}
 
} else if(window.LastModDateFormat=='dmy'){
var dt;
dt=m[3]+' '+window.LastModMonths[m[2]]+' '+m[1];
if(LastModDateFormat=='dmy'){
} else if(window.LastModDateFormat=='dmy'){
dt=m[3]+' '+LastModMonths[m[2]]+' '+m[1];
dt=window.LastModMonths[m[2]]+' '+m[3]+', '+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];
} else {
m[23]++=m[3].toString(); if(m[23]<10.length==1) m[23]='0'+m[23];
m[3] dt=m[31].toString(); if(+'-'+m[32].length==1) m[3]=+'0-'+m[3];
dt=m[1]+'-'+m[2]+'-'+m[3]; }
} dt+=' '+m[4]+':'+m[5];
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);
};,
error:function(xhr,textStatus,errorThrown){
x.send('action=query&format=json&titles='+encodeURIComponent(wgPageName)+'&prop=revisions&rvprop=timestamp');
throw new Error('AJAX error: '+textStatus+' '+errorThrown);
}
});
});
});