Jump to content

User:Writ Keeper/Scripts/previewAndDiff.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Writ Keeper (talk | contribs) at 10:51, 17 December 2013 (remove on new sections). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
$(document).ready(function()
{
	if($("form#editform").length == 1 && $("input[name='wpSection']").attr("value") != "new")
	{
		

		if(/previewanddiff=true/.test(window.location.href))
		{
			mw.loader.load('mediawiki.action.history.diff');
			$("#wikiPreview").after('<h2></h2><h2>Your changes</h2><table class="diff" id="ajaxDiff"><colgroup><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content"></colgroup></table>');
			var requestString="/w/api.php";
			var requestData = {action:"query", prop:"revisions", format:"json", rvcontentformat:"text/x-wiki", revids:$("input[name='baseRevId']").attr("value"), rvdifftotext:$("textarea#wpTextbox1").text()};
			if($("input[name='wpSection']").attr("value") !== "")
			{
				requestData.rvsection = $("input[name='wpSection']").attr("value");
			}
			$.post(requestString, requestData, function(response, status)
			{
				$("#ajaxDiff").append(response.query.pages[Object.keys(response.query.pages)[0]].revisions[0].diff["*"]);
			});
		}
		$("#wpDiff").after('<input type="submit" title="Preview your changes and show a diff of the changes you made; please use this before saving." value="Show preview and diff" tabindex="8" name="wpPreview" id="wpPreviewAndDiff">');
		$("#wpPreviewAndDiff").click(function()
		{
			var formAction = $("#editform").attr("action");
			$("#editform").attr("action", formAction+"&previewanddiff=true");
		});
	}
});