User:Lupin/listsince.js

Source: 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.
function addListSinceLink() {
	var found=findNamespaceForm();
	if (!found) return;
	window.sinceLinkThen=+(new Date());
	var sp=document.createElement('span');
	sp.innerHTML='<a id="listSince" onclick="loadSincePage()" href="#">Changes since last load</a>';
	found.parentNode.insertBefore(sp,found.nextSibling);
}

function findNamespaceForm() {
	var forms=document.getElementsByTagName('form');
	for (var i=0; i<forms.length; ++i) {
		if (forms[i].innerHTML.indexOf('Namespace:')> -1) { return forms[i]; }
	}
	return null;
}

function listSinceUrl( ) {
	var then=window.sinceLinkThen;
	var days = ( +(new Date()) - then )/(1000 * 3600 * 24);
	var url=document.location.href.split('#')[0];
	if ( /[?&]days=/.test(url) ) return url.replace(/([?&]days=)[^?&]*/, '$1'+days);
	if (url.indexOf('?')==-1) url += '?'; else url += '&';
	return url + 'days=' + days;
}

window.loadSincePage=function() {
	var lk=document.getElementById('listSince');
	lk.href=listSinceUrl();
}
	
if (/[=\/]Special(%3[Aa]|:)Watchlist/.test(document.location.href)) {
	addOnloadHook(addListSinceLink);
}