Jump to content

User:Tom.Reding/MoreDiffInfo.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/*** More Diff Info ***/

// Adds more information to diff pages such as revision ID, size, and ORES score
// Documentation at [[en:w:User:BrandonXLF/MoreDiffInfo]]
// By [[en:w:User:BrandonXLF]]

$.when(mw.loader.using('moment'), $.ready).then(function(require) {
	var moment = require('moment'),
		DA_IMG = '/media/wikipedia/commons/d/d1/Icon_no.png', // 178 bytes
		GF_IMG = '/media/wikipedia/commons/0/03/Green_check.svg'; // 314 bytes

	function createImage(type, url) {
		return '<img style="height:1em;vertical-align:text-top;" title="' + type + '" alt="' + type + '" src="' + url + '">';
	}

	function getInnerORES(scores) {
		var innerORES = [];

		if (scores.damaging) {
			innerORES.push(Math.round(scores.damaging.true * 100) + '%' + createImage('Damaging', DA_IMG) + ' | ');
		}

		if (scores.goodfaith) {
			innerORES.push(Math.round(scores.goodfaith.true * 100) + '%' + createImage('Good Faith', GF_IMG));
		}

		return innerORES.join(' ') || 'No ORES';
	}

	function generateInfo(revision, previousRevision) {
		var out = [
				revision.revid,
				revision.size.toLocaleString() +' bytes',
				getInnerORES(revision.oresscores),
			],
			help = '<a target="_blank" href="https://en.wikipedia.org/wiki/User:BrandonXLF/MoreDiffInfo#Guide">(?)</a>';

		if (previousRevision) {
			var diff = revision.size - previousRevision.size;

			out[1] += ' <span style="color:' + (diff < 0 ? '#8b0000' : diff > 0 ? '#006400' : '') + '">(' + (diff > 0 ? '+' : '') + diff + ')</span>';

			out.push(moment(revision.timestamp).from(previousRevision.timestamp, true) + ' later');
		}

		return out.join(' | ') + ' ' + help;
	}

	mw.hook('wikipage.diff').add(function() {
		var ids = mw.config.get(['wgDiffOldId', 'wgDiffNewId']);

		if (!ids.wgDiffOldId || !ids.wgDiffNewId) return;

		new mw.Api().get({
			action: 'query',
			prop: 'revisions',
			revids: [ids.wgDiffOldId, ids.wgDiffNewId],
			rvprop: ['ids', 'size', 'oresscores', 'timestamp'],
			rvslots: 'main',
			formatversion: 2
		}).then(function(res) {
			var revisions = res.query.pages[0].revisions,
				oldRevision,
				newRevision;

			for (var i = 0; i < revisions.length; i++) {
				if (revisions[i].revid == ids.wgDiffOldId) {
					oldRevision = revisions[i];
				} else if (revisions[i].revid == ids.wgDiffNewId) {
					newRevision = revisions[i];
				}
			}

			if (!oldRevision || !newRevision) return;

			$('#mw-diff-otitle2').after($('<div></div>').append(generateInfo(oldRevision)));
			$('#mw-diff-ntitle2').after($('<div></div>').append(generateInfo(newRevision, oldRevision)));
		});
	});
});