Jump to content

User:Evad37/TimestampDiffs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 19:08, 29 August 2020 (New version that actually works). 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.
/***************************************************************************************************
 TimestampDiffs --- by Evad37
 > Links timestamp to diffs on discussion pages
----------------------------------------------------------------------------------------------------
***************************************************************************************************/
/* jshint laxbreak: true, undef: true, maxerr: 999*/
/* globals console, document, $, mw */
// <nowiki>
$.when(
	mw.loader.using(["mediawiki.api"]),
	$.ready
).then(function() {
	var config = {
		version: "0.0.1",
		mw: mw.config.get([
			"wgNamespaceNumber",
			"wgPageName"
		]),
		months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
	};
	// Only activate on talk page and project pages
	var isTalkPage = config.mw.wgNamespaceNumber > 0 && config.mw.wgNamespaceNumber%2 === 1;
	var isProjectPage = config.mw.wgNamespaceNumber === 4;
	if ( !isTalkPage && !isProjectPage ) {
		return;
	}

	mw.util.addCss(".tsdiffs-timestamp a { color:inherit; text-decoration: underline dotted #6495ED; }" );
	var api = new mw.Api( {
		ajax: {
			headers: { 
				"Api-User-Agent": "TimestampDiffs/" + config.version + 
					" ( https://en.wikipedia.org/wiki/User:Evad37/TimestampDiffs )"
			}
		}
	} );
	api.get({
		"action": "query",
		"format": "json",
		"prop": "revisions",
		"titles": config.mw.wgPageName,
		"formatversion": "2",
		"rvprop": "timestamp|user|comment|ids",
		"rvslots": "",
		"rvlimit": "max"
	}).then(function(response) {
		if (!response || !response.query || !response.query.pages || !response.query.pages[0] || !response.query.pages[0].revisions) {
			return $.Deferred().reject("API response did not contain any revisions");
		}
		var pageRevisions = response.query.pages[0].revisions.map(function(revision) {
			var date = new Date(revision.timestamp);
			var hours = ("0"+date.getUTCHours()).slice(-2);
			var minutes =  ("0"+date.getUTCMinutes()).slice(-2);
			var day = date.getUTCDate();
			var month = config.months[date.getUTCMonth()];
			var year = date.getUTCFullYear();
			revision.timestampText = hours + ":" + minutes + ", " + day + " " + month + " " + year + " (UTC)";
			return revision;
		});


		$(".mw-parser-output").html(
			$(".mw-parser-output").html().replace(/(\d{2}:\d{2}, \d{1,2} \w+ \d{4} \(UTC\))/g, "<span class='tsdiffs-timestamp'>$1</span>")
		);
		 // Firing hook allows scripts or gadgets like Navpops to update themselves if needed
		mw.hook( 'wikipage.content' ).fire( $(".mw-parser-output") );
		$(".tsdiffs-timestamp").each(function() {
			var timestamp = this.textContent;
			var revisions = pageRevisions.filter(function(revision) {
				return revision.timestampText === timestamp;
			});
			if (revisions.length) {
				var newerRevId = revisions[0].revid;
				var olderRevId = revisions[revisions.length-1].parentid || "prev";
				var comment = revisions.length === 1 ? revisions[0].comment : revisions.length + " edits";
				$(this).empty().append(
					$("<a>").attr({
						"href": "/wiki/Special:Diff/" + olderRevId + "/" + newerRevId,
						"title": "Diff (" + comment + ")"
					}).text(timestamp)
				);
			// } else {
			//		Could not find any revisions with this timestamp
			//		TODO: continue the initial query and try again
			}
		});
	}).catch(function(code, error) {
		mw.notify("Error: " + (code || "unknown"), {title:"TimestampDiffs failed to load"});
		console.warn("[TimestampDiffs] Error: " + (code || "unknown"), error)
	});
});
// </nowiki>