User:Splarka/lastmod.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Splarka/lastmod. |
/* Last mod duration inserter, version [0.0.1]
Originally from: http://en.wiki.x.io/wiki/User:Splarka/lastmod.js
Parses the "last mod" date in getElementById('lastmod') and returns a duration since.
Notes:
* Checks via API for your timezone offset preference.
** If none is found it uses site timezone offset.
*** Once found it sets a cookie for a day (or session).
* Inserts it as a text node in the 'lastmod' object.
* Language sensitive, fragile, can fail gracefully if the interace message or language changes.
*/
var lmRegex = /last modified on (\d+ [a-z]{3,10} \d{4}) at (\d{2}\:\d{2})\./i;
if(wgAction == 'view' && wgNamespaceNumber >= 0 && wgArticleId != 0) addOnloadHook(lastmodShowDuration)
function lastmodShowDuration() {
var lm = document.getElementById('lastmod');
if(!lm) return;
if(!getCookie('timecorrection')) {
getOffsetCookie();
return;
}
var lmt = getText(lm);
if(!lmt.match(lmRegex)[2]) return
var lmdate = lmt.match(lmRegex)[1] + ' ' + lmt.match(lmRegex)[2];
var lmd = new Date(lmdate + ' GMT');
var now = new Date();
var ms = lmd.getTime() - (parseInt(getCookie('timecorrection')) * 60000);
lmd.setTime(ms);
var diff = parseInt((now.getTime() - lmd.getTime()) / 1000);
if(diff < 0) return
lm.appendChild(document.createTextNode(' About ' + duration(diff,2) + ' ago.'));
}
function getOffsetCookie() {
var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&format=json&meta=userinfo|siteinfo&uiprop=options';
var req = sajax_init_object();
req.open('GET', url, true);
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
eval("getOffsetCookieHandler(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')");
}
}
req.send(null);
}
function getOffsetCookieHandler(obj,txt) {
if(obj['error']) {
//jsMsg('Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info'] + '\n');
return;
}
var timeoffset = 0;
if(obj['query'] && !obj['query']['general'] && !obj['query']['general']['timeoffset']) {
timeoffset = obj['query']['general']['timeoffset'];
}
if(obj['query']['userinfo'] && obj['query']['userinfo']['options'] && obj['query']['userinfo']['options']['timecorrection']) {
var tc = obj['query']['userinfo']['options']['timecorrection'].split('|');
if(tc[1]) timeoffset = tc[1]
}
setCookie('timecorrection',timeoffset,1);
if(getCookie('timecorrection')) lastmodShowDuration()
}
function duration(input,depth) {
var num = input;
var out = '';
var s = num % 60; num = Math.floor(num / 60);
var m = num % 60; num = Math.floor(num / 60);
var h = num % 24; num = Math.floor(num / 24);
var d = num % 7; num = Math.floor(num / 7);
var w = num % 52; num = Math.floor(num / 52);
var y = num
if(y > 0) out += y + 'yrs '
if(y + w > 0) out += w + 'wks '
if(y + w + d > 0) out += d + 'days '
if(y + w + d + h > 0) out += h + 'hrs '
if(y + w + d + h + m > 0) out += m + 'mins '
out += s + 'secs';
if(depth && depth < out.split(' ').length) {
out = out.split(' ').slice(0,depth).join(' ');
}
return out;
}
function getText(object) {
if (object.nodeType == 3) return object.nodeValue;
var txt = [];
var i=0;
while(object.childNodes[i]) {
txt[txt.length] = getText(object.childNodes[i]);
i++;
}
return txt.join('');
}
function setCookie(cookieName, cookieValue, days) {
var today = new Date();
var expire = new Date();
var nDays = days || 365;
expire.setTime(today.getTime() + (3600000 * 24 * nDays));
document.cookie = cookieName + '=' + escape(cookieValue) + ';path=/' + ';expires='+expire.toGMTString();
}
function getCookie(cookieName) {
var start = document.cookie.indexOf(cookieName + '=');
if(start == -1) return '';
var len = start + cookieName.length + 1;
if((!start) && (cookieName != document.cookie.substring(0, cookieName.length))) return ''
var end = document.cookie.indexOf(';', len);
if(end == -1) end = document.cookie.length
return unescape(document.cookie.substring(len, end));
}
function deleteCookie(cookieName) {
if(getCookie(cookieName)) setCookie(cookieName,null,-365)
}