User:Mike Dillon/Scripts/recentpages.js

This is an old revision of this page, as edited by Mike Dillon (talk | contribs) at 01:19, 22 April 2007. 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.
// Requires: [[User:Mike Dillon/Scripts/easydom.js]], [[User:Mike Dillon/Scripts/cookies.js]]

/* <pre><nowiki> */

var pageHistoryCookieName;
var pageHistoryCookieItemCount;

addOnloadHook(function () {
    // Set defaults for variables
    if (!pageHistoryCookieName) pageHistoryCookieName = "pageHistory";
    if (!pageHistoryCookieItemCount) pageHistoryCookieItemCount = 10;

    // Create portlet
    with (easyDom) {
        var historyPortlet = div({ class: "portlet", id: "p-history" },
            h5("Page history"));
        var historyList = ul();
        historyPortlet.appendChild(div({ class: "pBody" }, historyList));
    }

    // Insert portlet before toolbox
    var toolboxPortlet = document.getElementById("p-tb");
    if (!toolboxPortlet) throw "Could not find toolbox portlet";
    toolboxPortlet.parentNode.insertBefore(historyPortlet, toolboxPortlet);

    // Extract previous history from cookie
    var historyItems = [];
    var historyCookie = readCookie(pageHistoryCookieName);
    if (historyCookie) {
        var cookieItems = historyCookie.split(",");
        for (var n in cookieItems) {
            historyItems.push(decodeURIComponent(cookieItems[n]));
        }
    }

    // Prepend the current page to the list, remove duplicates, and control item count
    if (wgIsArticle) {
        historyItems.unshift(wgPageName);
        for (var n = 1; n < historyItems.length; n++) {
            if (historyItems[n] == wgPageName) {
                historyItems.splice(n, 1);
            }
        }
        while (historyItems.length > pageHistoryCookieItemCount) {
            historyItems.pop();
        }
    }

    // Build out the history list
    with (easyDom) {
        if (historyItems.length == 0) {
            historyList.appendChild(li(em("No page history")));
        } else {
            for (var n in historyItems) {
                var itemUrl = wgArticlePath.replace(/\$1/,
                     encodeURIComponent(historyItems[n]));
                itemUrl = itemUrl.replace(/%2f/gi, "/");
                itemUrl = itemUrl.replace(/%3a/gi, ":");

                var itemLabel = historyItems[n].replace(/_/g, " ");
                var itemSlash = itemLabel.lastIndexOf("/");
                if (itemSlash != -1) itemLabel = itemLabel.substr(itemSlash + 1);

                historyList.appendChild(li(
                    a({ href: itemUrl, title: historyItems[n] }, itemLabel)));
            }
        }
    }

    // Write out the updated cookie
    historyCookie = "";
    for (var n in historyItems) {
        if (n > 0) historyCookie += ",";
        historyCookie += encodeURIComponent(historyItems[n]);
    }
    writeCookie(pageHistoryCookieName, historyCookie, { path: "/" });
});

/* </nowiki></pre> */