MediaWiki:Gadget-SpecialWikitext.js
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
"use strict";
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 };
}
};
// <pre>
(function () { return __awaiter(void 0, void 0, void 0, function () {
function lua_check(input_string, content_model) {
var contentModel = "".concat(content_model || mw.config.get("wgPageContentModel")).toLowerCase();
switch (contentModel) {
case "json":
return lua_getJSONwikitext(input_string);
case "js":
case "javascript":
case "text":
case "wiki":
return lua_getJSwikitext(input_string);
case "css":
case "sanitized-css":
return lua_getCSSwikitext(input_string);
default:
return "";
}
}
function lua_addText(input_str, new_str, _escape) {
var input_string = input_str;
if (new_str !== "") {
if (input_string !== "") {
input_string += "\n";
}
var text = new_str;
if (_escape) {
var escape_str = JSON.parse("[".concat(JSON.stringify("".concat(new_str).replace(/\\([ux])/ig, "$1")).replace(/\\\\/g, "\\"), "]"))[0];
text = escape_str;
}
input_string += text;
}
return input_string;
}
function lua_getString(str) {
var test_str = /[^\n]*\*\//.exec(str);
if (test_str) {
test_str = test_str[0] || "";
test_str = test_str.substr(0, test_str.length - 2);
}
else {
test_str = str;
}
var trim_check = getCleanText(test_str);
var first_char = trim_check.charAt(0);
if (first_char === trim_check.charAt(trim_check.length - 1) && (first_char === "'" || first_char === '"')) {
return trim_check.substr(1, trim_check.length - 2);
}
return test_str;
}
function lua_getContentText(str) {
var wikitext = "";
try {
str.replace(new RegExp("".concat(wikiTextKey, "\\s*\\{[^c\\}]*content\\s*:\\s*[^\n]*"), "g"), function (text) {
var temp_text = (/content\s*:\s*[^\n]*/.exec(text) || ["content:"])[0].
replace(/^[\s\uFEFF\xA0\t\r\n\f ;}]+|[\s\uFEFF\xA0\t\r\n\f ;}]+$/g, "").
replace(/\s*content\s*:\s*/, "");
if (wikitext !== "") {
wikitext += "\n";
}
wikitext += lua_getString(temp_text);
return text;
});
}
catch (ex) {
return "";
}
return wikitext;
}
function lua_getObjText(str) {
var wikitext = "";
try {
str.replace(new RegExp("".concat(wikiTextKey, "\\s*[\\=:]\\s*[^\n]*"), "g"), function (text) {
var temp_text = text.replace(/^[\s\uFEFF\xA0\t\r\n\f ;}]+|[\s\uFEFF\xA0\t\r\n\f ;}]+$/g, "").
replace(new RegExp("".concat(wikiTextKey, "\\s*[\\=:]\\s*")), "");
if (wikitext !== "") {
wikitext += "\n";
}
wikitext += lua_getString(temp_text);
return text;
});
}
catch (ex) {
return "";
}
return wikitext;
}
function lua_getCSSwikitext(input_string) {
var wikitext = "";
var css_text = getCleanText(input_string || $("#wpTextbox1").val() || "");
if (css_text === "") {
return "";
}
wikitext = lua_addText(wikitext, lua_getContentText(css_text), true);
wikitext = lua_addText(wikitext, lua_getObjText(css_text), true);
return wikitext;
}
function lua_getJSwikitext(input_string) {
var wikitext = "";
var js_text = getCleanText(input_string || $("#wpTextbox1").val() || "");
if (js_text === "") {
return "";
}
wikitext = lua_addText(wikitext, lua_getObjText(js_text), true);
return wikitext;
}
function lua_getJSONwikitext(input_string) {
var wikitext = "";
var json_text = getCleanText(input_string || $("#wpTextbox1").val() || "");
if (json_text === "") {
return "";
}
try {
var json_data_1 = JSON.parse(json_text);
Object.keys(json_data_1).forEach(function (key) {
var k = key, v = json_data_1[key];
if (new RegExp(wikiTextKey).exec(k) && typeof v === typeof "") {
wikitext = lua_addText(wikitext, v);
}
if (typeof v !== typeof "") {
for (var prop in v) {
if (Object.hasOwnProperty.call(v, prop)) {
var testArr_k = prop, testArr_v = v[prop];
if (new RegExp(wikiTextKey).exec(testArr_k) && typeof testArr_v === typeof "") {
wikitext = lua_addText(wikitext, testArr_v);
}
}
}
}
});
}
catch (ex) {
return "";
}
return wikitext;
}
var getCleanText, wikiTextKey, api, wgPageName, wgRevisionId, noticeHTML, mwConfigIfMatchInLowerCase, getLanguage, addLoadingNotice, loadingFailNotice, removeLoadingNotice, addParsedWikitext, ifNeedPreview, mwAddWikiText, mwAddLuaText, mwGetFromNowRevision, mwApplyNotice;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (mw.config.get("wgIsSpecialWikitextPreview")) {
return [2 /*return*/];
}
mw.config.set("wgIsSpecialWikitextPreview", true);
return [4 /*yield*/, $.ready];
case 1:
_a.sent();
getCleanText = function (input) { return typeof input === "string" || input ? "".concat(input).trim() : ""; };
wikiTextKey = "_addText";
api = new mw.Api();
wgPageName = mw.config.get("wgPageName");
wgRevisionId = mw.config.get("wgRevisionId");
noticeHTML = {
loading: "<div class=\"mw-_addText-preview-loading\"><div class=\"quotebox\" style=\"margin: auto; width: 50%; padding: 6px; border: 1px solid #aaa; font-size: 88%; background-color: #F9F9F9;\"><div class=\"mw-_addText-preview-loading-content\" style=\"background-color: #F9F9F9; color: black; text-align: center; font-size: larger;\"><img src=\"https://img.moegirl.org.cn/common/d/d1/Windows_10_loading.gif\" decoding=\"async\" data-file-width=\"64\" data-file-height=\"64\" style=\"width: 32px; height: 32px;\"> ".concat(wgULS("预览加载中...", "預覽載入中..."), " </div></div></div>"),
fail: "<img src=\"//upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Gnome-dialog-warning2.svg/32px-Gnome-dialog-warning2.svg.png\" decoding=\"async\" srcset=\"//upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Gnome-dialog-warning2.svg/48px-Gnome-dialog-warning2.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Gnome-dialog-warning2.svg/64px-Gnome-dialog-warning2.svg.png 2x\" data-file-width=\"48\" data-file-height=\"48\" width=\"32\" height=\"32\">".concat(wgULS("预览加载失败", "預覽載入失敗"))
};
mwConfigIfMatchInLowerCase = function (key, preferValues) {
var data = getCleanText(mw.config.get(key)).toLowerCase();
if (!data) {
return false;
}
return (Array.isArray(preferValues) ? preferValues : preferValues ? [preferValues] : []).includes(data);
};
getLanguage = function () {
var lang = mw.config.get("wgUserLanguage");
if (lang.includes("zh")) {
return mw.config.get("wgUserVariant");
}
return lang;
};
addLoadingNotice = function () { return addParsedWikitext(noticeHTML.loading); };
loadingFailNotice = function () { return $(".mw-_addText-preview-loading-content").html(noticeHTML.fail); };
removeLoadingNotice = function () { return $(".mw-_addText-preview-loading").empty(); };
addParsedWikitext = function (html) {
var previewLoading = $(".mw-_addText-preview-loading");
if (previewLoading.length > 0) {
previewLoading.html(html);
return;
}
var node = $(html);
for (var _i = 0, _a = [".diff-currentversion-title", ".previewnote", ".mw-undelete-revision", "#mw-content-text"]; _i < _a.length; _i++) {
var selector = _a[_i];
var anchor = $(selector);
if (anchor.length > 0) {
anchor.after(node);
return;
}
}
};
ifNeedPreview = function () { return document.body.innerHTML.includes("_addText"); };
mwAddWikiText = function (wikitext, pagename, isPreview) { return __awaiter(void 0, void 0, void 0, function () {
var text, params, data, parsedWikitext, parsedHTML, e_1;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
text = getCleanText(wikitext);
if (text.length === 0) {
removeLoadingNotice();
return [2 /*return*/];
}
params = {
action: "parse",
uselang: getLanguage(),
useskin: mw.config.get("skin"),
title: pagename,
text: text,
contentmodel: "wikitext",
prop: "text",
format: "json"
};
if (isPreview) {
params.preview = true;
params.disableeditsection = true;
}
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, api.post(params)];
case 2:
data = _c.sent();
parsedWikitext = (_b = (_a = data === null || data === void 0 ? void 0 : data.parse) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b["*"];
if (!parsedWikitext) {
return [2 /*return*/];
}
parsedHTML = getCleanText(parsedWikitext);
if (parsedHTML !== "") {
addParsedWikitext(parsedHTML);
}
else {
removeLoadingNotice();
}
return [3 /*break*/, 4];
case 3:
e_1 = _c.sent();
console.error(e_1);
loadingFailNotice();
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
mwAddLuaText = function (wikitext, isPreview, callback) { return __awaiter(void 0, void 0, void 0, function () {
var temp_module_name, module_call, text, params, data, parsedWikitext, parsedHTML, e_2;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
temp_module_name = "AddText/Temp/Module/Data.lua";
module_call = {
wikitext: "#invoke:",
pagename: "Module:"
};
text = getCleanText(wikitext);
if (text.length === 0) {
removeLoadingNotice();
return [2 /*return*/];
}
params = {
action: "parse",
uselang: getLanguage(),
useskin: mw.config.get("skin"),
format: "json",
title: wgPageName,
text: "{{".concat(module_call.wikitext).concat(temp_module_name, "|main}}"),
prop: "text",
contentmodel: "wikitext",
templatesandboxtitle: module_call.pagename + temp_module_name,
templatesandboxtext: "return {main=function()\nxpcall(function()\n".concat(wikitext, "\nend,function()end)\nlocal moduleWikitext = package.loaded[\"Module:ModuleWikitext\"]\nif moduleWikitext then\nlocal wikitext = moduleWikitext.main()\nif mw.text.trim(wikitext)~=''then\nreturn mw.getCurrentFrame():preprocess(moduleWikitext.main())\nend\nend\nreturn ''\nend}"),
templatesandboxcontentmodel: "Scribunto",
templatesandboxcontentformat: "text/plain"
};
if (isPreview) {
params.preview = true;
params.disableeditsection = true;
}
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, api.post(params)];
case 2:
data = _c.sent();
parsedWikitext = (_b = (_a = data === null || data === void 0 ? void 0 : data.parse) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b["*"];
if (!parsedWikitext) {
return [2 /*return*/];
}
parsedHTML = getCleanText(parsedWikitext);
if (parsedHTML !== "") {
if (!$(parsedHTML).find(".scribunto-error").text().includes(temp_module_name)) {
if (typeof callback === "function") {
callback(parsedHTML);
}
else {
addParsedWikitext(parsedHTML);
}
}
else {
removeLoadingNotice();
}
}
else {
removeLoadingNotice();
}
return [3 /*break*/, 4];
case 3:
e_2 = _c.sent();
console.error(e_2);
loadingFailNotice();
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
mwGetFromNowRevision = function () { return __awaiter(void 0, void 0, void 0, function () {
var data, parsedWikitext, parsedHTML, pageContent, e_3;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
return [4 /*yield*/, api.post({
action: "parse",
oldid: wgRevisionId,
prop: "wikitext"
})];
case 1:
data = _c.sent();
parsedWikitext = (_b = (_a = data === null || data === void 0 ? void 0 : data.parse) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b["*"];
if (!parsedWikitext) {
return [2 /*return*/];
}
parsedHTML = lua_check(getCleanText(parsedWikitext));
pageContent = getCleanText("".concat(document.querySelector("#mw-clearyourcache") ? "{{#invoke:SpecialWikitext/Template|int|clearyourcache}}" : "").concat(parsedHTML));
if (pageContent) {
mwAddWikiText(pageContent, wgPageName, true);
}
else {
removeLoadingNotice();
}
return [3 /*break*/, 3];
case 2:
e_3 = _c.sent();
console.error(e_3);
removeLoadingNotice();
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
mwApplyNotice = function (pagename, subPagename) { return __awaiter(void 0, void 0, void 0, function () {
var data, parsedWikitext, parsedHTML, e_4;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
return [4 /*yield*/, api.post({
action: "parse",
uselang: getLanguage(),
useskin: mw.config.get("skin"),
title: "".concat(pagename).concat(subPagename),
text: "".concat("{{#invoke" + ":SpecialWikitext/Template|getNotices|").concat(pagename, "|").concat(subPagename, "}}"),
prop: "text",
format: "json"
})];
case 1:
data = _c.sent();
parsedWikitext = (_b = (_a = data === null || data === void 0 ? void 0 : data.parse) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b["*"];
if (!parsedWikitext) {
return [2 /*return*/];
}
parsedHTML = getCleanText(parsedWikitext);
if (getCleanText($(parsedHTML).text())) {
addParsedWikitext(parsedHTML);
}
return [3 /*break*/, 3];
case 2:
e_4 = _c.sent();
console.error(e_4);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
(function () {
if (mwConfigIfMatchInLowerCase("wgPageContentModel", ["javascript", "js", "json", "text", "css", "sanitized-css"])) {
if (document.querySelector(".previewnote")) {
var previewNode = $(".previewnote .mw-message-box-warning > p > b a");
if (previewNode.length > 0) {
var path = decodeURI(previewNode.attr("href")).replace(/^\//, "");
if (path !== wgPageName) {
return;
}
}
var addWikitext = "".concat(lua_check());
if (addWikitext) {
addLoadingNotice();
mwAddWikiText(addWikitext, wgPageName, true);
}
}
else if (!document.querySelector(".mw-_addText-content") && mwConfigIfMatchInLowerCase("wgAction", "view")) {
if (!ifNeedPreview()) {
return;
}
$("#mw-clearyourcache").empty();
if (!document.querySelector("#wpTextbox1")) {
addLoadingNotice(); //放置提示,提示使用者等待AJAX
mwGetFromNowRevision();
}
}
else if (document.querySelector("#mw-revision-info") && mwConfigIfMatchInLowerCase("wgAction", "view")) {
if (!document.querySelector("#wpTextbox1")) {
$("#mw-clearyourcache").html(noticeHTML.loading);
mwGetFromNowRevision();
}
}
else {
removeLoadingNotice();
}
}
else if (mwConfigIfMatchInLowerCase("wgPageContentModel", ["scribunto", "lua"])) {
if (!ifNeedPreview()) {
return;
}
if (document.querySelector("#wpTextbox1") && document.querySelector("table.diff") && !document.querySelector(".previewnote") && !mwConfigIfMatchInLowerCase("wgAction", "view")) {
$("#wikiDiff").after(noticeHTML.loading);
mwAddLuaText($("#wpTextbox1").val(), true);
}
}
else if (document.querySelector(".mw-undelete-revision")) {
if (!ifNeedPreview()) {
return;
}
if (document.querySelector(".specialWikitextJSON, pre, .mw-json")) {
var $tryGetWiki = $("textarea").val();
var tryAddWiki = getCleanText(lua_getJSONwikitext($tryGetWiki)) || getCleanText(lua_getCSSwikitext($tryGetWiki));
if (tryAddWiki) {
addLoadingNotice();
mwAddWikiText(tryAddWiki, mw.config.get("wgRelevantPageName"), true);
}
else if (/Module[_ ]wikitext.*_addText/i.exec($(".mw-parser-output").text())) {
// 预览失效时启用下方一行
// mwAddLuaText($tryGetWiki, mw.config.get("wgRelevantPageName"), true);
}
}
}
else if (!document.querySelector(".mw-editnotice") && mwConfigIfMatchInLowerCase("wgCanonicalNamespace", "special")) {
var fullPagename = getCleanText(mw.config.get("wgCanonicalSpecialPageName"));
var subPagename = fullPagename.replace(RegExp("^".concat(mw.config.get("wgCanonicalNamespace"), ":[^/]+")), "");
if (fullPagename) {
var fullpagename = "".concat(mw.config.get("wgCanonicalNamespace"), ":").concat(fullPagename);
mwApplyNotice(fullpagename, subPagename);
}
}
else {
removeLoadingNotice();
}
})();
(function () { return __awaiter(void 0, void 0, void 0, function () {
var testcaseList, wikitextPackages, _loop_1, _i, testcaseList_1, testcaseNode, wikitextPackage, wikitext, params, data, parsedWikitext, parsedHTML, parsedNodes, i, e_5;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!ifNeedPreview()) {
return [2 /*return*/];
}
testcaseList = document.querySelectorAll(".special-wikitext-preview-testcase");
if (testcaseList.length <= 0) {
return [2 /*return*/];
}
wikitextPackages = [];
_loop_1 = function (testcaseNode) {
var codeNode = testcaseNode.querySelector(".specialWikitextJSON, pre, .mw-json");
if (!codeNode) {
return "continue";
}
var lang = getCleanText(codeNode.getAttribute("lang")).toLowerCase();
var code = getCleanText(codeNode.innerText);
if (!code) {
return "continue";
}
if (["javascript", "js", "css", "json", "wiki", "text"].includes(lang)) {
var addWikitext = getCleanText(lua_check(code, lang));
if (addWikitext) {
var i = "".concat(wikitextPackages.length);
$(testcaseNode).attr("data-preview-id", i).prepend(noticeHTML.loading);
wikitextPackages.push("<div class=\"special-wikitext-preview-testcase\" data-preview-id=\"".concat(i, "\">\n").concat(addWikitext, "\n</div>"));
}
}
else if (["lua", "scribunto"].includes(lang)) {
mwAddLuaText(code, true, function (wikitext) { return $(testcaseNode).prepend(wikitext); });
}
};
for (_i = 0, testcaseList_1 = testcaseList; _i < testcaseList_1.length; _i++) {
testcaseNode = testcaseList_1[_i];
_loop_1(testcaseNode);
}
wikitextPackage = getCleanText(wikitextPackages.join(""));
if (!wikitextPackage) {
return [2 /*return*/];
}
wikitext = "<div class=\"special-wikitext-preview-testcase-container\">".concat(wikitextPackage, "</div>");
params = {
action: "parse",
text: wikitext,
contentmodel: "wikitext",
prop: "text",
format: "json",
preview: true,
disableeditsection: true
};
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, api.post(params)];
case 2:
data = _c.sent();
parsedWikitext = (_b = (_a = data === null || data === void 0 ? void 0 : data.parse) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b["*"];
if (!parsedWikitext) {
return [2 /*return*/];
}
parsedHTML = getCleanText(parsedWikitext);
if (!parsedHTML) {
return [2 /*return*/];
}
parsedNodes = $(parsedHTML);
for (i = 0; i <= wikitextPackages.length; i++) {
$(".special-wikitext-preview-testcase[data-preview-id=".concat(i, "]")).find(".mw-_addText-preview-loading").before(parsedNodes.find(".special-wikitext-preview-testcase-container > .special-wikitext-preview-testcase[data-preview-id=".concat(i, "]")));
}
removeLoadingNotice();
return [3 /*break*/, 4];
case 3:
e_5 = _c.sent();
console.error(e_5);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); })();
return [2 /*return*/];
}
});
}); })();
// </pre>