User:This, that and the other/afdlog.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.
// This script is a bookmarklet. You can make it into a 
// bookmarklet using http://ted.mielczarek.org/code/mozilla/bookmarklet.html
// and then run the bookmarklet on any WP page.

if (typeof mw === "undefined") {
	alert("You must be on a Wikipedia page for this to work.");
	return;
}
mw.loader.using(["ext.gadget.Twinkle"], function() {
	window.afdlog = {};
	window.afdlog.fetchContribs = function(ucstart) {
		var api = new Wikipedia.api("action", { 
			'action': 'query',
			'rawcontinue': '',
			'list': 'usercontribs',
			'uclimit': 500,
			'ucuser': wgUserName,
			'ucnamespace': 4,
			'ucprop': 'title|flags|timestamp',
			'ucstart': ucstart
		}, window.afdlog.contribsCallback);
		api.post();
	};
	window.afdlog.contribResults = [];
	window.afdlog.contribResultsIndex = 0;
	window.afdlog.contribsCallback = function(apiResult) {
		var $doc = $(apiResult.responseXML);
		$doc.find('item[new=""][title^="Wikipedia:Articles for deletion/"]').each(function() {
			var $this = $(this);
			var ts = new Date($this.attr("timestamp")).toUTCString();
			var title = $this.attr("title").substring(32).replace(/ \([0-9]+[nrst][dht] nomination\)$/, "");
			if (title.indexOf("Log/2") !== 0) {
				window.afdlog.contribResults.push({ ts: ts, title: title, afd: $this.attr("title") });
			}
		});
		var $ctn = $doc.find("query-continue usercontribs");
		if ($ctn.length) {
			window.afdlog.fetchContribs($ctn.attr("ucstart"));
		} else {
			window.afdlog.fetchPage();
		}
	};
	window.afdlog.fetchPage = function() {
		var sect = window.afdlog.contribResults.slice(window.afdlog.contribResultsIndex, window.afdlog.contribResultsIndex + 19);
		var titles = [];
		$.each(sect, function(k, v) {
			titles.push(v.title);
		});
		var api = new Wikipedia.api("action", { 
			'action': 'query',
			'prop': 'info',
			'titles': titles.join("|")
		}, window.afdlog.pageCallback);
		api.post();
	};
	window.afdlog.pageCallback = function(apiResult) {
		$(apiResult.responseXML).find("page").each(function() {
			var $this = $(this);
			var newarr = window.afdlog.contribResults.filter(function(v) { 
				return v.title === $this.attr("title");
			});
			newarr[0].exists = $this.attr("missing") !== "";
		});
		window.afdlog.contribResultsIndex += 20;
		if (window.afdlog.contribResultsIndex >= window.afdlog.contribResults.length) {
			window.afdlog.$ol.empty();
			$.each(window.afdlog.contribResults, function(k, v) {
				window.afdlog.$ol.append($('<li>' + v.ts + ': <a href="' + wgArticlePath.replace("$1", v.title) + 
					'" style="color:' + (v.exists ? '#0645AD' : '#BA0000') + '">' + v.title + '</a> was <a href="' + 
					wgArticlePath.replace("$1", v.afd) + '" style="color:#0645AD">nominated for AfD</a></li>'));
			});
			window.afdlog.$ol.css({'margin-left': '10px', 'padding-left': '34px'});
		} else {
			window.afdlog.fetchPage();
		}
	};
	window.afdlog.$ol = $('<ol>Please wait... (may take several minutes)</ol>');
	window.afdlog.$ol.dialog({width: 900, height: 480, title: 'Your AfD nominations'});
	window.afdlog.fetchContribs();
});