User:Lower/js/moesync.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
//<pre>
var _addText = '{{NotMGP}}';
/**
* -------------------------------------------------------------------------
* 开发者:User:实验性:无用论废人 OOUI实现:User:屠麟傲血
* -------------------------------------------------------------------------
*/
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
$(function () {
/*函数执行体*/
if (mw.config.get("wgAction") === 'edit' || mw.config.get("wgIsArticle")) {
$(mw.util.addPortletLink("p-cactions", "javascript:;", "同步萌百", "ca-sync", "同步萌百同名" + wgULS("页面", "頁面"))
).on("click", function () {
mw.notify(wgULS("请求中...", "請求中..."));
// remove old window manager, which causes API error.
$('#moe-background').remove();
var diffWindow = new syncDialog({
id: "moe-background",
size: 'larger'
});
// Create and append a window manager, which opens and closes the window.
var windowManager = new OO.ui.WindowManager();
$("body").append(windowManager.$element);
windowManager.addWindows([diffWindow]);
// Open the window!
windowManager.openWindow(diffWindow);
setTimeout(moesync_jsonp(), 200);
});
}
/*預備變量*/
var moe_data = "";
var api = new mw.Api();
//点名批评 mediawiki.ForeignApi 不能直接用!
var zhmoeapi = new mw.ForeignApi("https://mzh.moegirl.org.cn/api.php", { anonymous: true });
var neterr = wgULS('网络连接出错', "網路連接出錯", null, null, "網絡連接出錯");
// 预备函数(请求)
function moesync_jsonp() {
zhmoeapi.get({
action: "query",
format: "json",
formatversion: 2,
titles: mw.config.get("wgPageName"),
prop: "revisions",
rvprop: "content"
}).then(function (result) {
moe_data = result.query.pages[0].revisions[0].content;
moesync_compare();
}, function (e) {
moesync_dont();
if (e == "missingtitle") {
mw.notify('萌百' + wgULS('无对应页面', "無對應頁面"), { type: "warn" });
} else {
mw.notify(neterr, { type: "error" });
}
});
}
function moesync_compare() {
api.post({
'action': 'compare',
'format': 'json',
'fromtitle': mw.config.get("wgPageName"),
'toslots': 'main',
'totext-main': moe_data,
'prop': 'diff'
}).then(function (data) {
if (data.compare['*']) {
$('#moesync-diff').append(data.compare['*']);
mw.notify(wgULS("请求", "請求") + '完成', { type: "success" });
} else {
moesync_dont();
mw.notify('萌百' + wgULS('与镜像站内容一致', "與鏡像站內容一致"), { type: "warn" });
}
}, function (e) {
if (e == "missingtitle") {
moesync_compare_create();
} else {
moesync_dont();
mw.notify(neterr, { type: "error" });
}
});
}
function moesync_compare_create() {
api.post({
'action': 'compare',
'format': 'json',
'fromslots': 'main',
'fromtext-main': '',
'toslots': 'main',
'totext-main': moe_data,
'prop': 'diff'
}).then(function (data) {
$('#moesync-diff').append(data.compare['*']);
mw.notify(wgULS("请求", "請求") + '完成', { type: "success" });
}, function (e) {
moesync_dont();
mw.notify(neterr, { type: "error" });
});
}
function moesync_edit(summary, watchpage, minoredit) {
var params = {
action: 'edit',
format: 'json',
title: mw.config.get('wgPageName'),
text: moe_data,
summary: summary,
tags: 'Automation tool',
watchlist: watchpage,
bot: mw.config.get("wgUserGroups").includes("flood"),
};
if (minoredit) {
params.minor = true;
} else {
params.notminor = true;
}
api.postWithToken('csrf', params).done(function (data) {
mw.notify(wgULS("即将刷新……", "即將刷新……"), {
title: "提交完成",
type: "success",
tag: "moesync"
});
$('#moe-background').hide(200);
setTimeout(location.reload(), 730);
});
}
// 报错时关闭提交按钮
function moesync_dont() {
$("#moe-background .oo-ui-processDialog-actions-primary").remove();
$("#moe-background input").attr("disabled", "disabled");
$('.oo-ui-fieldLayout').css("color", "#72777d");
}
// 预备函数(DOM构造)
var syncDialog = /** @class */ (function (_super) {
__extends(syncDialog, _super);
function syncDialog(config) {
// Parent constructor
return _super.call(this, config) || this;
}
syncDialog.prototype.initialize = function () {
// Parent method
_super.prototype.initialize.call(this);
this.panelLayout = new OO.ui.PanelLayout({
scrollable: false,
expanded: false,
padded: true
});
this.summaryBox = new OO.ui.TextInputWidget({
value: "来源于萌娘百科,依CC BY-NC-SA 3.0 CN导入,原贡献者请参见来源页面历史",
});
var summaryField = new OO.ui.FieldLayout(this.summaryBox, {
label: wgULS("编辑摘要", "編輯摘要"),
align: "top",
id: "diff-summary",
});
this.watchlistBox = new OO.ui.CheckboxInputWidget({
selected: false,
});
var watchlistField = new OO.ui.FieldLayout(this.watchlistBox, {
label: wgULS("监视本页", "監視此頁面"),
align: "inline",
id: "watchlist",
});
this.minorBox = new OO.ui.CheckboxInputWidget({
selected: false,
});
var minorField = new OO.ui.FieldLayout(this.minorBox, {
label: wgULS("这是一次小编辑", "這是次要編輯"),
align: "inline",
id: "minor",
});
this.panelLayout.$element.append(summaryField.$element, minorField.$element, watchlistField.$element); //按钮合成
this.content = new OO.ui.BookletLayout({ padded: true, expanded: false, id: "moe-diff-div" });
this.content.$element.append('<table class="diff moe-diff">' +
'<colgroup>' +
'<col class="diff-marker">' +
'<col class="diff-content">' +
'<col class="diff-marker">' +
'<col class="diff-content">' +
'</colgroup>' +
'<tbody id="moesync-diff">' +
'<tr>' +
'<td colspan="2" class="diff-lineno" id="mw-diff-left-l1">' +
wgULS("镜像站(编辑前内容):", "鏡像站(編輯前內容):") +
'</td>' +
'<td colspan="2" class="diff-lineno">' +
'萌百' + wgULS("(编辑后内容):", "(編輯後內容):") +
'</td>' +
'</tr> ' +
'</tbody>' +
'</table>');
this.$body.append(this.content.$element, this.panelLayout.$element);
};
syncDialog.prototype.getActionProcess = function (action) {
var _this = this, trcount = $("tbody#moesync-diff > tr").eq(1).length;
if (action === "cancel") {
return new OO.ui.Process(function () {
_this.close({ action: action });
}, this);
} else if (trcount == 0 && action === "submit") {
return new OO.ui.Process(function () {
mw.notify(wgULS("API未加载完成,请您坐和放宽", "API未加載完成,請您坐和放寬"), {
type: "error",
title: wgULS("镜像站提醒您", "鏡像站提醒您"),
tag: "unloaded"
});
}, this);
} else if (action === "submit" && trcount == 1) {
return new OO.ui.Process(function () {
var summary = _this.summaryBox.getValue();
var watchpage = _this.watchlistBox.isSelected() ? "watch" : "nochange";
var minoredit = _this.minorBox.isSelected();
moesync_edit(summary, watchpage, minoredit);
}, this);
}
// Fallback to parent handler
return _super.prototype.getActionProcess.call(this, action);
};
syncDialog.prototype.getBodyHeight = function () {
return Math.round($(window).height() * 0.9);
};
syncDialog.static = __assign(__assign({}, _super.static), {
name: "moesync", title: "同步萌百同名" + wgULS("页面", "頁面"), actions: [
{
action: "cancel",
label: "取消",
flags: ["safe", "close", "destructive"],
},
{
action: "submit",
label: "提交" + wgULS("编辑", "編輯"),
flags: ["primary", "progressive"],
},
]
});
return syncDialog;
}(OO.ui.ProcessDialog));
});
//</pre>