User:NguoiDungKhongDinhDanh/MoreDiffInfo.js

// For attribution: User:BrandonXLF/MoreDiffInfo.js // jshint esversion: 6

$(function {	mw.loader.using(['moment'], function(require) { var moment = require('moment'); function generateInfo(revision, previousRevision) { var divisor = $(' ').attr({				class: 'moreDiffInfo-divisor'			}).css({				'margin': '0 5px',				'border-left': '1px solid black',			}); var out = [ $(' ').attr({					class: 'moreDiffInfo-revid'				}).text(					revision.revid				), divisor.clone, $(' ').attr({					class: 'moreDiffInfo-size'				}).text(					revision.size.toLocaleString + ' bytes '				) ];			if (previousRevision) { var diff = revision.size - previousRevision.size; var color; switch (true) { case diff < 0: color = '#8b0000'; break; case diff > 0: color = '#006400'; break; default: color = '#000000'; break; }				out[2].append(					$(' ').attr({ class: 'moreDiffInfo-diff' }).css({ color: color }).text( '(' + (diff > 0 ? '+' : '') + diff + ')' )				);				out.push(					divisor.clone,					$(' ').attr({ class: 'moreDiffInfo-timestamp' }).text( moment(revision.timestamp).from(previousRevision.timestamp, true) + ' later' )				);			}			return out.map(e => e.prop('outerHTML')); }		function getids(difftable) { var ids = ['oldid', 'newid']; var ret = {}; for (let b of [0, 1]) { for (let i of ids) { if (!b) { ret[i] = $(difftable).find('#mw-diff-' + i[0] + 'title1 > strong > a').attr('href'); } else { ret[i] = (new mw.Uri(ret[i])).query.oldid; }				}			}			return [ret.oldid, ret.newid]; }		function init(difftable) { if ($(difftable).find('.moreDiffInfo').length) return; var ids = getids(difftable); if (!ids[0] || !ids[1]) return; (new mw.Api).get({				action: 'query',				prop: 'revisions',				revids: [ids[0], ids[1]],				rvprop: ['ids', 'size', 'timestamp'],				rvslots: 'main',				format: 'json',				formatversion: 2			}).then(function(response) {				var revisions = response.query.pages[0].revisions;				var oldRevision, newRevision;				for (var i = 0; i < revisions.length; i++) {					if (revisions[i].revid == ids[0]) {						oldRevision = revisions[i];					} else if (revisions[i].revid == ids[1]) {						newRevision = revisions[i];					}				}				if (!oldRevision || !newRevision) return;				$('#mw-diff-otitle2', difftable).after( $(' ').attr('class', 'moreDiffInfo moreDiffInfo-oldid').append(						generateInfo(oldRevision)					) );				$('#mw-diff-ntitle2', difftable).after( $(' ').attr('class', 'moreDiffInfo moreDiffInfo-newid').append(						generateInfo(newRevision, oldRevision)					) );			});		}		mw.hook('wikipage.diff').add(init); mw.hook('new-diff-table').add(init); mw.hook('diff-update').add(init); }); });