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}-
- "use strict";
- const STYLE_SHEET = `
- #batch-watch-result-container {
- height: 400px;
- width: max(80%, 400px);
- overflow: scroll;
- display: flex;
- flex-direction: column-reverse;
- border: 2px solid #2c56a3;
- border-radius: 5px;
- }`;
- const download = (filename, text) => {
- const element = document.createElement('a');
- element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
- element.setAttribute('download', filename);
- element.style.display = 'none';
- document.body.appendChild(element);
- element.click();
- document.body.removeChild(element);
- };
- const download_existing_watch_list = async () => {
- const BATCH_COUNT = 500;
- let offset = 0;
- let result = [];
- const api = new mw.Api();
- const result_object = $("#batch-watch-result")
- let cont = "";
- while (true) {
- result_object.append(`<p>正在获取监视列表的第${offset + 1}个至第${offset + BATCH_COUNT}个页面</p>`);
- let response;
- try {
- let params = {
- 'action': 'query',
- 'format': 'json',
- 'generator': 'watchlistraw',
- 'gwrlimit': BATCH_COUNT,
- 'gwrnamespace': '*'
- };
- if (cont !== '') {
- params.gwrcontinue = cont;
- }
- response = await api.postWithToken('csrf', params);
- } catch (e) {
- result_object.append(`<p>出错了。问题见控制台。</p>`);
- console.error(e);
- return;
- }
- const pages = response['query']['pages'];
- for (let page_id in pages) {
- const page = pages[page_id]
- if (page['missing'] === undefined) {
- result.push(page['title']);
- offset += 1
- }
- }
- if (response['continue'] === undefined) {
- break;
- } else {
- cont = response['continue']['gwrcontinue'];
- }
- // TODO: sleep here
- }
- result_object.append('<p>开始下载名为"监视列表.txt"的文件</p>');
- download("监视列表.txt", result.join("\n"));
- };
- const manipulate_watch_list = (watch) => {
- const cat = $('#batch-watch-cat').val();
- const ns = $('#batch-watch-ns').val();
- const api = new mw.Api();
- const result_object = $("#batch-watch-result");
- let counter = 0;
- const watch_function = (cont) => {
- api.postWithToken('watch', {
- 'action': 'watch',
- 'format': 'json',
- 'unwatch': !watch,
- 'generator': 'categorymembers',
- 'gcmlimit': 500,
- 'gcmnamespace': ns,
- 'gcmtitle': cat,
- 'gcmcontinue': cont
- }).then(
- (response) => {
- for (let index in response['watch']) {
- const page = response['watch'][index];
- const title = page['title'];
- result_object.append(`<p>已将页面<a href="/${title}">${title}</a>${watch ? '加入' : '移出'}监视列表</p>`);
- counter += 1;
- // TODO: sleep here
- }
- const cont = response['continue'];
- if (cont !== undefined && cont['gcmcontinue'] !== undefined) {
- watch_function(cont['gcmcontinue']);
- } else {
- result_object.append(`<p>操作完成。总共处理了${counter}个页面。</p>`);
- }
- },
- (error) => {
- console.log(error);
- result_object.append(`<p>出错了,问题见控制台。</p>`);
- }
- );
- };
- watch_function("");
- }
- const add_to_watch_list = () => {
- manipulate_watch_list(true);
- };
- const remove_from_watch_list = () => {
- manipulate_watch_list(false);
- };
- const start = () => {
- $(document).prop('title', '批量监视小工具');
- $('h1').text("批量监视小工具");
- // $('head').append(STYLE_SHEET);
- mw.util.addCSS(STYLE_SHEET);
- const body = $("#bodyContent");
- body.html(
- "<p>由于对监视列表的操作难以撤销,强烈建议您在使用本工具前下载您的监视列表作为备份。" +
- "如果误操作,可以前往<a href='/Special:编辑监视列表/raw'>编辑监视列表</a>恢复。</p>");
- const download_button = $("<button></button>").text("下载监视列表");
- download_button.click("click", download_existing_watch_list);
- body.append(download_button);
- body.append("<hr>");
- const form = $("<form></form>").attr('id', 'batch-watch-form');
- form.append($("<label></label>").attr('for', 'batch-watch-cat').text('分类:'));
- form.append($("<input/>").attr('id', 'batch-watch-cat').val('Category:'));
- form.append($("<label></label>").attr('for', 'batch-watch-ns').text('名字空间:'));
- form.append($("<input/>").attr('id', 'batch-watch-ns').val('0'));
- body.append(form);
- body.append('<p>注:填写分类时请保留"Category:",否则会出错。0指的是主名字空间。如需访问位于所有名字空间的页面,请使用"*"。</p>')
- body.append('<p>确保以上信息无误后,请点击下面的按钮批量操作</p>');
- const add_button = $("<button></button>").text("添加至监视列表").css('margin-right', '10px');
- add_button.click("click", add_to_watch_list);
- body.append(add_button);
- const remove_button = $("<button></button>").text("从监视列表移除");
- remove_button.click("click", remove_from_watch_list);
- body.append(remove_button);
- const result_wrapper = $("<div></div>")
- .attr('id', 'batch-watch-result-container')
- const result = $("<div></div>").attr("id", "batch-watch-result").append('<p>小工具加载完毕。</p>');
- result_wrapper.append(result);
- body.append(result_wrapper);
- };
- const BATCH_WATCH_TITLES = new Set(["batchwatch", "watch", "监视", "批量监视"]);
- if (mw.config.get('wgNamespaceNumber') === -1 && BATCH_WATCH_TITLES.has(mw.config.get('wgTitle').toLowerCase())) {
- console.log("Running batch watch...");
- start();
- }