User:Opencooper/lastEdit.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:Opencooper/lastEdit. |
// This script reimpliments the last edit banner used on mobile
// Note: The [[Wikipedia:Metadata gadget]] conflicts with this script,
// especially in Firefox. If it runs after this script it overwrites the
// inserted element.
// TODO: standardize on either single or double quotes
function requestRevision() {
// If we're not reading an article, do nothing
if (!(mw.config.get( 'wgAction' ) === 'view'
&& mw.config.get( 'wgIsArticle' )
&& mw.config.get("wgPageName") !== "Main_Page")) {
return;
}
// API docs: https://www.mediawiki.org/wiki/API:Revisions
$.ajax({
url: "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=json&titles="
+ mw.config.get('wgPageName')
+ "&rvprop=timestamp%7Cuser%7Ccomment%7Ctags%7Cids",
success: displayEdit
});
}
// Display the last edit info to the right of the site subhead
function displayEdit(response) {
var pageId = mw.config.get('wgArticleId');
var pageInfo = response.query.pages[pageId].revisions[0];
var relativeTimestamp = calculateRelativeTimestamp(pageInfo.timestamp);
var lastEdit = "<a href='/wiki/Special:Diff/" + pageInfo.revid + "' title='"
+ pageInfo.comment + "'>Last edited " + relativeTimestamp
+ " ago </a>";
var lastEditor = "<a href='/wiki/Special:Contributions/" + pageInfo.user
+ "'>" + pageInfo.user + "</a>";
// Can be filtered if needed
var pageTags = "";
if (pageInfo.tags.length > 0) {
pageTags = "<span class='mw-tag-markers'> (" + pageInfo.tags + ")</span>";
}
var notice = lastEdit + " by " + lastEditor + pageTags;
$('#siteSub').append("<div style='float: right;'>" + notice + "</div>");
// Unfortunately [[Template:Coords]] absolutely positions itself so we have
// to move it down so we don't get obscured
var sheet = window.document.styleSheets[0];
sheet.insertRule('#coordinates { top: 2em !important; }', sheet.cssRules.length); // IE 9+
}
// Adapted from https://github.com/wikimedia/mediawiki-extensions-MobileFrontend/blob/master/resources/mobile.modifiedBar/time.js
function calculateRelativeTimestamp(timestamp) {
var editTimestamp = new Date(timestamp).getTime() / 1000;
var currentTimestamp = Math.round( new Date().getTime() / 1000);
var timestampDelta = currentTimestamp - editTimestamp;
var units = [ 'second', 'minute', 'hour', 'day', 'month', 'year' ],
limits = [ 1, 60, 3600, 86400, 2592000, 31536000 ];
var i = 0;
while ( i < limits.length && timestampDelta > limits[i + 1] ) {
++i;
}
var delta = Math.round(timestampDelta / limits[i]);
var deltaUnit = units[i];
// Pluralize units
if (delta > 1) {
deltaUnit += "s";
}
return delta + " " + deltaUnit;
}
$(requestRevision);
// [[Category:Wikipedia scripts|lastEdit]]