User:C8H17OH/quick-save.js
注意:在您儲存之後您必須清除瀏覽器快取才可看到最新的變動。
- Firefox / Safari:按住「Shift」時點選「重新整理」,或按「Ctrl-F5」或「Ctrl-R」 (Mac則為「⌘-R」)
- Google Chrome:按「Ctrl-Shift-R」 (Mac 則為「⌘-Shift-R」)
- Internet Explorer:按住「Ctrl」時點選「重新整理」,或按「Ctrl-F5」
- Opera:進入「工具→偏好設定」中清除快取。
- 如果您已登入但該頁面出現未登入狀態,請嘗試在網址列的URL最後新增代碼
?_=1
來訪問最新頁面。- 新增如上代碼後的本頁地址:
http://zh.moegirl.tw/User:C8H17OH/quick-save.js?_=1
- 新增如上代碼後的本頁地址:
- // 搬運自[[User:AnnAngela/js/quick-save.js]],將權限限制由管理員降至巡查姬
- /* eslint-disable no-magic-numbers */
- /* global mw */
- // <pre>
- $(function() {
- if (!mw.config.get("wgPageName").startsWith("萌娘百科_talk:討論版/")) { return; }
- if (mw.config.get("wgUserGroups").indexOf("patroller") === -1) { return; }
- mw.loader.load(`${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/User:AnnAngela/js/quick-save.js/style.css?action=raw&ctype=text/css`, "text/css");
- mw.loader.using("mw.Api").then(function() {
- var container = $('<div class="AnnTools_Frame" style="display: none;"><div class="AnnTools_Frame_Head"><div class="AnnTools_Frame_Title">公共討論頁段落存檔工具</div><span class="AnnTools_Frame_Close">×</span></div><div class="AnnTools_Frame_Content"><div class="AnnTools_Confirm"><div class="AnnTools_Confirm_Content">請問你是要存檔這個段落嗎?<br>段落標題:<span class="AnnTools_SectionTitle"></span></div><div class="AnnTools_Confirm_Yes">是呀是呀</div><div class="AnnTools_Confirm_No">並不是呢</div></div><div class="AnnTools_Info"><div class="AnnTools_ProgressBar"><div class="AnnTools_ProgressBar_Finished"></div></div>進度:<ol class="AnnTools_WorkDetail"></ol></div></div></div>').appendTo("body"),
- api = new mw.Api();
- container.on("click", function(event) {
- var target = $(event.target);
- if (target.is(".AnnTools_Frame_Close") && !target.is(".disable")) {
- container.fadeOut(370).queue(function() {
- container.find(".AnnTools_Confirm, .AnnTools_Info, .AnnTools_ProgressBar_Finished").removeAttr("style");
- container.find(".AnnTools_WorkDetail").empty();
- $(this).dequeue();
- });
- } else if (target.is(".AnnTools_Confirm_Yes")) {
- container.find(".AnnTools_Confirm").fadeOut(370);
- container.find(".AnnTools_Info").fadeIn(370);
- var date = new Date();
- date.month = date.getMonth() + 1;
- if (date.month < 10) { date.month = "0" + date.month; }
- container.trigger("submit", date);
- } else if (target.is(".AnnTools_Confirm_No")) { container.fadeOut(370); }
- }).on("submit", function(_, date) {
- new Promise(async function(res) {
- container.trigger("update", "正在獲取該段落內容");
- const hash = container.data("sectionTitle");
- const toclist = Object.fromEntries((await api.post({
- action: "parse",
- format: "json",
- pageid: mw.config.get("wgArticleId"),
- prop: "sections",
- })).parse.sections.map(({ anchor, index }) => [anchor, index]));
- if (!(hash in toclist)) {
- throw new Error("請移除該標題內的模板後再行操作……");
- }
- container.data("section", toclist[hash]);
- res();
- }).then(function() {
- return new Promise(function(res) {
- container.find(".AnnTools_Frame_Close").addClass("disable");
- $.ajax({
- url: `${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/index.php`,
- data: {
- title: mw.config.get("wgPageName"),
- action: "raw",
- section: container.data().section,
- },
- success: function(_data) {
- var data = _data + "";
- var sectionText = data; /* .replace(new RegExp("^\\s*(\\={1,})" + container.data().sectionTitle + "\\1"), "$1" + container.data().sectionTitleSafe + "$1"); */
- var sectionTitleRaw = data.match(/==(.*)==/);
- if (sectionTitleRaw && sectionTitleRaw[1]) { sectionTitleRaw = sectionTitleRaw[1]; } else { sectionTitleRaw = container.data().sectionTitle; }
- container.trigger("success");
- res({
- sectionText: sectionText,
- sectionTitleRaw: sectionTitleRaw,
- });
- },
- error: function(_, textStatus) {
- throw new Error(textStatus);
- },
- });
- });
- }).then(function(data) {
- container.trigger("update", "正在存檔該段落內容");
- return new Promise(function(res) {
- api.postWithToken("csrf", {
- action: "edit",
- format: "json",
- title: mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月",
- text: data.sectionText.replace("==" + data.sectionTitleRaw + "==", "").trim(),
- section: "new",
- tags: "快速存檔討論串|Automation tool",
- sectiontitle: data.sectionTitleRaw,
- summary: "快速存檔討論串:" + container.data().sectionTitle,
- }).then(function(result) {
- if (result.error) { throw new Error("Editing Error: " + result.error["*"]); }
- container.trigger("success");
- res(data);
- }, function(_, textStatus) {
- throw new Error(textStatus);
- });
- });
- }).then(function(data) {
- container.trigger("update", "正在標記該段落為已存檔");
- return new Promise(function(res) {
- api.postWithToken("csrf", {
- action: "edit",
- format: "json",
- title: mw.config.get("wgPageName"),
- summary: "快速存檔討論串:" + container.data().sectionTitle,
- text: "==" + data.sectionTitleRaw + "==\n" + "{{Saved|link=" + mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月" + "|title=" + container.data().sectionTitleSafe.replace(/\|/g, "{{!}}") + "}}",
- section: container.data().section,
- tags: "快速存檔討論串|Automation tool",
- }).then(function(result) {
- if (result.error) { throw new Error("Editing Error: " + result.error["*"]); }
- container.trigger("success");
- res(data);
- }, function(_, textStatus) {
- throw new Error(textStatus);
- });
- });
- }).then(function(data) {
- container.trigger("update", "正在檢查存檔頁面是否帶有檔案館模板");
- return new Promise(function(res) {
- $.ajax({
- url: `${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/index.php`,
- data: {
- title: mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月",
- action: "raw",
- section: "0",
- },
- success: function(result) {
- data.savePageText = result + "";
- if (data.savePageText.indexOf(mw.config.get("wgTitle").replace(/\/.*$/, "") + "頁頂/檔案館}}") !== -1) {
- container.trigger("success", "模板存在");
- window.setTimeout(function() {
- container.trigger("finish");
- }, 730);
- } else {
- container.data("steps", container.data("steps") + 1);
- container.trigger("success", "模板不存在");
- res(data);
- }
- },
- error: function(_, textStatus) {
- throw new Error(textStatus);
- },
- });
- });
- }).then(function() {
- container.trigger("update", "正在向存檔頁添加檔案館模板");
- api.postWithToken("csrf", {
- action: "edit",
- format: "json",
- title: mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月",
- prependtext: "{{" + mw.config.get("wgTitle").replace(/\/.*$/, "") + "頁頂/檔案館}}\n",
- tags: "快速存檔討論串|Automation tool",
- summary: "添加檔案館模板",
- }).then(function(result) {
- if (result.error) { throw new Error("Editing Error: " + result.error["*"]); }
- container.trigger("success");
- window.setTimeout(function() {
- container.trigger("finish");
- }, 730);
- }, function(_, textStatus) {
- throw new Error(textStatus);
- });
- }).catch(function(reason) {
- container.trigger("error", reason);
- });
- }).on("update", function(_, text) {
- container.find(".AnnTools_WorkDetail").append($("<li>", {
- attr: {
- "class": "AnnTools_WorkDetail_Ongoing",
- },
- text: text + "……",
- }));
- }).on("success", function(_, _text) {
- var list = container.find(".AnnTools_WorkDetail");
- var text;
- if (_text) { text = "成功," + _text + "!"; } else { text = "成功!"; }
- list.find("li").last().append(text).toggleClass("AnnTools_WorkDetail_Ongoing AnnTools_WorkDetail_Succeed");
- container.find(".AnnTools_ProgressBar_Finished").width(100 * list.find("li").length / container.data().steps + "%");
- }).on("error", function(_, reason) {
- var text = "失敗!";
- if (reason) { text += "[" + reason + "]"; }
- container.find(".AnnTools_ProgressBar").addClass("error");
- container.find(".AnnTools_WorkDetail li").last().append(text).removeClass("AnnTools_WorkDetail_Ongoing").addClass("AnnTools_WorkDetail_Failed");
- container.find(".AnnTools_Frame_Close").removeClass("disable");
- }).on("finish", function() {
- container.find(".AnnTools_WorkDetail").after('<div class="AnnTools_Notice">存檔完成,即將刷新頁面……</div>');
- window.setTimeout(function() {
- window.location.reload();
- }, 730);
- });
- $("#mw-content-text > .mw-parser-output > h2, #mw-content-text > .mw-parser-output > .discussionContainer > h2").each(function() {
- var self = $(this);
- var content = self.nextUntil("h2").not("h2");
- if (content.hasClass("saveNotice")) { return; }
- var section = +new mw.Uri(self.find('.mw-editsection a[href*="action=edit"][href*="section="]').attr("href")).query.section,
- sectionTitle = self.find(".mw-headline").attr("id");
- /* Array.from(self.find(".mw-headline")[0].childNodes).forEach(function (n) {
- if (n.classList && n.classList.contains("mw-headline-number")) { return; }
- sectionTitle += n.textContent.trim() + "";
- }); */
- var sectionTitleSafe = sectionTitle;
- /*.split("").map(function (c) {
- if (c === "#") { return ""; }
- if (/[A-Za-z0-9 \u4e00-\u9fa5]/.test(c)) { return c; }
- return "&#" + c.charCodeAt(0) + ";";
- }).join("");*/
- if (/_\d+$/.test(sectionTitle) && document.getElementById(sectionTitle.replace(/_\d+$/, ""))) sectionTitleSafe = sectionTitleSafe.replace(/_\d+$/, "");
- self.find(".mw-editsection-bracket").first()
- .after('<span class="mw-editsection-divider"> | </span>')
- .after('<a href="javascript:void(0)" class="AnnTools_QuickSave">快速存檔</a>');
- self.on("click", function(event) {
- if (!$(event.target).is(".AnnTools_QuickSave") || container.is(":visible")) { return true; }
- container.find(".AnnTools_SectionTitle").text(sectionTitle);
- container.data({
- section: section,
- sectionTitle: sectionTitle,
- sectionTitleSafe: sectionTitleSafe,
- steps: 4,
- }).fadeIn(370);
- });
- if (self.find(".AnnTools_MarkAsResolved")[0]) {
- var quicksave = self.find(".AnnTools_QuickSave");
- var divider = quicksave.next(".mw-editsection-divider");
- self.find(".mw-editsection .mw-editsection-bracket").first().after(divider).after(quicksave);
- }
- });
- });
- });
- // </pre>
此頁面最後編輯於 2020年8月16日 (週日) 16:08。