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來訪問最新頁面。
  1. // 搬運自[[User:AnnAngela/js/quick-save.js]],將權限限制由管理員降至巡查姬
  2. /* eslint-disable no-magic-numbers */
  3. /* global mw */
  4. // <pre>
  5. $(function() {
  6. if (!mw.config.get("wgPageName").startsWith("萌娘百科_talk:討論版/")) { return; }
  7. if (mw.config.get("wgUserGroups").indexOf("patroller") === -1) { return; }
  8. 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");
  9. mw.loader.using("mw.Api").then(function() {
  10. 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"),
  11. api = new mw.Api();
  12. container.on("click", function(event) {
  13. var target = $(event.target);
  14. if (target.is(".AnnTools_Frame_Close") && !target.is(".disable")) {
  15. container.fadeOut(370).queue(function() {
  16. container.find(".AnnTools_Confirm, .AnnTools_Info, .AnnTools_ProgressBar_Finished").removeAttr("style");
  17. container.find(".AnnTools_WorkDetail").empty();
  18. $(this).dequeue();
  19. });
  20. } else if (target.is(".AnnTools_Confirm_Yes")) {
  21. container.find(".AnnTools_Confirm").fadeOut(370);
  22. container.find(".AnnTools_Info").fadeIn(370);
  23. var date = new Date();
  24. date.month = date.getMonth() + 1;
  25. if (date.month < 10) { date.month = "0" + date.month; }
  26. container.trigger("submit", date);
  27. } else if (target.is(".AnnTools_Confirm_No")) { container.fadeOut(370); }
  28. }).on("submit", function(_, date) {
  29. new Promise(async function(res) {
  30. container.trigger("update", "正在獲取該段落內容");
  31. const hash = container.data("sectionTitle");
  32. const toclist = Object.fromEntries((await api.post({
  33. action: "parse",
  34. format: "json",
  35. pageid: mw.config.get("wgArticleId"),
  36. prop: "sections",
  37. })).parse.sections.map(({ anchor, index }) => [anchor, index]));
  38. if (!(hash in toclist)) {
  39. throw new Error("請移除該標題內的模板後再行操作……");
  40. }
  41. container.data("section", toclist[hash]);
  42. res();
  43. }).then(function() {
  44. return new Promise(function(res) {
  45. container.find(".AnnTools_Frame_Close").addClass("disable");
  46. $.ajax({
  47. url: `${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/index.php`,
  48. data: {
  49. title: mw.config.get("wgPageName"),
  50. action: "raw",
  51. section: container.data().section,
  52. },
  53. success: function(_data) {
  54. var data = _data + "";
  55. var sectionText = data; /* .replace(new RegExp("^\\s*(\\={1,})" + container.data().sectionTitle + "\\1"), "$1" + container.data().sectionTitleSafe + "$1"); */
  56. var sectionTitleRaw = data.match(/==(.*)==/);
  57. if (sectionTitleRaw && sectionTitleRaw[1]) { sectionTitleRaw = sectionTitleRaw[1]; } else { sectionTitleRaw = container.data().sectionTitle; }
  58. container.trigger("success");
  59. res({
  60. sectionText: sectionText,
  61. sectionTitleRaw: sectionTitleRaw,
  62. });
  63. },
  64. error: function(_, textStatus) {
  65. throw new Error(textStatus);
  66. },
  67. });
  68. });
  69. }).then(function(data) {
  70. container.trigger("update", "正在存檔該段落內容");
  71. return new Promise(function(res) {
  72. api.postWithToken("csrf", {
  73. action: "edit",
  74. format: "json",
  75. title: mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月",
  76. text: data.sectionText.replace("==" + data.sectionTitleRaw + "==", "").trim(),
  77. section: "new",
  78. tags: "快速存檔討論串|Automation tool",
  79. sectiontitle: data.sectionTitleRaw,
  80. summary: "快速存檔討論串:" + container.data().sectionTitle,
  81. }).then(function(result) {
  82. if (result.error) { throw new Error("Editing Error: " + result.error["*"]); }
  83. container.trigger("success");
  84. res(data);
  85. }, function(_, textStatus) {
  86. throw new Error(textStatus);
  87. });
  88. });
  89. }).then(function(data) {
  90. container.trigger("update", "正在標記該段落為已存檔");
  91. return new Promise(function(res) {
  92. api.postWithToken("csrf", {
  93. action: "edit",
  94. format: "json",
  95. title: mw.config.get("wgPageName"),
  96. summary: "快速存檔討論串:" + container.data().sectionTitle,
  97. text: "==" + data.sectionTitleRaw + "==\n" + "{{Saved|link=" + mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月" + "|title=" + container.data().sectionTitleSafe.replace(/\|/g, "{{!}}") + "}}",
  98. section: container.data().section,
  99. tags: "快速存檔討論串|Automation tool",
  100. }).then(function(result) {
  101. if (result.error) { throw new Error("Editing Error: " + result.error["*"]); }
  102. container.trigger("success");
  103. res(data);
  104. }, function(_, textStatus) {
  105. throw new Error(textStatus);
  106. });
  107. });
  108. }).then(function(data) {
  109. container.trigger("update", "正在檢查存檔頁面是否帶有檔案館模板");
  110. return new Promise(function(res) {
  111. $.ajax({
  112. url: `${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/index.php`,
  113. data: {
  114. title: mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月",
  115. action: "raw",
  116. section: "0",
  117. },
  118. success: function(result) {
  119. data.savePageText = result + "";
  120. if (data.savePageText.indexOf(mw.config.get("wgTitle").replace(/\/.*$/, "") + "頁頂/檔案館}}") !== -1) {
  121. container.trigger("success", "模板存在");
  122. window.setTimeout(function() {
  123. container.trigger("finish");
  124. }, 730);
  125. } else {
  126. container.data("steps", container.data("steps") + 1);
  127. container.trigger("success", "模板不存在");
  128. res(data);
  129. }
  130. },
  131. error: function(_, textStatus) {
  132. throw new Error(textStatus);
  133. },
  134. });
  135. });
  136. }).then(function() {
  137. container.trigger("update", "正在向存檔頁添加檔案館模板");
  138. api.postWithToken("csrf", {
  139. action: "edit",
  140. format: "json",
  141. title: mw.config.get("wgPageName") + "/存檔/" + date.getFullYear() + "年" + date.month + "月",
  142. prependtext: "{{" + mw.config.get("wgTitle").replace(/\/.*$/, "") + "頁頂/檔案館}}\n",
  143. tags: "快速存檔討論串|Automation tool",
  144. summary: "添加檔案館模板",
  145. }).then(function(result) {
  146. if (result.error) { throw new Error("Editing Error: " + result.error["*"]); }
  147. container.trigger("success");
  148. window.setTimeout(function() {
  149. container.trigger("finish");
  150. }, 730);
  151. }, function(_, textStatus) {
  152. throw new Error(textStatus);
  153. });
  154. }).catch(function(reason) {
  155. container.trigger("error", reason);
  156. });
  157. }).on("update", function(_, text) {
  158. container.find(".AnnTools_WorkDetail").append($("<li>", {
  159. attr: {
  160. "class": "AnnTools_WorkDetail_Ongoing",
  161. },
  162. text: text + "……",
  163. }));
  164. }).on("success", function(_, _text) {
  165. var list = container.find(".AnnTools_WorkDetail");
  166. var text;
  167. if (_text) { text = "成功," + _text + "!"; } else { text = "成功!"; }
  168. list.find("li").last().append(text).toggleClass("AnnTools_WorkDetail_Ongoing AnnTools_WorkDetail_Succeed");
  169. container.find(".AnnTools_ProgressBar_Finished").width(100 * list.find("li").length / container.data().steps + "%");
  170. }).on("error", function(_, reason) {
  171. var text = "失敗!";
  172. if (reason) { text += "[" + reason + "]"; }
  173. container.find(".AnnTools_ProgressBar").addClass("error");
  174. container.find(".AnnTools_WorkDetail li").last().append(text).removeClass("AnnTools_WorkDetail_Ongoing").addClass("AnnTools_WorkDetail_Failed");
  175. container.find(".AnnTools_Frame_Close").removeClass("disable");
  176. }).on("finish", function() {
  177. container.find(".AnnTools_WorkDetail").after('<div class="AnnTools_Notice">存檔完成,即將刷新頁面……</div>');
  178. window.setTimeout(function() {
  179. window.location.reload();
  180. }, 730);
  181. });
  182. $("#mw-content-text > .mw-parser-output > h2, #mw-content-text > .mw-parser-output > .discussionContainer > h2").each(function() {
  183. var self = $(this);
  184. var content = self.nextUntil("h2").not("h2");
  185. if (content.hasClass("saveNotice")) { return; }
  186. var section = +new mw.Uri(self.find('.mw-editsection a[href*="action=edit"][href*="section="]').attr("href")).query.section,
  187. sectionTitle = self.find(".mw-headline").attr("id");
  188. /* Array.from(self.find(".mw-headline")[0].childNodes).forEach(function (n) {
  189. if (n.classList && n.classList.contains("mw-headline-number")) { return; }
  190. sectionTitle += n.textContent.trim() + "";
  191. }); */
  192. var sectionTitleSafe = sectionTitle;
  193. /*.split("").map(function (c) {
  194. if (c === "#") { return ""; }
  195. if (/[A-Za-z0-9 \u4e00-\u9fa5]/.test(c)) { return c; }
  196. return "&#" + c.charCodeAt(0) + ";";
  197. }).join("");*/
  198. if (/_\d+$/.test(sectionTitle) && document.getElementById(sectionTitle.replace(/_\d+$/, ""))) sectionTitleSafe = sectionTitleSafe.replace(/_\d+$/, "");
  199. self.find(".mw-editsection-bracket").first()
  200. .after('<span class="mw-editsection-divider"> | </span>')
  201. .after('<a href="javascript:void(0)" class="AnnTools_QuickSave">快速存檔</a>');
  202. self.on("click", function(event) {
  203. if (!$(event.target).is(".AnnTools_QuickSave") || container.is(":visible")) { return true; }
  204. container.find(".AnnTools_SectionTitle").text(sectionTitle);
  205. container.data({
  206. section: section,
  207. sectionTitle: sectionTitle,
  208. sectionTitleSafe: sectionTitleSafe,
  209. steps: 4,
  210. }).fadeIn(370);
  211. });
  212. if (self.find(".AnnTools_MarkAsResolved")[0]) {
  213. var quicksave = self.find(".AnnTools_QuickSave");
  214. var divider = quicksave.next(".mw-editsection-divider");
  215. self.find(".mw-editsection .mw-editsection-bracket").first().after(divider).after(quicksave);
  216. }
  217. });
  218. });
  219. });
  220. // </pre>
此頁面最後編輯於 2020年8月16日 (週日) 16:08。
搜尋萌娘百科 (按"/"快速搜尋)
有新的未讀公告