User:Bluedeck/serve/ar-admin-2.js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
"use strict";
// Author: Bluedeck <bluedeck@outlook.com> Licence: MIT (Compatible with CC-BY-SA 3.0 and 4.0)
// 如何使用:importScript这个页面,在侧边栏中可见 Article rescue 链接,(效果:i.imgur.com/uiu9KGV.mp4)
// 点击后在小框内输入 Wikipedia:test,即可找回[[Wikipedia:test]]的已删除版本,
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
(function (ele, txt, time, condition) {
if (!condition || !ele)
return;
ele.innerHTML = "";
(function (ele, txt, time) {
var txt_split = txt.split("");
var len = txt.length, rate = time / len;
for (var i = 0; i < len; i++)
setTimeout(function () { ele.innerHTML += txt_split.shift(); }, i * rate);
})(ele, txt, time);
})(document.getElementById("ar_admin"), "2020.3.0 / ab30", 400, document.getElementById("8c23b4144bd58c689e192c6ab912a3b75c76f6849977518b8bedefd5e347d67f"));
(function () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return __awaiter(this, void 0, void 0, function () {
function ding(a, b) { }
function asyncSleep(timeMs) {
return new Promise(function (res, rej) {
setTimeout(res, timeMs);
});
}
function replace(a, b, c) {
return a.split(b).join(c);
}
function regulateWikitextForHtml(a) {
return replace(replace(replace(replace(replace(a, "<", "<"), "{{", "<nowiki>{{</nowiki>"), "~~", "~<!---->~"), "[[", "[[:"), "[[::", "[[:");
}
function regulateTextForHtml(a) {
if (typeof a !== "string") {
a = String(a);
}
return replace(replace(replace(a, "<", "<"), "\"", """), "'", "'");
}
function reverseArray(a) {
var b = [];
for (var i = 0; i < a.length; i++) {
b[a.length - i - 1] = a[i];
}
return b;
}
function asyncPost(body) {
return new Promise(function (res, rej) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
res(xhr.responseText);
}
else {
rej(xhr);
}
}
};
xhr.open("POST", endpoint, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(body);
});
}
function getToken(force) {
if (force === void 0) { force = false; }
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function () {
var new_token;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!force && token) {
return [2 /*return*/, token];
}
return [4 /*yield*/, asyncPost("action=query&meta=tokens&format=json")];
case 1:
new_token = _d.sent();
token = (_c = (_b = (_a = JSON.parse(new_token)) === null || _a === void 0 ? void 0 : _a.query) === null || _b === void 0 ? void 0 : _b.tokens) === null || _c === void 0 ? void 0 : _c.csrftoken;
return [2 /*return*/, token];
}
});
});
}
function asyncEdit(pageName, newContent, comment) {
return __awaiter(this, void 0, void 0, function () {
var responseText, _a, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
ding("Requesting page edit for: " + pageName + ", Summary: " + comment);
_a = asyncPost;
_b = "action=edit&minor=1&bot=1&format=json&title=" + eur(pageName) + "&text=" + eur(newContent) + "&summary=" + eur(comment) + "&token=";
_c = eur;
return [4 /*yield*/, getToken()];
case 1: return [4 /*yield*/, _a.apply(void 0, [_b + _c.apply(void 0, [_d.sent()])])];
case 2:
responseText = _d.sent();
ding("Following page edited: " + pageName + " Detail: " + responseText);
return [2 /*return*/, responseText];
}
});
});
}
function appAsyncArticleRescue(pagename, abortSignal) {
var _a;
return __awaiter(this, void 0, void 0, function () {
function logLine() { console.log("------------------------------"); }
var HISTORY_STORAGE_PAGE, INDEX_STORAGE_PAGE, PLACEHOLDER, FINAL_PLACEHOLDER, queryPart, rawResponseJson, _b, _c, revlist, indexPageRevList, indexPageContentList, someArchivesFailed, _d, _e, j, i, archiveEditJsonResponse, _f, _g, i, indexPageMainContent, indexPageFailedContent, finalrev, _h;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
openUiWindow();
setUiPercentage(0);
uiWindowElements.closeWindowButtonElement.style.display = "none";
uiWindowElements.abortButtonElement.style.display = "inline-block";
HISTORY_STORAGE_PAGE = "Wikipedia:已删除内容查询/查询/" + pagename;
INDEX_STORAGE_PAGE = "Wikipedia:已删除内容查询/查询/" + pagename;
PLACEHOLDER = "{" + "{CSD placeholder}}";
FINAL_PLACEHOLDER = false;
logLine();
logToUi("正在准备启动AR程序...");
console.log("starting async art-res procedure on " + pagename);
return [4 /*yield*/, asyncEdit(HISTORY_STORAGE_PAGE, PLACEHOLDER, "-")];
case 1:
_j.sent();
logLine();
queryPart = "action=query&prop=deletedrevisions&format=json&drvprop=content|comment|user|userid|timestamp|size|ids&drvlimit=5000&titles=";
_c = (_b = JSON).parse;
return [4 /*yield*/, asyncPost(queryPart + eur(pageName))];
case 2:
rawResponseJson = _c.apply(_b, [_j.sent()]);
revlist = new Array();
indexPageRevList = [];
indexPageContentList = [];
someArchivesFailed = false;
_j.label = 3;
case 3:
if (!rawResponseJson["continue"]) return [3 /*break*/, 5];
revlist = reverseArray(Object.entries(rawResponseJson.query.pages)[0][1].deletedrevisions).concat(revlist);
_e = (_d = JSON).parse;
return [4 /*yield*/, asyncPost(queryPart + eur(pagename) + "&drvcontinue=" + eur(rawResponseJson["continue"].drvcontinue))];
case 4:
rawResponseJson = _e.apply(_d, [_j.sent()]);
return [3 /*break*/, 3];
case 5:
revlist = reverseArray(Object.entries(rawResponseJson.query.pages)[0][1].deletedrevisions).concat(revlist);
logToUi("\u53D6\u5F97\u4E86" + revlist.length + "\u4E2A\u5DF2\u5220\u9664\u7248\u672C");
console.log(revlist.length + " DRVs fetched.");
logLine();
return [4 /*yield*/, asyncSleep(275)];
case 6:
_j.sent();
j = 0;
_j.label = 7;
case 7:
if (!(j < revlist.length)) return [3 /*break*/, 10];
if (abortSignal.aborted) {
setUiPercentage(1);
logToUi("查询已中止");
uiWindowElements.closeWindowButtonElement.style.display = "inline-block";
uiWindowElements.abortButtonElement.style.display = "none";
return [2 /*return*/];
}
i = j;
_g = (_f = JSON).parse;
return [4 /*yield*/, asyncEdit(HISTORY_STORAGE_PAGE, revlist[i]["*"], "DRV lookup: [[:" + pagename + "]] - Author: [[user talk:" + revlist[i].user + "|]] - Time: " + revlist[i].timestamp + " - Revision ID: " + revlist[i].revid)];
case 8:
archiveEditJsonResponse = _g.apply(_f, [_j.sent()]);
if (archiveEditJsonResponse.error) {
console.log(archiveEditJsonResponse.error.code, archiveEditJsonResponse.error.info);
indexPageRevList[i] = { errorCode: archiveEditJsonResponse.error.code };
someArchivesFailed = true;
}
else {
indexPageRevList[i] = (_a = archiveEditJsonResponse.edit) === null || _a === void 0 ? void 0 : _a.newrevid;
}
if (!indexPageRevList[i]) {
indexPageRevList[i] = indexPageRevList[i - 1];
}
console.log("(" + (i + 1).toString() + "/" + revlist.length + ") revision " + revlist[i].revid + " was put into rev " + indexPageRevList[i]);
if (typeof indexPageRevList[i] === "number") {
logToUi("(" + (i + 1).toString() + "/" + revlist.length + ") 已删版本" + revlist[i].revid + "已被写入" + indexPageRevList[i]);
}
else {
logToUi("(" + (i + 1).toString() + "/" + revlist.length + ") 已删版本" + revlist[i].revid + "在保存时失败报错:" + indexPageRevList[i].errorCode);
}
setUiPercentage((i + 1) / revlist.length);
_j.label = 9;
case 9:
j++;
return [3 /*break*/, 7];
case 10:
uiWindowElements.abortButtonElement.style.display = "none";
logLine();
return [4 /*yield*/, asyncEdit(HISTORY_STORAGE_PAGE, PLACEHOLDER, "-")];
case 11:
_j.sent();
console.log("Please wait while the index page is being created...");
logToUi("正在创建索引页面");
logLine();
for (i = 0; i < revlist.length; i++) {
indexPageContentList[i] = "\n\n<tr>\n<td> " + (typeof indexPageRevList[i] !== "number" ? "'''\u5B58\u6863\u5931\u8D25'''<ref>\u5931\u8D25\u539F\u56E0\uFF1A" + indexPageRevList[i].errorCode + "</ref>" : "[[special:permanentLink/" + indexPageRevList[i] + "|'''\u67E5\u770B\u5B58\u6863''']]") + " </td>\n<td> " + regulateTextForHtml(revlist[i].timestamp.split("T").join(" ").split("Z").join("")) + " </td>\n<td> [[user talk:" + regulateTextForHtml(revlist[i].user) + "|]] </td>\n<td> " + regulateTextForHtml(revlist[i].size) + " </td>\n<td> " + regulateTextForHtml(revlist[i].revid) + " </td>\n<td> " + regulateWikitextForHtml(revlist[i].comment) + "</td>\n</tr>";
}
indexPageMainContent = "\u9875\u9762[[:" + pagename + "]]\u5171\u6709" + revlist.length + "\u4E2A\u5DF2\u5220\u9664\u7248\u672C\uFF0C\u5B58\u6863\u5982\u4E0B\uFF1A\n----\n<table style='white-space:nowrap'><tr><td></td><td>'''\u7F16\u8F91\u65F6\u5206'''</td><td>'''\u7528\u6237'''</td><td>'''\u9875\u9762\u5927\u5C0F'''</td><td>'''\u7248\u672C\u53F7'''</td><td>'''\u7F16\u8F91\u6458\u8981'''</td></tr>" + reverseArray(indexPageContentList).join("") + "\n</table>\n----\n{" + "{subst:User:Bluedeck/infr/ar.thankyou.js}}";
indexPageFailedContent = "\n\n===\u5931\u8D25\u539F\u56E0===\n\u88AB\u67E5\u8BE2\u7684\u7248\u672C\u4E2D\u6709\u4E9B\u65E0\u6CD5\u5B58\u6863\uFF0C\u5B58\u6863\u5931\u8D25\u7684\u539F\u56E0\u8BB0\u5F55\u5982\u4E0B\u3002<references />";
return [4 /*yield*/, asyncEdit(INDEX_STORAGE_PAGE, indexPageMainContent + (someArchivesFailed ? indexPageFailedContent : ""), "DRV lookup completed: [[:" + pagename + "]]")];
case 12:
finalrev = _j.sent();
if (!FINAL_PLACEHOLDER) return [3 /*break*/, 14];
return [4 /*yield*/, asyncEdit(INDEX_STORAGE_PAGE, PLACEHOLDER, "-")];
case 13:
_h = _j.sent();
return [3 /*break*/, 15];
case 14:
_h = void null;
_j.label = 15;
case 15:
_h;
console.log("All done: " + JSON.parse(finalrev).edit.newrevid);
logLine();
setUiPercentage(1);
logToUi("全部完成,存档页面版本:" + JSON.parse(finalrev).edit.newrevid);
uiWindowElements.closeWindowButtonElement.style.display = "inline-block";
return [2 /*return*/];
}
});
});
}
function entryArticleRescue() {
return __awaiter(this, void 0, void 0, function () {
var page, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (inProgress) {
return [2 /*return*/];
}
inProgress = true;
_a.label = 1;
case 1:
_a.trys.push([1, 3, 4, 5]);
page = prompt("Page name: ", mw.config.values.wgPageName);
if (typeof page !== "string") {
return [2 /*return*/];
}
abortSignal.aborted = false;
return [4 /*yield*/, appAsyncArticleRescue(page, abortSignal)];
case 2:
_a.sent();
return [3 /*break*/, 5];
case 3:
e_1 = _a.sent();
console.log(e_1);
alert("Failed. Specified page does not have deleted revisions.");
return [2 /*return*/];
case 4:
inProgress = false;
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
}
});
});
}
function logToUi(oneLiner) {
uiWindowElements.textLineElement.innerText = oneLiner;
}
function abort() {
abortSignal.aborted = true;
}
function createUiWindow() {
var windowElement = document.createElement("div");
var titleTextElement = document.createElement("div");
var textLineElement = document.createElement("div");
var abortButtonElement = document.createElement("button");
abortButtonElement.innerText = "中止";
abortButtonElement.addEventListener("click", abort);
abortButtonElement.style.borderRadius = "9999px";
abortButtonElement.style.marginLeft = "1em";
abortButtonElement.style.border = "0";
abortButtonElement.style.position = "absolute";
abortButtonElement.style.right = "1em";
abortButtonElement.style.top = "0.7em";
var closeWindowButtonElement = document.createElement("button");
closeWindowButtonElement.innerText = "关闭";
closeWindowButtonElement.addEventListener("click", closeUiWindow);
closeWindowButtonElement.style.borderRadius = "9999px";
closeWindowButtonElement.style.marginLeft = "1em";
closeWindowButtonElement.style.border = "0";
closeWindowButtonElement.style.position = "absolute";
closeWindowButtonElement.style.right = "1em";
closeWindowButtonElement.style.top = "0.7em";
var progressBarBaseElement = document.createElement("div");
var progressBarInnerFillerElement = document.createElement("div");
document.body.appendChild(windowElement);
windowElement.appendChild(abortButtonElement);
windowElement.appendChild(closeWindowButtonElement);
windowElement.appendChild(titleTextElement);
windowElement.appendChild(textLineElement);
windowElement.appendChild(progressBarBaseElement);
windowElement.style.position = "fixed";
windowElement.style.top = "50%";
windowElement.style.left = "50%";
windowElement.style.transform = "translate(-50%, -50%)";
windowElement.style.width = "350px";
windowElement.style.maxWidth = "80vw";
windowElement.style.maxHeight = "80vh";
windowElement.style.minHeight = "1em";
windowElement.style.display = "none";
windowElement.style.border = "solid 0 #ccc";
windowElement.style.borderRadius = "0.3em";
windowElement.style.boxShadow = "0 3px 11px 2px rgba(0,0,0,0.3)";
windowElement.style.background = "#fff";
windowElement.style.zIndex = "4200";
windowElement.style.padding = "1em";
windowElement.style.fontSize = "85%";
windowElement.style.gridTemplateColumns = "";
windowElement.style.gridTemplateRows = "";
titleTextElement.innerText = "ARTICLE RESCUE";
titleTextElement.style.color = "#777";
titleTextElement.style.fontWeight = "bold";
titleTextElement.style.fontSize = "90%";
textLineElement.innerText = "Putting contents from 60934914 into 89392112... ";
progressBarBaseElement.appendChild(progressBarInnerFillerElement);
progressBarBaseElement.style.borderRadius = "9999px";
progressBarBaseElement.style.background = "#e6e6e6";
progressBarBaseElement.style.height = "7px";
progressBarBaseElement.style.overflow = "hidden";
progressBarBaseElement.style.marginTop = "4px";
progressBarInnerFillerElement.style.background = "#ace";
progressBarInnerFillerElement.style.height = "100%";
progressBarInnerFillerElement.style.width = "0%";
progressBarInnerFillerElement.style.transition = "width 0.12s";
return {
windowElement: windowElement,
titleTextElement: titleTextElement,
abortButtonElement: abortButtonElement,
closeWindowButtonElement: closeWindowButtonElement,
textLineElement: textLineElement,
progressBarBaseElement: progressBarBaseElement,
progressBarInnerFillerElement: progressBarInnerFillerElement
};
}
function openUiWindow() {
uiWindowElements.windowElement.style.display = "inline-grid";
}
function closeUiWindow() {
uiWindowElements.windowElement.style.display = "none";
}
function setUiPercentage(percentage) {
uiWindowElements.progressBarInnerFillerElement.style.width = (percentage * 100).toString() + "%";
}
function createUiLink() {
var _a, _b, _c;
var listElement = document.createElement("li");
var aElement = document.createElement("a");
aElement.href = "javascript: void(0)";
aElement.addEventListener("click", entryArticleRescue);
aElement.innerText = "Article rescue";
listElement.appendChild(aElement);
(_c = (_b = (_a = document.getElementById("p-tb-label")) === null || _a === void 0 ? void 0 : _a.nextElementSibling) === null || _b === void 0 ? void 0 : _b.firstElementChild) === null || _c === void 0 ? void 0 : _c.appendChild(listElement);
}
var eur, url, pageName, userName, userPageName, userTalkPageName, token, endpoint, abortSignal, inProgress, uiWindowElements;
return __generator(this, function (_k) {
eur = encodeURIComponent;
url = (_c = (_b = (_a = mw === null || mw === void 0 ? void 0 : mw.config) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.wgServer) !== null && _c !== void 0 ? _c : null;
pageName = (_f = (_e = (_d = mw === null || mw === void 0 ? void 0 : mw.config) === null || _d === void 0 ? void 0 : _d.values) === null || _e === void 0 ? void 0 : _e.wgPageName) !== null && _f !== void 0 ? _f : null;
userName = (_j = (_h = (_g = mw === null || mw === void 0 ? void 0 : mw.config) === null || _g === void 0 ? void 0 : _g.values) === null || _h === void 0 ? void 0 : _h.wgUserName) !== null && _j !== void 0 ? _j : null;
userPageName = userName ? "User:" + userName : null;
userTalkPageName = userName ? "User_talk:" + userName : null;
token = "";
endpoint = url + "/w/api.php";
getToken(true);
setInterval(function () { return getToken(true); }, 1800000);
abortSignal = { aborted: false };
inProgress = false;
createUiLink();
uiWindowElements = createUiWindow();
return [2 /*return*/];
});
});
})();