Jump to content

User:Danski454/unwatch.js

From Wikipedia, the free encyclopedia
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;//do not add infinite links
			}
			if (typeof unwatchAJAX !== "undefined" && unwatchAJAX) {// add window.unwatchAJAX = true; to appropriate code file to enable AJAX links
				line.append(' <span class="unwatch-link">[<a href="#UNWATCH_PAGE" role="button">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
						//do the same for associated page
						var titleObj = new mw.Title(watch.title);
						if (titleObj.isTalkPage()) {
							//console.log(titleObj.getSubjectPage().toText());
							$('.mw-changeslist-line-inner[data-target-page="' + titleObj.getSubjectPage().toText() + '"] .unwatch-link')
								.html("[<b>unwatched</b>]");//say assosciated page has been unwatched
						} else if (titleObj.getTalkPage()) {//make sure it is not null
							//console.log(titleObj.getTalkPage().toText());
							$('.mw-changeslist-line-inner[data-target-page="' + titleObj.getTalkPage().toText() + '"] .unwatch-link')
								.html("[<b>unwatched</b>]");//say assosciated 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>');
				}
			}
		});
	});
});