Jump to content

User:Evad37/Watchlist-openUnread.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
better support for enhanced watchlist. Less customisable, beacause that caused problems in firefox.
use wgCanonicalSpecialPageName so that this only runs on the watchlist, not other special pages
Line 1: Line 1:


$(document).ready( function () {
$(document).ready( function () {
if( mw.config.get('wgNamespaceNumber') != -1 ) {
if( mw.config.get('wgCanonicalSpecialPageName') != 'Watchlist' ) {
// only operate in Special: namespace, on Special:Watchlist
// only operate in Special: namespace, on Special:Watchlist
return;
return;

Revision as of 04:44, 23 January 2017

$(document).ready( function () {
	if( mw.config.get('wgCanonicalSpecialPageName') != 'Watchlist' ) {
	// only operate in Special: namespace, on Special:Watchlist
	return;
	}


	// Set default options 
	var openUnread_maxnum = "10"; 
	var openUnread_oldest = true; 
	var openUnread_diff = true; 

	//Add a form to open multiple unread pages
	var openUnread_form = '<div id="openUnread-input" style="margin:5px 0;">'+
		'<button id="openUnread-go">Open unread pages</button> '+
		'(Options: <input type="text" name="openUnread-number" id="openUnread-number" style="width:2em;" value=' + openUnread_maxnum + ' /> pages max / '+
		'<span class="mw-input-with-label"><input name="openUnread-order" id="openUnread-order" type="checkbox">&nbsp;<label for="openUnread-order">oldest first</label></span> / '+
		'<span class="mw-input-with-label"><input name="openUnread-diff" id="openUnread-diff" type="checkbox">&nbsp;<label for="openUnread-diff">show diffs</label></span>)'+
		'</div><hr>';
	$("form#mw-watchlist-form").after(openUnread_form);
	$("fieldset#mw-watchlist-options").css("margin-bottom", "0");

	if (openUnread_oldest) {
		$("#openUnread-order").prop( "checked", "true" );
	}
	if (openUnread_diff) {
		$("#openUnread-diff").prop( "checked", "true" );
	}
	
	// Button clicked
	$("#openUnread-go").click(function() {
		// get form options
		var maxnum = parseInt( $('#openUnread-number').val() );
		if (isNaN(maxnum) || maxnum < 1) {
			return;		// error - zero, negative, or not a number
		}
		
		// get unread title links as array
		var unread = $("li.mw-changeslist-line-watched, table.mw-changeslist-line-watched").find("a.mw-changeslist-title").map(function() {
			var diffquery;
			if ( $('#openUnread-diff').prop('checked') ) {
				$sincelastvisit = $(this).parent().parent().find("a:contains('since last visit')");
				$changes = $(this).parent().parent().find("a:contains('changes')");
				if ($sincelastvisit.length === 1) {
					diffquery="?" + $sincelastvisit.attr("href").match(/diff=.*&oldid=.*/g);
				} else if ($changes.length === 1) {
					diffquery="?" + $changes.attr("href").match(/diff=.*&oldid=.*/g);
				} else {
					diffquery = "?diff=cur&oldif=prev";
				}
			} else {
				diffquery = "";
			}
			return $( this ).attr("href") + diffquery;
		}).get();

		// reverse if order is oldest first
		if ( $('#openUnread-order').prop('checked') ) {
			unread.reverse();
		}


		//remove duplicates
		var seen = {};
		var unique_unread = [];
		var len = unread.length;
		var j = 0;
		for (var i = 0; i < len; i++) {
			var item = unread[i];
			if(seen[item] !== 1) {
				seen[item] = 1;
				unique_unread[j++] = item;
			}
		}

		// number of links to open
		if (unique_unread.length < maxnum) {
			maxnum = unique_unread.length;
		}

		// open links in new tabs/windows
		for (var ii=0; ii < maxnum; ii++) {
			window.open(unique_unread[ii], "_blank");
		}
	});
});