User:Writ Keeper/Scripts/watchlistToggle.js

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.
function removeWatchlistItem(element)
{
	mw.loader.using("mediawiki.api").done(function()
	{
		var pageTitle = $(element).siblings().find(".mw-changeslist-history").prop("title");
		var mwApi = new mw.Api();
		mwApi.unwatch(pageTitle).done(function() 
		{
			$(element).unbind("click");
			$(element).click(function ()
			{
				return addWatchlistItem(this);
			});
			$(element).text("add");
			$(element).attr("title","Re-add this item to your watchlist");
		});
	});
	return false;
}
function addWatchlistItem(element)
{
	var pageTitle = $(element).siblings().find(".mw-changeslist-history").prop("title");
	var mwApi = new mw.Api();
	mwApi.watch(pageTitle).done(function() 
	{
		$(element).unbind("click");
		$(element).click(function ()
		{
			return addWatchlistItem(this);
		});
		$(element).text("rem");
		$(element).attr("title","Remove this item from your watchlist");
	});
	return false;
}

$(document).ready( function()
{
	if(mw.config.get("wgCanonicalSpecialPageName") === "Watchlist")
	{
		$("li.mw-changeslist-edit .mw-changeslist-links").not(".mw-usertoollinks").each(function(ind, el){$(el).append("&nbsp;|&nbsp;<a class='watchlistToggle' title='Remove this item from your watchlist'>rem</a>")});
		$("a.watchlistToggle").click(function() 
		{
			return removeWatchlistItem(this);
		});
	}
});