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:50, 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;

        let seen = new Set();
        let count = 0;
        const baseInterval = 30000;
        const minDelay = 5000;

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

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

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

        const 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 checkPending() {
            const start = performance.now();
            const 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(res => res.json())
                .then(data => {
                    const changes = data?.query?.recentchanges || [];

                    for (const change of changes) {
                        if (change.tags.includes("flaggedrevs-pending") && !seen.has(change.revid)) {
                            seen.add(change.revid);
                            count++;

                            const pageTitle = change.title;
                            const pageUrl = `https://en.wikipedia.org/wiki/${encodeURIComponent(pageTitle.replace(/ /g, "_"))}`;

                            notifDropdown.prepend(
                                $("<div>").css({
                                    marginBottom: "8px",
                                    paddingBottom: "8px",
                                    borderBottom: "1px solid #eee"
                                }).append(
                                    $("<a>")
                                        .attr("href", pageUrl)
                                        .attr("target", "_blank")
                                        .text(pageTitle)
                                        .css({ fontWeight: "bold", color: "#0645ad" })
                                )
                            );

                            notifCount.text(count).show();

                            mw.notify(
                                $("<a>")
                                    .attr("href", pageUrl)
                                    .attr("target", "_blank")
                                    .text("Pending: " + pageTitle)[0],
                                {
                                    title: "Pending Change Alert",
                                    autoHide: false
                                }
                            );
                        }
                    }
                })
                .catch(e => {
                    console.error("Pending changes check failed:", e);
                })
                .finally(() => {
                    const duration = performance.now() - start;
                    const delay = Math.max(minDelay, baseInterval - duration);
                    setTimeout(checkPending, delay);
                });
        }

        checkPending();
    }

    startPendingChecker();
});