Jump to content

User:Tollens/longCommenter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tollens (talk | contribs) at 23:35, 16 May 2024 (install instructions, description). 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.
/*
    To use this script, add the following line to your Special:MyPage/common.js:

    importScript("User:Tollens/longCommenter.js"); // Backlink: [[User:Tollens/longCommenter.js]]

    The button will be placed in the same dropdown menu as the "Move" button,
    which depending on the skin used could be called "More" or "Tools". It will
    only appear on article pages, and will not tag any page that is already
    256 bytes or larger.
*/


//<nowiki>
if (sessionStorage.getItem("justAddedLongComment") === "true") {
    mw.notify("Long comment tag added successfully", { type: "success" });
    sessionStorage.removeItem("justAddedLongComment");
}

$.when(mw.loader.using(["mediawiki.util"]), $.ready).done(function () {
    if (mw.config.get("wgNamespaceNumber") !== 0) return;

    const api = new mw.Api();
    const currentPage = mw.config.get("wgPageName");

    let button = mw.util.addPortletLink(
        "p-cactions",
        "#",
        "Add long comment tag",
        "ca-add-longcomment",
        "Add long comment tag to this page"
    );

    if (button) {
        button.addEventListener("click", async function () {
            const length = Object.values(
                (
                    await api.post({
                        action: "query",
                        prop: "revisions",
                        titles: currentPage,
                        rvprop: "size"
                    })
                ).query.pages
            )[0].revisions[0].size;

            if (length >= 256) {
                mw.notify(`Page too long (${length} bytes), will not tag`, { type: "warn" });
                return;
            }

            await api.postWithToken("csrf", {
                action: "edit",
                title: currentPage,
                summary: "Adding long comment tag to prevent legitimately short page from listing at [[Special:ShortPages]] (via [[User:Tollens/longCommenter.js|script]])",
                appendtext: "\n{{subst:long comment}}"
            });

            sessionStorage.setItem("justAddedLongComment", "true");

            window.location.reload();
        });
    }
});
//</nowiki>