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:59, 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 Map(); // key: title, value: { url, element }
        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, revid) {
            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" })
            );

            notifDropdown.prepend(item);
            notifCount.text(++count).show();

            seen.set(title, { url: url, revid: revid, element: item });

            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&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 revid = page.revisions[0].revid;
                                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);
                                }
                            });
                        });
                })
                .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.title)
                        ) {
                            var title = change.title;
                            var url = "https://en.wikipedia.org/w/index.php?title=" + encodeURIComponent(title) + "&diff=" + change.revid + "&oldid=prev";
                            addNotification(title, url, change.revid);
                        }
                    }
                })
                .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();
});