MediaWiki:Gadget-Purgecache.js

H萌娘,万物皆可H的百科全书!
imported>=海豚=2021年3月17日 (三) 09:22的版本
跳到导航 跳到搜索

注意:这类代码页面在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

// <pre>
$(function(){
    mw.loader.using("moment").then(function() {
        var li = $('<li/>').appendTo("#p-personal > ul"),
            textNode = $('<span/>'),
            containerNode;
        if (mw.config.get('wgNamespaceNumber') === -1) {
            containerNode = $('<span/>');
            containerNode.css({
                'cursor': 'default',
                'user-select': 'none'
            });
            containerNode.append('特殊页面(').append(textNode).append(')');
        } else {
            containerNode = $('<a/>');
            var statusNode = $('<span/>').text('清除页面缓存'),
                runningStatus = false;
            containerNode.attr("href", 'javascript:void(0);');
            containerNode.append(statusNode).append('(').append(textNode).append(')');
            containerNode.on('click', function() {
                if (runningStatus) return;
                statusNode.text('正在清除页面缓存...');
                statusNode.prepend('<img src="https://www.hmoegirl.com/images/d/d1/Windows_10_loading.gif" style="height: 1em; margin-top: -.25em;">');
                runningStatus = true;
                var api = new mw.Api(),
                    opt = {
                        action: 'purge',
                        format: 'json',
                        forcelinkupdate: true,
                        titles: mw.config.get('wgPageName')
                    };
                api.post(opt).then(function() {
                    setTimeout(function() {
                        api.post(opt).then(function() {
                            statusNode.text('清除页面缓存成功!');
                            setTimeout(location.reload.bind(location), 1000);
                        }, function() {
                            statusNode.text('清除页面缓存失败,点击可重试!');
                            runningStatus = false;
                            setTimeout(function() {
                                if (!runningStatus) statusNode.text('清除页面缓存');
                            }, 5000);
                        });
                    }, 370);
                }, function() {
                    statusNode.text('清除页面缓存失败,点击可重试!');
                    runningStatus = false;
                    setTimeout(function() {
                        if (!runningStatus) statusNode.text('清除页面缓存');
                    }, 5000);
                });
            });
        }
        li.append(containerNode);
        textNode.text(moment().format('A h[:]mm[:]ss'));
        var date = new Date();
        var now = date.getTime();
        date.setMilliseconds(0);
        date.setSeconds(date.getSeconds() + 1);
        setTimeout(function() {
            setInterval(function() {
                textNode.text(moment().format('A h[:]mm[:]ss'));
            }, 1e3);
            textNode.text(moment().format('A h[:]mm[:]ss'));
        }, date.getTime() - now);
    });
    new Image().src = "https://static.mengniang.org/common/d/d1/Windows_10_loading.gif";
});
// </pre>