H萌娘衷心希望身在疫区的编辑和读者保重身体。愿世界新型冠状病毒肺炎疫情早日结束!

Difference between revisions of "用户:=海豚=/js/editConflict.js"

From H萌娘
Jump to: navigation, search
(One intermediate revision by the same user not shown)
Line 87: Line 87:
 
                     } catch (e) {
 
                     } catch (e) {
 
                         console.error("editConflict", e);
 
                         console.error("editConflict", e);
                         wpSave.val(i <= 3 ? `正在提交......` : "提交失败,请备份您的编辑内容后刷新界面重新编辑");
+
                         wpSave.val(i <= 3 ? `提交失败!正在重试(第${i}次)......` : "提交失败,请备份您的编辑内容后刷新界面重新编辑");
 
                     }
 
                     }
 
                 }
 
                 }

Revision as of 16:11, 23 May 2020

(async () => {
    if (!["edit", "submit"].includes(mw.config.get("wgAction"))) {
        return;
    }
    await mw.loader.using("mw.Api");
    mw.loader.using('mediawiki.notification');
    const wgPageName = mw.config.get("wgPageName");
    const wgCurRevisionId = mw.config.get("wgCurRevisionId");
    let needCheckFlag = true;
    const api = new mw.Api({ timeout: 5000 });
    const wpSave = $("#wpSave");
    const editform = $("#editform");
    const conflictAlert = "";
    const SYMBOL_UNDEFINED = Symbol("SYMBOL_UNDEFINED");
    wpSave.val = (value = SYMBOL_UNDEFINED) => {
        const val = $.fn.val.bind(wpSave);
        return value === SYMBOL_UNDEFINED ? val() : val(value).attr("title", value);
    };
    const disable = ($buttons) => {
        return $buttons.css("font-weight", "normal").parent().removeClass("oo-ui-widget-enabled oo-ui-flaggedElement-primary oo-ui-flaggedElement-progressive").addClass("oo-ui-widget-disabled");
    }
    wpSave.on("click", () => {
        if (needCheckFlag === true) {
            needCheckFlag = false;
            setTimeout(async () => {
                disable(wpSave.attr("disabled", "disabled").val("正在提交......"));
                for (let i = 1; i <= 4; i++) {
                    try {
                        const result = await api.post({
                            action: "query",
                            prop: "revisions",
                            rvprop: "ids",
                            rvlimit: 1,
                            titles: wgPageName,
                        });
                        const pageid = Object.keys(result.query.pages)[0];
                        if (pageid > 0 && result.query.pages[pageid].revisions[0].revid > wgCurRevisionId) {
                            wpSave.val("正在提交......");
                            const pre = $("<pre/>", {
                                css: {
                                    position: "absolute",
                                    left: "-99999px",
                                    "z-index": "-99999",
                                }
                            });
                            const textarea = $("#wpTextbox1");
                            $("#copyCurrentRawCode").on("click", () => {
                                if ($(".ace_editor").length > 0) {
                                    mw.notify("当前已开启代码编辑器,无法获取真实编辑内容,请手动复制。");
                                    return;
                                }
                                const selection = window.getSelection();
                                const range = selection.getRangeAt(0);
                                pre.text(textarea.val());
                                selection.selectAllChildren(pre[0]);
                                document.execCommand("copy");
                                window.setTimeout(function() {
                                    selection.removeAllRanges();
                                    selection.addRange(range);
                                    pre.empty();
                                }, 0);
                                mw.notify("复制成功!");
                            });
                            $("#showNewestRevisionDiff").on("click", () => {
                                window.open(`https://www.hmoegirl.com/?diff=${result.query.pages[pageid].revisions[0].revid}`, "_blank").focus();
                            });
                            $("#refreshPage").on("click", () => {
                                if (confirm("请注意刷新后原来的编辑内容将会被最新版本的源码替换!\n历时如果您想找回您的编辑请点击还原备份按钮\n点击确定将会刷新页面!!")) {
                                    location.reload(false);
                                }
                            });
                            disable($("#wpSave").on("click", () => {
                                setTimeout(() => {
                                    alert(conflictAlert);
                                }, 1);
                                return false;
                            }).attr("title", conflictAlert));
                            await mw.loader.using('mediawiki.notification');
                            $("#mw-notification-area").css({
                                position: "fixed",
                                top: 0,
                            }).appendTo("body");
                        } else {
                            wpSave.val("正在提交......");
                            editform.submit();
                        }
                    } catch (e) {
                        console.error("editConflict", e);
                        wpSave.val(i <= 3 ? `提交失败!正在重试(第${i}次)......` : "提交失败,请备份您的编辑内容后刷新界面重新编辑");
                    }
                }
                setTimeout(() => {
                    needCheckFlag = true;
                }, 2000);
            }, 1);
        }
    });
})();