跳转到内容

User:魔琴/gadgets/3007-zero/index.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
mw.loader.using('mediawiki.util').done(function () {

    if (mw.config.get("wgNamespaceNumber") < 0) {
        return;
    }

    var p = mw.config.get("wgPageName");
    var API = new mw.Api();
    var linkp = mw.util.addPortletLink('p-cactions', '#', '3007 Zero', 'ca-3007-zero', '将U+25CB改为U+3007');
    $(linkp).click(function (event) {
        parse();
    });

    function parse() {
        var getText = API.post({
            action: 'parse',
            page: p,
            prop: 'wikitext',
            formatversion: '2'
        }).done(function (data) {
            a = data.parse.wikitext
            b = null
            ref = []
            i = 0

            do {
                if (b !== null) {
                    a = b
                }
                ref[i] = a.replace(/^(.|\r|\n)*?(<\s*ref[^\/]*?\/\s*>)(.|\r|\n)*?$/, '$2')
                b = a.replace(/<\s*ref[^\/]*?\/\s*>/, '<@zauberviolino-3007-zero-ref--' + i + '/>')
                i = i + 1
            } while (a !== b);

            i = i - 1

            do {
                if (b !== null) {
                    a = b
                }
                ref[i] = a.replace(/^(.|\r|\n)*?(<\s*?ref(.|\r|\n)*?>(.|\r|\n)*?<\s*?\/\s*?ref\s*?>)(.|\r|\n)*?$/, '$2')
                b = a.replace(/<\s*?ref(.|\r|\n)*?>(.|\r|\n)*?<\s*?\/\s*?ref\s*?>/, '<@zauberviolino-3007-zero-ref--' + i + '/>')
                i = i + 1
            } while (a !== b);

            // length需要减去2

            a = b

            a = a.replace('○', '〇')

            for (j = 0; j < ref.length - 1; j++) {
                a = a.replace('<@zauberviolino-3007-zero-ref--' + j + '/>', ref[j])
            }

            var getDiff = API.post({
                action: 'compare',
                format: 'json',
                fromtitle: p,
                toslots: 'main',
                prop: 'diff',
                "totext-main": a,
                formatversion: '2'
            }).done(function (data) {
                var dl = null;
                var html = data.compare.body
                    ? '<div id="3007-zero-table" style="overflow-y:scroll"><table class="diff"><colgroup><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content"></colgroup><tbody>' + data.compare.body + '</tbody></table></div>'
                    : '<div style="margin:20px;font-size:18px;text-align:center;" id="3007-zero-table">无差异</div>';
                if (dl) {
                    dl.html(html).dialog("open");
                    return null;
                }
                if (data.compare.body) {
                    dl = $(html).dialog({
                        title: '3007 Zero',
                        minWidth: 600,
                        minHeight: 100,
                        buttons: [{
                            text: "确定",
                            click: function () {
                                edit(a);
                                $(this).dialog('close');
                                $("#3007-zero-table").closest(".ui-dialog").remove();
                            }
                        },
                        {
                            text: "取消",
                            click: function () {
                                $(this).dialog('close');
                            }
                        }
                        ]
                    });
                } else {
                    dl = $(html).dialog({
                        title: '3007 Zero',
                        minWidth: 600,
                        minHeight: 100,
                        buttons: [{
                            text: "关闭",
                            click: function () {
                                $(this).dialog('close');
                            }
                        }
                        ]
                    });
                }

            }).fail(function (error) {
                mw.notify("错误:" + error);
            });

        }).fail(function (error) {
            mw.notify("错误:" + error);
        });

    }


    function edit(t) {
        var revId = mw.config.get('wgRevisionId');
        var checkId = API.post({
            action: 'parse',
            page: p,
            prop: 'revid',
            formatversion: '2'
        }).done(function (data) {
            if (data.parse.revid !== revId) {
                mw.notify('编辑冲突,请重试。');
            } else {
                var promise = API.postWithToken('csrf', {
                    action: 'edit',
                    title: p,
                    text: t,
                    summary: '[[User:魔琴/gadgets/3007-zero|U+25CB --> U+3007]]'
                }).then(function (result) {
                    mw.notify("编辑成功,将自动刷新页面");
                    setTimeout(function () {
                        window.location.reload();
                    }, 4000);
                }).fail(function (error) {
                    mw.notify("错误:" + error);
                });
            }
        }).fail(function (error) {
            mw.notify("错误:" + error);
        });
    }

});