Jump to content

User:Evad37/Watchlist-openUnread.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 03:04, 23 January 2017 (fix). 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.
/* Add any or all of these lines after the importScript line to set various options (the default values are shown):

	var openUnread_maxnum = "10";  // Preset value for max number of pages
	var openUnread_oldest = false; // Preset state of checkbox "oldest first". Set to  true  for checked, or  false  for unchecked

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


	// Set default options for any that haven't been set
	if ( window[openUnread_maxnum] === undefined ) { window[openUnread_maxnum] = "10"; }
	if ( window[openUnread_oldest] === undefined ) { window[openUnread_oldest] = false; }
	

	//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>)'+
		'</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" );
	}
	
	// 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() {
			return $( this ).attr("href");
		}).get();

		//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;
			}
		}

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

		// 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");
		}
	});
});