Jump to content

User:Danski454/unwatch.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Danski454 (talk | contribs) at 12:08, 6 May 2019 (implement AJAX). 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.
/*
 * Based off Unwatch from watchlist
 * @source https://www.mediawiki.org/wiki/Snippets/Unwatch_from_watchlist
 * @author Krinkle
 * @revision 2016-09-01
*/
$(document).ready(function(){
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Watchlist' || location.href.indexOf( '/edit' ) > 0 || location.href.indexOf( '/raw' ) > 0 ) {
		return;
	}
	mw.hook( 'wikipage.content' ).add(function(){
		$(".mw-changeslist-line-inner").each(function(){
			var line = $(this);
			if (line.find(".unwatch-link").length) {
				return false;//do not add infinite links, end loop for speed
			}
			if (typeof unwatchAJAX !== "undefined" && unwatchAJAX) {
				line.append(' <span class="unwatch-link">[<a href="#">unwatch</a>]</span>');//add fake link
				line.find(".unwatch-link a").click(function(){
					var page = $(this).parent().parent().attr('data-target-page');//get page name
					var api = new mw.Api();
					api.unwatch(page).done(function(watch){//unwatch it
						$('.mw-changeslist-line-inner[data-target-page="' + watch.title + '"] .unwatch-link').html("[<b>unwatched</b>]");//say page has been unwatched
					});
				});
			} else {
				var href = line.find(".mw-changeslist-history").attr("href");
				if (href) {
					href = href .replace("action=history", "action=unwatch");
					line.append(' <span class="unwatch-link">[<a href="' + href + '">unwatch</a>]</span>');
				}
			}
		});
	});
});