Jump to content

User:Dantheox/watchlistdiff.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.
//<pre>
function instrumentWLgroup(g) {
	var edits = g.getElementsByTagName('a');
	var re = new RegExp(/curid=\d+&oldid=(\d+)/);
	var pre = new RegExp(/^(.*?\/w\/index.php\?title=[^&]+)/);
	var grp = g.id;
	var ids = new Array();
	var pageLink = null;
	for (var i=0; i<edits.length; i++) {
		var m = re.exec(edits[i].href);
		if (m != null && edits[i].href.match(/&diff=/) == null) {
			// This is a link to a particular revision being listed on the watchlist
			ids.push(m[1]);
		}
		
		m = pre.exec(edits[i].href);
		if (m != null && edits[i].href.match(/&diff=/)) {
			pageLink = m[1];
		}
	}

	for (var i=0; i<g.childNodes.length; i++) {
		var n = g.childNodes[i];
		if (n.nodeType==3 && n.textContent==") . . ") {
			var revID = ids.shift();
			var newNode = document.createElement('span');
			var rA = g.id + "A";
			var rB = g.id + "B";
			var doDiff = "location.href=\"" + pageLink + "&diff=\"+" + rB + "+\"&oldid=\"+" + rA + "; ";

			newNode.innerHTML = ") " + 
				"<input type=radio name=" + g.id + "A onClick='" + rA + "=" + revID + "; if ("+rB+") { " + doDiff + " } '/>" + 
				"<input type=radio name=" + g.id + "B onClick='" + rB + "=" + revID + "; if ("+rA+") { " + doDiff + " } '/> ";
			g.replaceChild(newNode, n);
		}
	}
}

function watchListGroup() {
	if (window.location.href.indexOf("Special:Watchlist") != -1) {
		var items=document.getElementById('bodyContent').getElementsByTagName('div');
		for (var i=0; i<items.length; i++) {
			if (items[i].id.match(/RCI\d+/)) {
				instrumentWLgroup(items[i]);
			}
		}
	}
}

addOnloadHook(watchListGroup);
//</pre>