Jump to content

User:Ieditrandomarticles/autobox.js

From Simple English Wikipedia, the free encyclopedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
    if (mw.config.get("wgNamespaceNumber") < 0) return;

    var now = new Date();
    var monthNames = [
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    ];
    var currentMonthYear = monthNames[now.getMonth()] + " " + now.getFullYear();

    var cleanupTags = [
        "cleanup", "copy edit", "complex", "uncategorized", "orphan", "wikify",
        "update", "refimprove", "onesource", "no sources", "inuse", "cleanup rewrite",
        "notability", "original research", "more footnotes", "lead too short",
        "advert", "tone", "peacock", "technical", "sections", "context", "plot",
        "disputed", "hoax", "inappropriate person", "POV", "globalize", "external links",
        "self-published", "restructure", "news release", "autobiography", "essay-like",
        "trivia", "dead end", "prose", "undisclosed paid", "inline", "redlinks"
    ];

    var stubTags = [
        "Stub", "Geo-stub", "France-geo-stub", "US-geo-stub", "Asia-stub", "Japan-stub",
        "Japan-sports-bio-stub", "Europe-stub", "Switzerland-stub", "UK-stub", "North-America-stub",
        "Canada-stub", "US-stub", "US-actor-stub", "US-bio-stub", "Food-stub", "History-stub",
        "Military-stub", "Politics-stub", "Religion-stub", "Transport-stub", "Actor-stub", 
        "Bio-stub", "Sports-bio-stub", "Sci-stub", "Biology-stub", "Chem-stub", "Math-stub", 
        "Med-stub", "Physics-stub", "Weather-stub", "Sport-stub", "Tech-stub", 
        "Video-game-stub", "Performing-arts-stub", "Movie-stub", "Music-stub", 
        "TV-stub", "Lit-stub"
    ];

    function createBox() {
        var box = document.createElement("div");
        box.id = "autobox";
        box.style.position = "fixed";
        box.style.bottom = "20px";
        box.style.right = "20px";
        box.style.zIndex = "9999";
        box.style.background = "#fdfdfd";
        box.style.border = "1px solid #ccc";
        box.style.borderRadius = "10px";
        box.style.padding = "12px";
        box.style.maxHeight = "80vh";
        box.style.width = "260px";
        box.style.overflow = "hidden";
        box.style.transition = "transform 0.3s ease, opacity 0.3s ease, width 0.3s ease";
        box.style.transformOrigin = "bottom right";
        box.style.boxShadow = "0 4px 12px rgba(0,0,0,0.1)";
        box.style.fontFamily = "sans-serif";
        box.style.opacity = "1";
        box.style.transform = "scale(1)";

        var isMinimized = false;

        var minimizeBtn = document.createElement("button");
        minimizeBtn.textContent = "X";
        minimizeBtn.style.position = "absolute";
        minimizeBtn.style.top = "4px";
        minimizeBtn.style.right = "4px";
        minimizeBtn.style.border = "none";
        minimizeBtn.style.background = "transparent";
        minimizeBtn.style.cursor = "pointer";
        minimizeBtn.style.fontSize = "16px";
        minimizeBtn.style.fontWeight = "bold";
        minimizeBtn.title = "Minimize";

        var unminimizeBtn = document.createElement("button");
        unminimizeBtn.textContent = "📦";
        unminimizeBtn.style.position = "absolute";
        unminimizeBtn.style.top = "4px";
        unminimizeBtn.style.right = "4px";
        unminimizeBtn.style.border = "none";
        unminimizeBtn.style.background = "transparent";
        unminimizeBtn.style.cursor = "pointer";
        unminimizeBtn.style.fontWeight = "bold";
        unminimizeBtn.style.fontSize = "48px";
        unminimizeBtn.style.width = "64px";
        unminimizeBtn.style.height = "64px";
        unminimizeBtn.style.borderRadius = "50%";
        unminimizeBtn.style.display = "none";

        var contentWrapper = document.createElement("div");
        contentWrapper.id = "autobox-content";
        contentWrapper.style.transition = "opacity 0.2s ease";
        contentWrapper.style.overflowY = "auto";
        contentWrapper.style.maxHeight = "calc(80vh - 60px)";
        contentWrapper.style.scrollBehavior = "smooth";

        var header = document.createElement("div");
        header.innerHTML = "<strong>📦 Autobox</strong> <em><small>(v0.3)</small></em><br><small>Leave feedback on <a href='https://simple.wikipedia.org/wiki/User_talk:Ieditrandomarticles'>my talk page (Ieditrandomarticles)</a></small><br><br>";
        header.style.marginBottom = "6px";
        contentWrapper.appendChild(header);

        var selectedTags = [];
        var selectedTagTypes = {};
        var selectedBox = document.createElement("div");
        selectedBox.style.margin = "5px 0";
        selectedBox.style.fontSize = "90%";
        selectedBox.style.color = "#444";

        function renderSelected() {
        selectedBox.innerHTML = "<strong>Tags:</strong> " + selectedTags.join(", ");
        }

        function makeButton(label, tag, isStub) {
        var btn = document.createElement("button");
        btn.textContent = label;
        btn.style.margin = "3px";
        btn.style.padding = "5px 10px";
        btn.style.border = "1px solid #ccc";
        btn.style.borderRadius = "20px";
        btn.style.background = "#f0f0f0";
        btn.style.color = "#333";
        btn.style.fontSize = "90%";
        btn.style.cursor = "pointer";
        btn.style.transition = "all 0.2s";

        btn.onclick = function () {
            if (!selectedTags.includes(tag)) {
            selectedTags.push(tag);
            selectedTagTypes[tag] = isStub ? "stub" : "cleanup";
            btn.style.background = "#d6e0ff";
            btn.style.borderColor = "#6a5acd";
            renderSelected();
            }
        };

        return btn;
        }

        cleanupTags.forEach(function (tag) {
        contentWrapper.appendChild(makeButton(tag, tag, false));
        });

        var stubHeader = document.createElement("div");
        stubHeader.style.marginTop = "10px";
        stubHeader.style.marginBottom = "4px";
        stubHeader.innerHTML = "<strong>Stub Tags</strong>";
        contentWrapper.appendChild(stubHeader);

        stubTags.forEach(function (tag) {
        contentWrapper.appendChild(makeButton(tag, tag, true));
        });

        contentWrapper.appendChild(selectedBox);

        var go = document.createElement("button");
        go.textContent = "➕ Add tags";
        go.style.marginTop = "10px";
        go.style.padding = "7px 14px";
        go.style.border = "none";
        go.style.borderRadius = "6px";
        go.style.background = "#6a5acd";
        go.style.color = "#fff";
        go.style.fontWeight = "bold";
        go.style.fontSize = "90%";
        go.style.cursor = "pointer";
        go.style.width = "100%";
        go.style.boxShadow = "0 2px 6px rgba(0,0,0,0.1)";
        go.onmouseover = function () {
        go.style.background = "#5943c4";
        };
        go.onmouseout = function () {
        go.style.background = "#6a5acd";
        };
        go.onclick = function () {
        submitTags(selectedTags.slice(), selectedTagTypes);
        };

        contentWrapper.appendChild(go);
        box.appendChild(minimizeBtn);
        box.appendChild(unminimizeBtn);
        box.appendChild(contentWrapper);
        document.body.appendChild(box);

        minimizeBtn.onclick = function () {
        isMinimized = !isMinimized;
        if (isMinimized) {
            contentWrapper.style.display = "none";
            box.style.transform = "scale(0.25)";
            box.style.opacity = "0.8";
            box.style.width = "42px";
            minimizeBtn.style.display = "none";
            unminimizeBtn.style.display = "block";
        } else {
            contentWrapper.style.display = "block";
            box.style.transform = "scale(1)";
            box.style.opacity = "1";
            box.style.width = "260px";
            minimizeBtn.style.display = "block";
            unminimizeBtn.style.display = "none";
        }
        };

        unminimizeBtn.onclick = function () {
            contentWrapper.style.display = "block";
            box.style.transform = "scale(1)";
            box.style.opacity = "1";
            box.style.width = "260px";
            minimizeBtn.style.display = "block";
            unminimizeBtn.style.display = "none";
        };
    }

    function submitTags(tags, types) {
        var api = new mw.Api();
        api.get({
        action: "query",
        prop: "revisions",
        titles: mw.config.get("wgPageName"),
        rvprop: "content",
        formatversion: "2"
        }).done(function (data) {
        var page = data.query.pages[0];
        if (!page || !page.revisions || !page.revisions.length) return;
        var content = page.revisions[0].content;
        var newText = content;

        tags.forEach(function (tag) {
            var template = "{{" + tag + "}}";
            if (types[tag] === "cleanup") {
            template = "{{" + tag + "|date=" + currentMonthYear + "}}";
            newText = template + "\n" + newText;
            } else {
            newText = newText.trim() + "\n" + template;
            }
        });

        var summary = tags.length === 1
            ? "added {{" + tags[0] + "}} with [[User:Ieditrandomarticles/Autobox|📦 Autobox]]"
            : "added multiple cleanup tags with [[User:Ieditrandomarticles/Autobox|📦 Autobox]]";

        api.postWithEditToken({
            action: "edit",
            title: mw.config.get("wgPageName"),
            text: newText,
            summary: summary
        }).done(function () {
            location.reload();
        }).fail(function (err) {
            console.error("📦 API error during edit:", err);
        });
        });
    }

    mw.loader.using(["mediawiki.api", "mediawiki.util"], function () {
        createBox();
    });
})();