Jump to content

User:Opencooper/lastEdit.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Opencooper (talk | contribs) at 06:16, 2 September 2019 (rm). 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.
// This script reimplements the last edit banner used on mobile

// License: CC0

function requestRevision() {
    // If we're not reading an article, do nothing
    if (!(mw.config.get("wgAction") === "view"
          && mw.config.get("wgIsArticle")
          && !mw.config.get("wgIsMainPage"))) {
        return;
    }

    // API docs: https://www.mediawiki.org/wiki/API:Revisions
    $.ajax({
        url: "https://en.wikipedia.org/w/api.php",
        data: {
            action: "query",
            prop: "revisions",
            format: "json",
            titles: mw.config.get("wgPageName"),
            rvprop: "timestamp|user|comment|tags|ids"
        },
        success: parseEdit
    });
}

function parseEdit(response) {
    var pageId = mw.config.get("wgArticleId");
    var pageInfo = response.query.pages[pageId].revisions[0];
    // Relies on moment.js. See script history for an inline implementation
    var relativeTimestamp = moment(pageInfo.timestamp).fromNow();

    var editComment = pageInfo.comment;
    if (!editComment) {
        editComment = "[No edit summary]";
    }
    editComment = editComment.replace(/'/g, "'"); // HTML encode quotes

    var lastEdit = "<a href='/wiki/Special:Diff/" + pageInfo.revid
                   + "' title='" + editComment + "'> Last edited "
                   + relativeTimestamp + "</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 noticeText = lastEdit + " by " + lastEditor + pageTags;
    var notice = "<div id='lastEdit' style='float: right;'>" + noticeText
                 + "</div>"

    displayEdit(notice);
}

// Display the last edit info to the right of the site subhead
function displayEdit(notice) {
    // [[MediaWiki:Gadget-metadata.js]] replaces the siteSub element so wait
    // for it to run first
    // Check if script is enabled and if it hasn't ran already
    if ($("script").text().search("ext.gadget.metadata") != -1
        && !$(".assess-article-rating").length) {
        var target = document.querySelector("#siteSub");
        var observer = new MutationObserver(function(mutations) { // IE 11+
            $("#siteSub").append(notice);
            observer.disconnect();
        });

        observer.observe(target, {childList: true});
    } else {
        $("#siteSub").append(notice);
    }

    // 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);
}

if (mw.config.get( 'skin' ) != "minerva") {
    $(requestRevision);
}