User:Katie lt3/Scripts/KatiesWatchlistManager.js

// Katie's Watchlist Manager mw.loader.using(['mediawiki.api', 'mediawiki.util', 'mediawiki.api.edit'], function {	var api = new mw.Api;	var kwmWatchlistPageName = 'User:' + mediaWiki.user.getName + '/Watchlist';	function kwmCreateSyncDialog(pubexclusive, privexclusive) {		var dialog = $(' ').addClass('kwm-sync-dialog').attr('title', 'Sync watchlists');

$(' Your Private watchlist is the one handled by Wikipedia. Your Public watchlist is the one located at User:Name/Watchlist. ').css({fontSize: '75%'}).appendTo(dialog); var form = $(' ').attr('action', '#').appendTo(dialog);

form.submit(function(e) {			e.preventDefault			var itemsToSync = $(this).serializeArray;			var toPublic = [];			var toPrivate = [];			for (let i = 0; i < itemsToSync.length; i++) {				if (itemsToSync[i].value === "privtopub") {					toPublic.push(itemsToSync[i].name);				} else {					toPrivate.push(itemsToSync[i].name);				}			}			$("Working...").appendTo(form);			return Promise.all([ kwmAddToPrivateWatchlist(toPrivate), kwmAddToPublicWatchlist(toPublic) ]).then(function { $(dialog).empty; $(" Done! ").appendTo(dialog); });		});		function createTable(list, classname, header) { var occlassname = classname; classname = 'kwm-sync-' + occlassname; $(' ').html(header).appendTo(form); var table = $(' ').addClass(classname).appendTo(form);

// Header row {				var headerRow = $(' ').appendTo(table); // Checkbox $(' ').attr('type', 'checkbox').addClass([classname, 'kwm-sync-toggleall']).change(function {					$('input[type=checkbox]', $(this).parent.parent).prop('checked', this.checked);				}).appendTo(' ').appendTo(headerRow); // Label $(' Article ').appendTo(headerRow); }

// Articles {				for (var i = 0; i < list.length; i++) { var article = list[i]; var row = $(' ').appendTo(table); var label = $(' ').attr('for', article).appendTo(' ').text(article).appendTo(row); $(' ').attr('type', 'checkbox').attr('value', occlassname).attr('id', article).attr('name', article).appendTo(' ').prependTo(label); }			}		}

createTable(privexclusive, 'privtopub', 'Private &rarr; public'); createTable(pubexclusive, 'pubtopriv', 'Public &rarr; private');

$(' ').appendTo(form); $(' ').attr('type', 'submit').attr('value', 'Sync').appendTo(form); dialog.dialog({autoOpen: true}); }	function kwmAddToPrivateWatchlist(items) { if (items && items.length !== 0) { console.log("Adding to private watchlist:", items) return api.watch(items); } else { console.log("Nothing to add to private watchlist"); }	}	function kwmAddToPublicWatchlist(items) { if (items && items.length !== 0) { console.log("Adding to public watchlist:", items); return api.edit(kwmWatchlistPageName, function(revision) {				var newContent = revision.content + "\n";				for (let i = 0; i < items.length; i++) {					newContent += "* \n";				}				return newContent;			}); } else { console.log("Nothing to add to public watchlist."); }	}

function kwmGetPrivateWatchlist(wrcontinue) { return api.get({			action: 'query',			wrcontinue: wrcontinue,			format: 'json',			list: 'watchlistraw',			wrnamespace: '0',			wrlimit: 'max'		}).then(function(data) {			var titles;			if (data.continue && data.continue.wrcontinue) {				titles = kwmGetPrivateWatchlist(data.continue.wrcontinue);			} else {				titles = new Promise((function (resolve) {					resolve([]);				}));			}			return titles.then(function(titles) { for (var i = 0; i < data.watchlistraw.length; i++) { titles.push(data.watchlistraw[i].title); }				return titles; });		});	}	function kwmGetPublicWatchlist { return api.get({			action: 'query',			format: 'json',			prop: 'revisions',			indexpageids: 1,			titles: kwmWatchlistPageName,			rvprop: 'content'		}).then(function(data) {			var page = data.query.pages[data.query.pageids[0]];			var content = page.revisions[0]['*'];			return content.match(/(?<=\{\{la2\|)[^\}]+(?=\}\})/g);		}); }	function kwmCompareLists(pub, priv) { return Promise.all([pub, priv]).then(function (lists) {			console.log(lists);			var pub = lists[0];			var priv = lists[1];			var pubexclusive = [];			for (var i = 0; i < pub.length; i++) {				if (priv.indexOf(pub[i]) === -1) {					pubexclusive.push(pub[i]);				}			}

var privexclusive = []; for (var j = 0; j < priv.length; j++) { if (pub.indexOf(priv[j]) === -1) { privexclusive.push(pub[j]); }			}

return [pubexclusive, privexclusive]; });	}	function kwmSync {		return kwmCompareLists( kwmGetPublicWatchlist, kwmGetPrivateWatchlist ).then(function (lists) { kwmCreateSyncDialog(lists[0], lists[1]); });	}

$(document).ready(function {		var link = mw.util.addPortletLink('p-cactions', '#', 'KWM Sync', 'ca-kwm-sync', 'Sync your private watchlist with your monolithic watchlist');		$(link).click(function(e) { e.preventDefault; return kwmSync.then(console.log); });	}); });