User:Shizhao/follow-up.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/* fork [[User:Xiplus/js/close-rfpp.js]], use in [[template:follow-up]] */
(function() {

    if (typeof Followup == 'undefined')
        Followup = {};

    if (typeof Followup.summarySuffix == 'undefined') {
        Followup.summarySuffix = ' via [[User:Shizhao/follow-up.js|follow-up]]';
    }

    if (!/^Wikipedia:(頁面|檔案)存廢討論\/記錄\/\d+\/\d+\/\d+$/.test(mw.config.get('wgPageName'))
        || mw.config.get('wgAction') !== 'view'
        || mw.config.get('wgRevisionId') !== mw.config.get('wgCurRevisionId')) {
        return;
    }

	mw.util.addCSS(`
	    a.fu-link {
	        color: green;
	        font-weight: bold;
	    }
	    a.fu-link-closed {
	        color: gray;
	    }
	    `);

    var getPageContent = new Promise(function(resolve, reject) {
        new mw.Api().get({
            action: 'query',
            prop: 'revisions',
            rvprop: ['content', 'timestamp'],
            titles: mw.config.get('wgPageName'),
            formatversion: '2',
            curtimestamp: true,
        }).then(function(data) {
            var page, revision;
            if (!data.query || !data.query.pages) {
                mw.notify('未能抓取頁面內容(unknown)');
                reject('unknown');
            }
            page = data.query.pages[0];
            if (!page || page.invalid) {
                mw.notify('未能抓取頁面內容(invalidtitle)');
                reject('invalidtitle');
            }
            if (page.missing) {
                mw.notify('未能抓取頁面內容(nocreate-missing)');
                reject('nocreate-missing');
            }
            revision = page.revisions[0];
            var content = revision.content;
            var basetimestamp = revision.timestamp;
            var curtimestamp = data.curtimestamp;
            resolve({
                content: content,
                basetimestamp: basetimestamp,
                curtimestamp: curtimestamp,
            });
        });
    }
    );

    function showFollowupButton() {
        var titles = $('#bodyContent').find('h2:has(.mw-headline), h3:has(.mw-headline)');

        titles.each(function(key, current) {
            var title = $(current).find('.mw-headline')[0].id;

            var fuLink = document.createElement('a');
            fuLink.href = '#';
            fuLink.className = 'fu-link';
            fuLink.innerText = '待处理';
            $(fuLink).on('click', function() {
                processFu(key+1, title);
                return false;
            });

            var node = current.getElementsByClassName('mw-editsection')[0];
            var delDivider = document.createElement('span');
            delDivider.appendChild(document.createTextNode(' | '));
            node.insertBefore(delDivider, node.childNodes[1]);
            node.insertBefore(fuLink, node.childNodes[1]);
        });
    }

    function processFu(sectionid, title) {
        mw.loader.using(['jquery.ui'], function() {
            var html = '<div>';
            html += '{{Follow-up}}处理方式(可选)<br>';
            html += '<input type="text" class="comment" size="85"><br>';
            html += '編輯摘要<br>';
            html += '<input type="text" class="summary" size="85" value="待处理">';
            html += '</div>';
            $(html).dialog({
                title: '待处理的存废讨论 - ' + title,
                minWidth: 590,
                minHeight: 150,
                buttons: [{
                    text: '確定',
                    click: function() {
                        processEdit(
                            sectionid,
                            title,
                            $(this).find('.comment').val(),
                            $(this).find('.summary').val()
                        );
                        $(this).dialog('close');
                    },
                }, {
                    text: '取消',
                    click: function() {
                        $(this).dialog('close');
                    },
                }],
            });
        });
    }

    function processEdit(sectionid, title, comment, summary) {
        new mw.Api().edit(mw.config.get('wgPageName'), function(revision) {
            var content = revision.content;
            const splittoken = 'CLOSE_SPLIT_TOKEN';
            content = content.replace(/^==/gm, splittoken + '==');
            var contents = content.split(splittoken);
            var newtext = contents[sectionid];
            newtext = newtext.trim();
            comment = '{{subst:' + 'Follow-up|' + comment + '}}';
            comment = comment.trim();
            newtext =  newtext + '\n' + comment;

            $($('#bodyContent').find('h2:has(.mw-headline), h3:has(.mw-headline)')[sectionid-1]).find('.fu-link').addClass('fu-link-closed');
            return {
                text: newtext,
                section: sectionid,
                basetimestamp: revision.timestamp,
                summary: '/* ' + title + ' */ ' + summary + Followup.summarySuffix,
            };
        }).then(function() {
            mw.notify('已修改 ' + title);
        }, function(e) {
            if (e == 'editconflict') {
                mw.notify('修改 ' + title + ' 時發生編輯衝突');
            } else {
                mw.notify('修改 ' + title + ' 時發生未知錯誤:' + e);
            }
        });
    }

    getPageContent.then(function(result) {
        window.content = result.content;
        var lenintext = result.content.split(/^==/gm).length - 1;
        var leninhtml = $('#bodyContent').find('h2:has(.mw-headline), h3:has(.mw-headline)').length;
        if (leninhtml !== lenintext) {
            mw.notify('抓取章節錯誤,在HTML找到 ' + leninhtml + ' 個章節,在原始碼找到 ' + lenintext + ' 個章節');
        } else {
            showFollowupButton();
        }
    });

}
)();