User:Terasail/ReadableDiff.js

From Wikifunctions

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*<nowiki>
	ReadableDiff.js
	Created by: Terasail
*/
// jshint esnext: false, esversion: 8
let wgcNamespace = mw.config.get("wgCanonicalNamespace");
if ((mw.config.get("wgDiffNewId") != null && (wgcNamespace == "" || wgcNamespace == "MobileDiff")) || (mw.config.get("wgCanonicalSpecialPageName") == "Undelete" && window.location.toString().search("diff=") != -1)) {
	let diff = $('.diff')[0].innerText;
	let seen = {};
	let zIds = diff.match(/Z\d+/g).filter(function(item) {
		return seen.hasOwnProperty(item) ? false : (seen[item] = true);
	});
	let kIds = diff.match(/Z\d+K\d+/g);
	if (kIds != null) {
		kIds.filter(function(item) {
			return seen.hasOwnProperty(item) ? false : (seen[item] = true);
		});
	}
	if (zIds != null && zIds.length <= 50) {
		if (kIds == null) {
			kIds = [];
		}
		ReadableDiff(zIds, kIds);
	} else {
		console.error("Invalid number of zObjects");
		console.error(zIds);
	}
}

async function ReadableDiff(zIds, kIds) {
	var api = new mw.Api();
	await api.loadMessages('Wikilambda-multilingualstring-nofallback');
	let nofallback = mw.msg('Wikilambda-multilingualstring-nofallback');
	let pages = await new Promise(function(resolve) {
		api.get({
			action: "query",
			prop: "info",
			titles: zIds,
			format: "json",
			formatversion: "2"
		}).done(function(data) {
			resolve(data.query.pages);
		}).fail(function(data, error) {
			console.error(data, error);
		});
	});
	let validPages = [];
	pages.forEach(function(object) { if (!object.missing) { validPages.push(object.title); }});
	let objects = await new Promise(function(resolve) {
		api.get({
			action: "wikilambda_fetch",
			language: mw.config.get("wgUserLanguage"),
			zids: validPages,
			format: "json",
			formatversion: "2"
		}).done(function(data) {
			resolve(Object.entries(data));
		}).fail(function(data, error) {
			console.error(data, error);
		});
	});
	let objLabels = [];
	objects.forEach(function(objectArr) {
		let object = JSON.parse(objectArr[1].wikilambda_fetch.replaceAll(/\n */g, ""));
		if (object.Z1K1 == "Z2") {
			let objLabel = object.Z2K3.Z12K1[1].Z11K2;
			let objId = object.Z2K1.Z6K1;
			let pObj = object.Z2K2;
			let objType = pObj.Z1K1;
			if (objType == "Z4" || objType == "Z8") {
				let keys = pObj.Z4K2 || pObj.Z8K1;
				kIds.forEach(function(keyId) {
					if (keyId.replace(/K\d+/, "") == objId) {
						let keyObjects = keys[keyId.replace(/Z\d+K/, "")];
						let keyLabels = keyObjects.Z3K3 || keyObjects.Z17K3;
						keyLabels.Z12K1.forEach(function(label) {
							if (label.Z11K1 == object.Z2K3.Z12K1[1].Z11K1) {
								objLabels.push([keyId, label.Z11K2 + " ⟨" + objId + "⟩"]);
							}
						});
					}
				});
			}
			if (objLabel != nofallback) {
				objLabels.push([objId, objLabel]);
			}
		}
	});
	let diff = $('.diff')[0];
	let diffHTML = diff.innerHTML.replaceAll("\\n", "<br>").replaceAll("\\t", "&nbsp;");
	objLabels.forEach(function(label) {
		let regex = new RegExp(label[0] + '(?![K\\d⟩])', "g");
		diffHTML = diffHTML.replaceAll(regex, label[1]);
	});
	diff.innerHTML = diffHTML;
}
//</nowiki>