Jump to content

User:SD0001/watchlist-update-title.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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
 * [[Wikipedia:User_scripts/Requests/Archive_4#Watchlist_notifier:_number_of_(automated)_updates_in_browser_page_title]]
 *
 */

if (mw.config.get('wgCanonicalSpecialPageName') === 'Watchlist') {
	
	// Enable "Live updates" every time
	$.ready.then(function() { $('a:contains("' + mw.messages.get('rcfilters-liveupdates-button') + '")').click(); });
	
	var watchlistUpdateNumber = 0;
	var watchlistHeading = document.title;
	var markSeenButtonPressed = false;
	
	$('.mw-rcfilters-ui-markSeenButtonWidget').on('click', function() {
		markSeenButtonPressed = true;
	});
	
	mw.hook('wikipage.content').add(function($content) {
		if (!$content.hasClass('mw-rcfilters-ui-changesListWrapperWidget')) {
			return;
		}
		
		// Handle the hook being fired just after the "Mark all changes as seen" is clicked
		if (markSeenButtonPressed) {
			watchlistUpdateNumber = 0;
			document.title = watchlistHeading;
			markSeenButtonPressed = false;
			return;
		}

		$content.find('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')) {
		    	watchlistUpdateNumber += 1;
		    }
		    
		});
		
		if (watchlistUpdateNumber > 0) {
			document.title = '(' + watchlistUpdateNumber + ') ' + watchlistHeading;
		}
		
	});
	
	$(window).focus(function() {
		watchlistUpdateNumber = 0;
		document.title = watchlistHeading;
	});
	
}