跳转到内容

User:Hotaru Natsumi/js/wikiplus-section-name-patch.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// 此js用于wikiplus自动摘要的锚点修复,会在window下挂一个全局函数sectionNum2sectionAnchor,参数1是wikiplus的段落编号
// 理论上将摘要配置中的 arg1.sectionName 替换为 sectionNum2sectionAnchor(arg1.sectionNum, arg1.sectionName) 即可

(function() {
    'use strict';
    window.mwPatchSections = void 0;

    function _setData() {
        if(window.mwPatchSections !== void 0){
            return;
        }
        try{
            fetch('/w/api.php?action=parse&page=' + mw.config.get('wgPageName') + '&prop=sections&format=json').then((res)=>{
                res.json().then((res)=>{
                    window.mwPatchSections = res;
                });
            });
        }catch(e){
            // pass
        }
    }
    _setData();

    let n = 3;

    while(n--){
        setTimeout(_setData, (1+n)*1000);
    }

    window.sectionNum2sectionAnchor = function(n, _default = '') {
        if(window.mwPatchSections == void 0){
            return _default;
        };
        let ret = _default;
        try{
            window.mwPatchSections.parse.sections.forEach((k, v)=>{
                if(k.index == n) {
                    ret = k.linkAnchor;
                }
            });
        }catch(e){
            // pass
        }
        return ret;
    }
})();