User:Opencooper/lastEdit.js

// This script reimplements the last edit banner used on mobile
// Namespace # reference: https://www.mediawiki.org/wiki/Manual:Namespace#Built-in_namespaces

// 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
    var apiUrl = location.origin + "/w/api.php";
    $.ajax({
        url: apiUrl,
        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");
    try {
      var pageInfo = response.query.pages[pageId].revisions[0];
    } catch (e) {
      return;
    }
    var relativeTimestamp = calculateRelativeTimestamp(pageInfo.timestamp);

    var editComment = pageInfo.comment;
    if (!editComment) {
        editComment = "[No edit summary]";
    }
    editComment = editComment.replace(/'/g, "'"); // HTML encode quotes
    var usernameEncoded = encodeURIComponent(pageInfo.user).replace(/'/g, "'");
    var message = "Last edited ";
    if (mw.config.get("wgNamespaceNumber") == 2) { // User namespace
    	message = "User page last edited ";
    }

    var lastEdit = "<a href='/wiki/Special:Diff/" + pageInfo.revid
                   + "' title='" + editComment + "'> " + message
                   + relativeTimestamp + " ago</a>";
    var lastEditor = "<a href='/wiki/Special:Contributions/" + usernameEncoded
                     + "'>" + 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);
}

// Adapted from https://github.com/wikimedia/mediawiki-extensions-MobileFrontend/blob/master/resources/mobile.modifiedBar/time.js
function calculateRelativeTimestamp(timestamp) {
    // Supposedly Date parsing is a bit wonky with respect to browsers and
    // timezones, but since we have an ISO-6801 date we can risk it
    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;
}

// 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
        && mw.config.get("wgNamespaceNumber") == 0) {
        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);
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.