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 02:45, 23 January 2017 (enhanced watchlist uses tables). 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.
$(document).ready( function () {
	if( mw.config.get('wgNamespaceNumber') != -1 ) {
	// only operate in Special: namespace, on Special:Watchlist
	return;
	}

console.log("Watchlist-openUnread.js script running");

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

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