User:Evad37/TimestampDiffs.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Evad37/TimestampDiffs. |
/***************************************************************************************************
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>