Jump to content

User:Xiplus/js/templates-last-edit.js

From Meta, a Wikimedia project coordination wiki

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function() {

	if (mw.config.get('wgAction') !== 'info') {
		return;
	}

	function main() {
		$('#template-last-edit').remove();

		var api = new mw.Api();

		var templates = [], elmap = [];
		mw.loader.using(['mediawiki.api'], function() {
			$('#mw-pageinfo-templates li>a:first-child').each(function(i, e) {
				var title = $(e).text();
				templates.push(title);
				elmap[title] = e;
			});
		});

		while (templates.length) {
			var chunks = templates.splice(0, 50);
			api.get({
				"action": "query",
				"format": "json",
				"prop": "revisions",
				"titles": chunks.join('|'),
				"rvprop": "timestamp"
			}).done(function(data) {
				for (const pageid in data.query.pages) {
					const page = data.query.pages[pageid];
					if (elmap.hasOwnProperty(page.title)) {
						var timestamp = page.revisions[0].timestamp;
						timestamp = timestamp.replace(/^(\d+-\d+-\d+)T(\d+:\d+):\d+Z$/, '$1 $2');
						$(elmap[page.title]).parent().append(timestamp);
					}
				}
			})
		}
	}

	$('<br><button id="template-last-edit">顯示最近編輯日期</button>').on('click', main).appendTo($('#mw-pageinfo-templates>:first-child'));

})();