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 03:55, 12 April 2025 (fixed). 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 Set();
        var count = 0;
        var baseInterval = 30000;
        var minDelay = 5000;

        var notifContainer = $('<div>').css({
            position: "fixed",
            top: "0.5em",
            right: "3em",
            zIndex: 9999,
            fontFamily: "sans-serif"
        });

        var notifIcon = $('<span>').text("🔔").css({
            cursor: "pointer",
            fontSize: "16px",
            background: "#fff",
            border: "1px solid #ccc",
            borderRadius: "50%",
            padding: "4px 8px",
            fontWeight: "bold",
            position: "relative",
            boxShadow: "0 1px 4px rgba(0,0,0,0.1)"
        });

        var notifCount = $('<span>').text("0").css({
            position: "absolute",
            top: "-6px",
            right: "-6px",
            background: "#d33",
            color: "#fff",
            borderRadius: "50%",
            padding: "2px 6px",
            fontSize: "10px",
            display: "none"
        });

        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"
        });

        notifIcon.on("click", function () {
            notifDropdown.toggle();
        });

        notifIcon.append(notifCount);
        notifContainer.append(notifIcon).append(notifDropdown);
        $("body").append(notifContainer);

        function addNotification(title, url) {
            notifDropdown.prepend(
                $("<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" })
                )
            );

            notifCount.text(++count).show();

            mw.notify(
                $("<a>")
                    .attr("href", url)
                    .attr("target", "_blank")
                    .text("Pending: " + title)[0],
                {
                    title: "Pending Change Alert",
                    autoHide: false
                }
            );
        }

        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) || [];
                    for (var i = 0; i < pages.length; i++) {
                        var title = pages[i].title;
                        var url = "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_"));
                        if (!seen.has(title)) {
                            seen.add(title);
                            addNotification(title, url);
                        }
                    }
                })
                .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&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.revid)
                        ) {
                            seen.add(change.revid);
                            var title = change.title;
                            var url = "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_"));
                            addNotification(title, url);
                        }
                    }
                })
                .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);
                });
        }

        // Load initial list + begin polling
        fetchExistingPending();
        checkRecentChanges();
    }

    startPendingChecker();
});