User:Writ Keeper/Scripts/contribsHistory.js

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.
diffRequestLocked = "f";
if(mw.config.get("wgCanonicalSpecialPageName") == "Contributions")
{
	
	function addContribsInspectionBoxes() {
	
			
			$("#mw-content-text ul").children("li").each(function(index, element)
			{
				if($(element).children('[href*="diff=prev"]').length > 0 && !(/(\.js|\.css)$/.test($(element).children('.mw-changeslist-date').attr('title'))))
				{
					var diffURL = $(element).children('.mw-changeslist-date').attr("href");
					var regex = /&oldid=(\d+)/.exec(diffURL);
					var diffID = regex[1];
					var inlineDiffButton;
                                        if(typeof inlineDiffSmallUI != "undefined")
                                        { 
                                              inlineDiffButton = document.createElement("a");
					      inlineDiffButton.href = "#";
					      inlineDiffButton.innerHTML = '<b><span style="color:black;">[</span><span style="color:MidnightBlue;">inspect diff</span><span style="color:black;">]</span></b>';
                                        }
                                        else
                                        {
                                              inlineDiffButton = document.createElement("input");
					      inlineDiffButton.type = "button";
					      inlineDiffButton.value = "Inspect edit";
                                        }
					inlineDiffButton.id = diffID;
					$(inlineDiffButton).click(function(){ return inspectContribsDiff(this);});
					element.appendChild(inlineDiffButton);
				}
			});

			mw.loader.load('mediawiki.action.history.diff')
		}
 
	function inspectContribsDiff(button)
	{
		if(diffRequestLocked === "t")
		{
			alert("An old request is still being processed, please wait...");
			return false;
		}
		else
		{
			diffRequestLocked = "t";
			$.getJSON("/w/api.php?action=query&prop=revisions&format=json&rvprop=timestamp&rvdiffto=prev&revids="+button.id, function(response, status){
 
				var diffString = response.query.pages[Object.keys(response.query.pages)[0]].revisions[0].diff["*"];
 
				if(diffString == null)
				{
				alert("Request failed!");
					diffRequestLocked = "f";
					return;
				}
 
				var newTable = document.createElement("table");
				newTable.className = "diff";
 
				var colGroup = document.createElement("colgroup");
				var diffCol = document.createElement("col");
				diffCol.className = "diff-marker";
				colGroup.appendChild(diffCol);
				diffCol = document.createElement("col");
				diffCol.className = "diff-content";
				colGroup.appendChild(diffCol);
				diffCol = document.createElement("col");
				diffCol.className = "diff-marker";
				colGroup.appendChild(diffCol);
				diffCol = document.createElement("col");
				diffCol.className = "diff-content";
				colGroup.appendChild(diffCol);
				newTable.appendChild(colGroup);
 
				$(newTable).append(diffString);
				$(newTable).insertAfter("#"+ button.id);
				newTable.id = button.id + "display";
	
				$(button).unbind("click");
                                if(typeof inlineDiffSmallUI != "undefined")
                                {
				    $(button).html('<b><span style="color:black;">[</span><span style="color:MidnightBlue;">hide diff</span><span style="color:black;">]</span></b>');
				    $(button).click(function(){ return hideSmallEditInspection(this);});
                                }
                                else
                                {
                                    $(button).attr("value","Hide edit");
                                    $(button).click(function(){ return hideEditInspection(this);});
                                }
 
				diffRequestLocked = "f";
				});
	
			}
                return false;
	}
 
	function showEditInspection( button)
	{
		$("#"+button.id+"display").css("display", "");
		$(button).attr("value","Hide edit");
		$(button).unbind("click");
		$(button).click(function(){ return hideEditInspection(this);});
                return false;
	}
 
	function hideEditInspection( button)
	{
		$("#"+button.id+"display").css("display", "none");
		$(button).attr("value","Show edit");
		$(button).unbind("click");
		$(button).click(function(){ return showEditInspection(this);});
                return false;
	}

	function showSmallEditInspection( button)
        {
                $("#"+button.id+"display").css("display", "");
		$(button).html('<b><span style="color:black;">[</span><span style="color:MidnightBlue;">hide diff</span><span style="color:black;">]</span></b>');
		$(button).unbind("click");
		$(button).click(function(){ return hideSmallEditInspection(this);});
                return false;
        }
        
        function hideSmallEditInspection( button)
        {
		$("#"+button.id+"display").css("display", "none");
		$(button).html('<b><span style="color:black;">[</span><span style="color:MidnightBlue;">show diff</span><span style="color:black;">]</span></b>');
		$(button).unbind("click");
		$(button).click(function(){ return showSmallEditInspection(this);});
                return false;
        }

	$(document).ready(addContribsInspectionBoxes);
}