跳转到内容

User:魔琴/gadgets/todo/index.js

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

    function han(s, t) {
        return wgULS(s, t);
        //return HanAssist.localize( { hans: s, hant: t } );
    }

    var API = new mw.Api();
    var cmt = "";
    var user = mw.config.get('wgUserName');
    var page = mw.config.get('wgPageName');
    var dl
    if (user === null)
        return;

    var personallink = mw.util.addPortletLink('p-personal', '/wiki/Special:Mypage/TODO', 'TODO', 'pt-zv-todo', han('待办事项', '待辦事項'), null, '#pt-sandbox');
    var link = mw.util.addPortletLink('p-cactions', 'javascript:void(0)', '+TODO', 'ca-zv-todo', han('列入待办事项', '列入待辦事項'));
    $(link).click(function (event) {
        main();
    });

    if (page === 'User:' + user + '/TODO') {
        listpage()
    }

    function main() {
        cmt = "";

        var html =
            '<div id="zv-todo-dialog">' +
            '<p>' + han('备注', '備註') +
            '<textarea name="zv-todo-cmt" id="zv-todo-cmt" rows=2 placeholder=' + han('按下回车,自动保存', '按下Enter,自動保存') + '></textarea>' +
            '</div>';

        if (dl) {
            dl.html(html).closest('.ui-dialog-content').dialog('destroy').remove();
            dl = null;
        }

        dl = $(html).dialog({
            title: han('加入待办事项', '加入待辦事項'),
            minWidth: 600,
            minHeight: 200,
            buttons: [
                {
                    text: '保存',
                    click: function () {
                        processCmt();
                        post();
                        $(this).dialog('close');
                    }
                },
                {
                    text: '取消',
                    click: function () {
                        processCmt();
                        $(this).dialog('close');
                    }
                }
            ]
        });

        $('#zv-todo-cmt').val(cmt);

        $('#zv-todo-cmt').keypress(function (e) {
            if (e.keyCode == $.ui.keyCode.ENTER) {
                processCmt();
                post();
                $('#zv-todo-dialog').dialog('close');
            }

        });

    }

    function post() {
        a = '\n# [[:' + page + ']]:' + cmt + '&nbsp;——~~~~~'
        pro = API.postWithToken('csrf', {
            action: 'edit',
            title: 'User:' + user + '/TODO',
            appendtext: a,
            summary: '+ ' + page + ' via [[User:魔琴/gadgets/todo|TODO]]'
        }).then(function () {
            mw.notify('添加成功');
            cmt = ""
        }, function (e) {
            mw.notify(e);
        });
    }

    function processCmt() {
        cmt = $('#zv-todo-cmt').val()
    }


    function listpage() {
        mw.loader.load('/w/index.php?title=User:魔琴/gadgets/todo/css.css&action=raw&ctype=text/css', 'text/css');
        no = 0
        $('#mw-content-text li').each(function () {
            no = no + 1
            $(this).prepend('<a class=todo-listpage-remove todo-li=' + no + ' title="移除">[ - ]</a>')
        });

        $('a.todo-listpage-remove').click(function (e) {
            e.preventDefault();
            n = $(this).attr('todo-li')
            getList = API.post({
                action: 'parse',
                page: page,
                prop: 'wikitext',
                formatversion: '2'
            }).done(function (data) {
                o = data.parse.wikitext;
                var lines = o.split('\n');
                var hashLines = lines.filter(line => line.trim().startsWith('#'));
                var lineToRemove = hashLines[n - 1];
                var itemToRemove = lineToRemove.replace(/^.*\[\[\:(.*?)\]\].*$/, '$1')
                var updatedLines = lines.filter(line => line !== lineToRemove);
                r = updatedLines.join('\n');
                postPromise = API.postWithToken('csrf', {
                    action: 'edit',
                    title: page,
                    text: r,
                    summary: '- ' + itemToRemove + ' via [[User:魔琴/gadgets/todo|TODO]]'
                }).then(function () {
                    $('#mw-content-text').append('<style>a.todo-listpage-remove{display:none;}</style>')
                    mw.notify(han('发布成功,正在刷新', '發佈成功,正在重新整理'))
                    setTimeout(function () {
                        document.location.reload();
                    }, 4000);
                }).fail(function (e) {
                    mw.notify(e);
                })
            })
        });
    }

});


//</nowiki>