Jump to content

User:Veko/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Veko (talk | contribs) at 04:08, 12 April 2025 (fixed RC alert). 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.
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();
});