User:Mike Dillon/Scripts/recentpages.js: Difference between revisions

Content deleted Content added
No edit summary
m Maintenance: Replacing addOnloadHook with native jQuery (mw:ResourceLoader/Migration_guide_(users)#addOnloadHook - phab:T130879)
 
(17 intermediate revisions by one other user not shown)
Line 1:
// Requires: [[User:Mike Dillon/Scripts/i18n.js]], [[User:Mike Dillon/Scripts/easydom.js]], [[User:Mike Dillon/Scripts/cookies.js]]
 
/* <pre><nowiki> */
Line 5:
var pageHistoryCookieName;
var pageHistoryCookieItemCount;
var pageHistoryBeforePorlet;
var pageHistoryExpiresInDays;
 
/* Messages */
addOnloadHook(function () {
// 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");
 
addOnloadHook$(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("Page historypageHistoryTitle")));
var historyList = ul();
var historyPBody = div({ "class": "pBody" }, historyList);
historyPortlet.appendChild(historyPBody);
}
 
// Insert portlet before toolbox
var toolboxPortletbeforePortlet = document.getElementById("p-tb"pageHistoryBeforePorlet);
document.getElementById("column-one").insertBefore(historyPortlet, beforePortlet);
if (!toolboxPortlet) throw "Could not find toolbox portlet";
toolboxPortlet.parentNode.insertBefore(historyPortlet, toolboxPortlet);
 
// Extract previous history from cookie
Line 36 ⟶ 56:
 
// Prepend the current page to the list, remove duplicates, and control item count
if (wgIsArticlewgArticleId > 0) {
historyItems.unshift(wgPageName);
for (var n = 1; n < historyItems.length; n++) {
Line 43 ⟶ 63:
}
}
whileif (historyItems.lengthpageHistoryCookieItemCount > pageHistoryCookieItemCount0) {
while (historyItems.pop(length > pageHistoryCookieItemCount); {
historyItems.pop();
}
}
}
Line 55 ⟶ 77:
}
with (easyDom) {
historyList.appendChild(li(em(wfMsg("No page historynoPageHistoryText"))));
}
deleteCookie(pageHistoryCookieName, { path: "/" });
Line 64 ⟶ 86:
with (easyDom) {
if (historyItems.length == 0) {
historyList.appendChild(li(em(wfMsg("No page historynoPageHistoryText"))));
} else {
for (var n in historyItems) {
Line 73 ⟶ 95:
 
var itemLabel = historyItems[n].replace(/_/g, " ");
var// itemSlashAdd =zero-width itemLabel.lastIndexOf("/");spaces before some punctuation
if (itemSlash != -1) itemLabel = itemLabel.substrreplace(itemSlash +/([\/(])/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": "#" }, em("cleartitle": historywfMsg("clearHistoryTitle")); },
em(wfMsg("clearHistoryLabel")));
clearHistoryLink.onclick = clearHistory;
historyPBody.appendChild(div(
{ "style": "padding-top: 1em; text-align: right" }, clearHistoryLink));
}
}
Line 90 ⟶ 115:
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 += encodeURIComponent(historyItems[n])encodedItem;
}
writeCookie(pageHistoryCookieName, historyCookie, { path: "/" });
{ "path": "/", "expiresInDays": pageHistoryExpiresInDays });
});