跳转到内容

MediaWiki:Gadget-edit0.js

勤求古训,博采众方
鹿野耕云留言 | 贡献2025年11月2日 (日) 20:13的版本

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
// [[en:MediaWiki:Gadget-edittop.js]]
// Verified spelling/grammar - Last update: 2024-03-20
(function($, mw) {
    "use strict";

    // [1] Early exit conditions.
    if (
        mw.config.get("wgAction") !== "view" || 
        mw.config.get("wgNamespaceNumber") < 0 ||
        document.querySelectorAll(".cloned-top-edit-button").length > 0
    ) {
        return;
    }

    // [2] Internationalization with fallbacks.
    const BUTTON_TEXTS = new Map([
        ["en", "Edit lead section"],
        ["zh", "编辑首段"],
        ["zh-hans", "编辑首段"],
        ["zh-cn", "编辑首段"],
        ["zh-tw", "編輯首段"],
        ["ja", "はじめの編集"]
    ]);

    const getLocalizedText = (langCode) => {
        return BUTTON_TEXTS.get(langCode) ?? 
               BUTTON_TEXTS.get(langCode.split("-")[0]) ?? 
               BUTTON_TEXTS.get("en");
    };

    // [3] Modern URL builder.
    const buildEditUrl = () => {
        const params = new Map([
            ["title", mw.config.get("wgPageName")],
            ["section", "0"],
            ["summary", "/* top */"]
        ]);

        const isVE = (
            mw.config.get("wgVisualEditor")?.isVisualEditor ??
            document.querySelector(".ve-edit-toolbar") !== null ??
            false
        );
        params.set(isVE ? "veaction" : "action", "edit");

        return `${mw.config.get("wgScript")}?${new URLSearchParams([...params]).toString()}`;
    };

    // [4] DOM operations.
    const initializeButton = () => {
        try {
            const $editSection = $(".mw-editsection:first,.mw-editsection-last:first");
            if ($editSection.length === 0) {
                throw new Error("No editable section found.");
            }

            const $button = $editSection.clone()
                .removeAttr("id")
                .find("[id]").removeAttr("id").end()
                .addClass("cloned-top-edit-button")
                .attr("aria-label", getLocalizedText(mw.config.get("wgUserLanguage")));

            $button.find("a")
                .attr({
                    href: buildEditUrl(),
                    title: getLocalizedText(mw.config.get("wgUserLanguage"))
                })
                .text(getLocalizedText(mw.config.get("wgUserLanguage")));

            const $heading = $([
                "#firstHeading",
                ".mw-first-heading",
                ".page-heading",
                ".firstHeading",
                "#section_0 h1"
            ].join(",")).first();

            if ($heading.length) {
                $heading.append(
                    $("<span>").addClass("mw-editsection").append($button)
                );
                mw.hook("wikipage.editform").fire($button);
            } else {
                throw new Error("No suitable heading found.");
            }
        } catch (e) {
            console.error("EditTopButton failed:", e);
            mw.track("gadget.edittop.error", e.message);
        }
    };

    // [5] Execution flow.
    if (document.readyState === "complete") {
        initializeButton();
    } else {
        $(document).one("ready mw.loader.ready", initializeButton);
    }
})(jQuery, mw);