置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

User:Lihp/js/batch-watch.js

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索

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

  • Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
  • Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
  • Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
  • Opera:在“工具→首选项”中清除缓存
如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码?_=1来访问最新页面。
添加代码后的本页地址如下:-{R|https://moegirl.uk/User:Lihp/js/batch-watch.js?_=1}-
  1. "use strict";
  2. const STYLE_SHEET = `
  3. #batch-watch-result-container {
  4. height: 400px;
  5. width: max(80%, 400px);
  6. overflow: scroll;
  7. display: flex;
  8. flex-direction: column-reverse;
  9. border: 2px solid #2c56a3;
  10. border-radius: 5px;
  11. }`;
  12. const download = (filename, text) => {
  13. const element = document.createElement('a');
  14. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  15. element.setAttribute('download', filename);
  16. element.style.display = 'none';
  17. document.body.appendChild(element);
  18. element.click();
  19. document.body.removeChild(element);
  20. };
  21. const download_existing_watch_list = async () => {
  22. const BATCH_COUNT = 500;
  23. let offset = 0;
  24. let result = [];
  25. const api = new mw.Api();
  26. const result_object = $("#batch-watch-result")
  27. let cont = "";
  28. while (true) {
  29. result_object.append(`<p>正在获取监视列表的第${offset + 1}个至第${offset + BATCH_COUNT}个页面</p>`);
  30. let response;
  31. try {
  32. let params = {
  33. 'action': 'query',
  34. 'format': 'json',
  35. 'generator': 'watchlistraw',
  36. 'gwrlimit': BATCH_COUNT,
  37. 'gwrnamespace': '*'
  38. };
  39. if (cont !== '') {
  40. params.gwrcontinue = cont;
  41. }
  42. response = await api.postWithToken('csrf', params);
  43. } catch (e) {
  44. result_object.append(`<p>出错了。问题见控制台。</p>`);
  45. console.error(e);
  46. return;
  47. }
  48. const pages = response['query']['pages'];
  49. for (let page_id in pages) {
  50. const page = pages[page_id]
  51. if (page['missing'] === undefined) {
  52. result.push(page['title']);
  53. offset += 1
  54. }
  55. }
  56. if (response['continue'] === undefined) {
  57. break;
  58. } else {
  59. cont = response['continue']['gwrcontinue'];
  60. }
  61. // TODO: sleep here
  62. }
  63. result_object.append('<p>开始下载名为"监视列表.txt"的文件</p>');
  64. download("监视列表.txt", result.join("\n"));
  65. };
  66. const manipulate_watch_list = (watch) => {
  67. const cat = $('#batch-watch-cat').val();
  68. const ns = $('#batch-watch-ns').val();
  69. const api = new mw.Api();
  70. const result_object = $("#batch-watch-result");
  71. let counter = 0;
  72. const watch_function = (cont) => {
  73. api.postWithToken('watch', {
  74. 'action': 'watch',
  75. 'format': 'json',
  76. 'unwatch': !watch,
  77. 'generator': 'categorymembers',
  78. 'gcmlimit': 500,
  79. 'gcmnamespace': ns,
  80. 'gcmtitle': cat,
  81. 'gcmcontinue': cont
  82. }).then(
  83. (response) => {
  84. for (let index in response['watch']) {
  85. const page = response['watch'][index];
  86. const title = page['title'];
  87. result_object.append(`<p>已将页面<a href="/${title}">${title}</a>${watch ? '加入' : '移出'}监视列表</p>`);
  88. counter += 1;
  89. // TODO: sleep here
  90. }
  91. const cont = response['continue'];
  92. if (cont !== undefined && cont['gcmcontinue'] !== undefined) {
  93. watch_function(cont['gcmcontinue']);
  94. } else {
  95. result_object.append(`<p>操作完成。总共处理了${counter}个页面。</p>`);
  96. }
  97. },
  98. (error) => {
  99. console.log(error);
  100. result_object.append(`<p>出错了,问题见控制台。</p>`);
  101. }
  102. );
  103. };
  104. watch_function("");
  105. }
  106. const add_to_watch_list = () => {
  107. manipulate_watch_list(true);
  108. };
  109. const remove_from_watch_list = () => {
  110. manipulate_watch_list(false);
  111. };
  112. const start = () => {
  113. $(document).prop('title', '批量监视小工具');
  114. $('h1').text("批量监视小工具");
  115. // $('head').append(STYLE_SHEET);
  116. mw.util.addCSS(STYLE_SHEET);
  117. const body = $("#bodyContent");
  118. body.html(
  119. "<p>由于对监视列表的操作难以撤销,强烈建议您在使用本工具前下载您的监视列表作为备份。" +
  120. "如果误操作,可以前往<a href='/Special:编辑监视列表/raw'>编辑监视列表</a>恢复。</p>");
  121. const download_button = $("<button></button>").text("下载监视列表");
  122. download_button.click("click", download_existing_watch_list);
  123. body.append(download_button);
  124. body.append("<hr>");
  125. const form = $("<form></form>").attr('id', 'batch-watch-form');
  126. form.append($("<label></label>").attr('for', 'batch-watch-cat').text('分类:'));
  127. form.append($("<input/>").attr('id', 'batch-watch-cat').val('Category:'));
  128. form.append($("<label></label>").attr('for', 'batch-watch-ns').text('名字空间:'));
  129. form.append($("<input/>").attr('id', 'batch-watch-ns').val('0'));
  130. body.append(form);
  131. body.append('<p>注:填写分类时请保留"Category:",否则会出错。0指的是主名字空间。如需访问位于所有名字空间的页面,请使用"*"。</p>')
  132. body.append('<p>确保以上信息无误后,请点击下面的按钮批量操作</p>');
  133. const add_button = $("<button></button>").text("添加至监视列表").css('margin-right', '10px');
  134. add_button.click("click", add_to_watch_list);
  135. body.append(add_button);
  136. const remove_button = $("<button></button>").text("从监视列表移除");
  137. remove_button.click("click", remove_from_watch_list);
  138. body.append(remove_button);
  139. const result_wrapper = $("<div></div>")
  140. .attr('id', 'batch-watch-result-container')
  141. const result = $("<div></div>").attr("id", "batch-watch-result").append('<p>小工具加载完毕。</p>');
  142. result_wrapper.append(result);
  143. body.append(result_wrapper);
  144. };
  145. const BATCH_WATCH_TITLES = new Set(["batchwatch", "watch", "监视", "批量监视"]);
  146. if (mw.config.get('wgNamespaceNumber') === -1 && BATCH_WATCH_TITLES.has(mw.config.get('wgTitle').toLowerCase())) {
  147. console.log("Running batch watch...");
  148. start();
  149. }

导航菜单

返回顶部