User:Mike Dillon/Scripts/recentpages.js
Appearance
< User:Mike Dillon | Scripts
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. |
![]() | Documentation for this user script can be added at User:Mike Dillon/Scripts/recentpages. |
// Requires: [[User:Mike Dillon/Scripts/i18n.js]], [[User:Mike Dillon/Scripts/easydom.js]], [[User:Mike Dillon/Scripts/cookies.js]]
/* <pre><nowiki> */
var pageHistoryCookieName;
var pageHistoryCookieItemCount;
var pageHistoryBeforePorlet;
var pageHistoryExpiresInDays;
/* Messages */
// pageHistoryTitle: title of page history portlet
wfAddMsg("en", "pageHistoryTitle", "Page history");
wfAddMsg("es", "pageHistoryTitle", "Historial de páginas");
// clearHistoryLabel: label of the "clear history" link
wfAddMsg("en", "clearHistoryLabel", "clear history");
wfAddMsg("es", "clearHistoryLabel", "borrar historial");
// clearHistoryTitle: tooltip of the "clear history" link
wfAddMsg("en", "clearHistoryTitle", "Clear page history");
wfAddMsg("es", "clearHistoryTitle", "Borrar historial de páginas");
// noPageHistoryText: text to display when there is no page history
wfAddMsg("en", "noPageHistoryText", "No page history");
wfAddMsg("es", "noPageHistoryText", "Ningún historial de páginas");
$(function () {
// Set defaults for variables
if (!pageHistoryCookieName) pageHistoryCookieName = "pageHistory";
if (!pageHistoryCookieItemCount) pageHistoryCookieItemCount = 10;
if (!pageHistoryBeforePorlet) pageHistoryBeforePorlet = null;
if (!pageHistoryExpiresInDays) pageHistoryExpiresInDays = 0;
// Create portlet
with (easyDom) {
var historyPortlet = div({ "class": "portlet", "id": "p-history" },
h5(wfMsg("pageHistoryTitle")));
var historyList = ul();
var historyPBody = div({ "class": "pBody" }, historyList);
historyPortlet.appendChild(historyPBody);
}
// Insert portlet
var beforePortlet = document.getElementById(pageHistoryBeforePorlet);
document.getElementById("column-one").insertBefore(historyPortlet, beforePortlet);
// 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 (wgArticleId > 0) {
historyItems.unshift(wgPageName);
for (var n = 1; n < historyItems.length; n++) {
if (historyItems[n] == wgPageName) {
historyItems.splice(n, 1);
}
}
if (pageHistoryCookieItemCount > 0) {
while (historyItems.length > pageHistoryCookieItemCount) {
historyItems.pop();
}
}
}
// History clearing
var clearHistory = function () {
historyItems = [];
while (historyList.firstChild) {
historyList.removeChild(historyList.firstChild);
}
with (easyDom) {
historyList.appendChild(li(em(wfMsg("noPageHistoryText"))));
}
deleteCookie(pageHistoryCookieName, { path: "/" });
return false;
};
// Build out the history list
with (easyDom) {
if (historyItems.length == 0) {
historyList.appendChild(li(em(wfMsg("noPageHistoryText"))));
} 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, " ");
// Add zero-width spaces before some punctuation
itemLabel = itemLabel.replace(/([\/(])/g, "\u200B$1");
// Add zero-width spaces after other punctuation
itemLabel = itemLabel.replace(/([:;,.)-])/g, "$1\u200B");
historyList.appendChild(li(
a({ "href": itemUrl, "title": historyItems[n] }, itemLabel)));
}
var clearHistoryLink = a({ "href": "#", "title": wfMsg("clearHistoryTitle") },
em(wfMsg("clearHistoryLabel")));
clearHistoryLink.onclick = clearHistory;
historyPBody.appendChild(div(
{ "style": "padding-top: 1em; text-align: right" }, clearHistoryLink));
}
}
// Write out the updated cookie
historyCookie = "";
for (var n in historyItems) {
var encodedItem = encodeURIComponent(historyItems[n]);
// Limit cookie value size to 4000 bytes
if (historyCookie.length + encodedItem.length + 1 > 4000) break;
if (n > 0) historyCookie += ",";
historyCookie += encodedItem;
}
writeCookie(pageHistoryCookieName, historyCookie,
{ "path": "/", "expiresInDays": pageHistoryExpiresInDays });
});
/* </nowiki></pre> */