User:Veko/common.js
Appearance
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. |
![]() | The accompanying .css page for this skin can be added at User:Veko/common.css. |
mw.loader.using(['jquery', 'mediawiki.util']).then(function () {
function startPendingChecker() {
if (mw.config.get("wgDBname") !== "enwiki") return;
var seen = new Map();
var count = 0;
var baseInterval = 30000;
var minDelay = 5000;
var localKey = "pendingChangesSeen";
var seenStore = new Set(JSON.parse(localStorage.getItem(localKey) || "[]"));
function updateStorage(revid) {
seenStore.add(revid);
if (seenStore.size > 500) {
seenStore = new Set(Array.from(seenStore).slice(-500));
}
localStorage.setItem(localKey, JSON.stringify(Array.from(seenStore)));
}
function formatTimestamp(date) {
const now = Date.now();
const diff = Math.floor((now - date) / 1000);
if (diff < 60) return `${diff}s ago`;
if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
return `${Math.floor(diff / 86400)}d ago`;
}
// Use the existing notification UI
var toolbar = document.querySelector('#pt-notifications-alert, .mw-echo-notifications-badge');
if (!toolbar) return;
var pendingBell = document.createElement("span");
pendingBell.id = "pending-alert-bell";
pendingBell.style.cssText = "position: relative; margin-left: 10px; cursor: pointer;";
pendingBell.innerHTML = `<span class='mw-echo-notifications-badgeItem' style='background:#d33;color:#fff;padding:2px 6px;border-radius:10px;font-size:11px;display:none;' id='pending-alert-count'>0</span>`;
toolbar.parentElement.appendChild(pendingBell);
var notifDropdown = $("<div>").css({
display: "none",
position: "absolute",
top: "30px",
right: "0",
width: "320px",
maxHeight: "400px",
overflowY: "auto",
background: "#fff",
border: "1px solid #ccc",
borderRadius: "6px",
boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
padding: "10px",
zIndex: 9999
});
$(pendingBell).on("click", function () {
notifDropdown.toggle();
});
var notifCount = $("#pending-alert-count");
var clearBtn = $('<div>').text("Clear seen history").css({
textAlign: "center",
margin: "10px 0",
cursor: "pointer",
fontSize: "12px",
color: "#0645ad",
textDecoration: "underline"
}).click(function () {
seenStore.clear();
localStorage.removeItem(localKey);
location.reload();
});
$("body").append(notifDropdown);
function addNotification(title, url, revid, timestamp) {
if (seenStore.has(revid)) return;
var timeAgo = formatTimestamp(new Date(timestamp));
var item = $("<div>").css({
marginBottom: "8px",
paddingBottom: "8px",
borderBottom: "1px solid #eee"
}).append(
$("<a>")
.attr("href", url)
.attr("target", "_blank")
.text(title)
.css({ fontWeight: "bold", color: "#0645ad" }),
$("<div>").text(timeAgo).css({ fontSize: "10px", color: "#666" })
);
notifDropdown.prepend(item);
notifDropdown.append(clearBtn);
notifCount.text(++count).show();
seen.set(title, { url: url, revid: revid, element: item });
updateStorage(revid);
mw.notify(
$("<a>")
.attr("href", url)
.attr("target", "_blank")
.text("Pending: " + title)[0],
{
title: "Pending Change Alert",
autoHide: false
}
);
}
function cleanupReviewed() {
if (seen.size === 0) return;
var titles = Array.from(seen.keys());
var url = "https://en.wikipedia.org/w/api.php?action=query&prop=flagged&titles=" + encodeURIComponent(titles.join("|")) + "&format=json&origin=*";
fetch(url)
.then(function (res) { return res.json(); })
.then(function (data) {
var pages = data.query.pages;
for (var id in pages) {
var page = pages[id];
if (!page.flagged || page.flagged.revid >= seen.get(page.title).revid) {
var info = seen.get(page.title);
if (info) {
info.element.remove();
seen.delete(page.title);
count--;
}
}
}
notifCount.text(count);
if (count === 0) notifCount.hide();
});
}
function fetchExistingPending() {
var url = "https://en.wikipedia.org/w/api.php?action=query&list=oldreviewedpages&ornamespace=0&orlimit=50&format=json&origin=*";
fetch(url)
.then(function (res) { return res.json(); })
.then(function (data) {
var pages = (data && data.query && data.query.oldreviewedpages) || [];
var titles = pages.map(function (page) { return page.title; });
if (titles.length === 0) return;
var revUrl = "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=ids|timestamp&titles=" + encodeURIComponent(titles.join("|")) + "&format=json&origin=*";
fetch(revUrl)
.then(function (res) { return res.json(); })
.then(function (data2) {
var pagesData = data2.query.pages;
Object.keys(pagesData).forEach(function (pageId) {
var page = pagesData[pageId];
if (!page.revisions || !page.revisions[0]) return;
var rev = page.revisions[0];
var revid = rev.revid;
var timestamp = rev.timestamp;
var title = page.title;
var url = "https://en.wikipedia.org/w/index.php?title=" + encodeURIComponent(title) + "&diff=" + revid + "&oldid=prev";
if (!seen.has(title)) {
addNotification(title, url, revid, timestamp);
}
});
});
})
.catch(function (err) {
console.error("Error loading oldreviewedpages:", err);
});
}
function checkRecentChanges() {
var start = performance.now();
var url = "https://en.wikipedia.org/w/api.php?action=query&list=recentchanges&rcprop=title|ids|tags|timestamp&rclimit=50&rcshow=!bot&format=json&origin=*";
fetch(url)
.then(function (res) { return res.json(); })
.then(function (data) {
var changes = (data && data.query && data.query.recentchanges) || [];
for (var i = 0; i < changes.length; i++) {
var change = changes[i];
if (
change.tags &&
change.tags.indexOf("flaggedrevs-pending") !== -1 &&
!seen.has(change.title)
) {
var title = change.title;
var revid = change.revid;
var timestamp = change.timestamp;
var url = "https://en.wikipedia.org/w/index.php?title=" + encodeURIComponent(title) + "&diff=" + revid + "&oldid=prev";
addNotification(title, url, revid, timestamp);
}
}
})
.catch(function (e) {
console.error("Pending changes check failed:", e);
})
.finally(function () {
var duration = performance.now() - start;
var delay = Math.max(minDelay, baseInterval - duration);
setTimeout(checkRecentChanges, delay);
setTimeout(cleanupReviewed, 10000);
});
}
fetchExistingPending();
checkRecentChanges();
}
startPendingChecker();
});