Jump to content

User:SD0001/watchlist-update-title.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 15:15, 21 July 2019. 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.
/**
 * Updates the document title to show a prefixed number indicating the number of new 
 * changes when the watchlist undergoes a live update
 * 
 * Should work on any wiki.
 * 
 * Per request by Jürgen Eissink
 *
 */

if (mw.config.get('wgCanonicalSpecialPageName') === 'Watchlist') {
	
	$.ready.then(function() { $('a:contains("' + mw.messages.get('rcfilters-liveupdates-button') + '")').click(); });
	
	var watchlistUpdateNumber = null;
	var watchlistHeading = document.title;
	
	mw.hook('wikipage.content').add(function() {
		
		// prevent action immediately after hook is added
		if ( watchlistUpdateNumber === null ) { 
		    watchlistUpdateNumber = 0;
		    return;
		}
		
		// When "Mark all changes as seen" is clicked
		if (! $('.mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator').length ) {
			watchlistUpdateNumber = 0;
			document.title = watchlistHeading;
			return;
		} 
		
		$('ul.special').children().each(function(i,e) {
			
			// exit on reaching the horizontal line separator
		    if (e.tagName === 'DIV') return false;
		    
		    // don't increment counter on one's own edits
		    if (e.querySelector('bdi').textContent !== mw.config.get('wgUserName')) {
		    	// when the watchlist updates, this hooked function actually gets executed twice due
		    	// to some reason. So increment counter by 0.5 so that the two increments add up to 1.
		    	watchlistUpdateNumber += 0.5;
		    }
		    
		});
		
		if (watchlistUpdateNumber > 0) {
			document.title = '(' + watchlistUpdateNumber + ') ' + watchlistHeading;
		}
		
	});
	
	$(window).focus(function() {
		watchlistUpdateNumber = 0;
		document.title = watchlistHeading;
	});
	
}