MediaWiki:Gadget-edit0.js:修订间差异
外观
小无编辑摘要 |
小无编辑摘要 |
||
| 第1行: | 第1行: | ||
// [[en:MediaWiki:Gadget-edittop.js]] | // [[en:MediaWiki:Gadget-edittop.js]] | ||
// | // Verified spelling/grammar - Last update: 2024-03-20 | ||
/ | |||
(function($, mw) { | (function($, mw) { | ||
"use strict"; | |||
// 1. | // [1] Early exit conditions. | ||
if ( | |||
mw.config.get("wgAction") !== "view" || | |||
mw.config.get("wgNamespaceNumber") < 0 || | |||
document.querySelectorAll(".cloned-top-edit-button").length > 0 | |||
) { | |||
return; | return; | ||
} | } | ||
// 2. | // [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 | 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 $ | 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); | ||
console. | } 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); | })(jQuery, mw); | ||
2025年11月2日 (日) 20:13的版本
// [[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);