MediaWiki:Common.js:修订间差异

HW
HW留言 | 贡献
HW
HW留言 | 贡献 (清空全部内容)
第1行: 第1行:
// 立即执行闭包,避免污染全局变量
(function() {
    // 1. 提前检查皮肤,不是 Minerva 直接退出
    if (mw.config.get('skin') !== 'minerva') return;


    // 2. 预获取页面信息(mw.config 在页面头部就已加载,无需等待)
    const pageTitle = mw.config.get('wgPageName');
    const isTalkPage = mw.config.get('wgNamespaceNumber') % 2 === 1;
    const isExistingPage = mw.config.get('wgArticleId') > 0;
    // 3. 预构造按钮 HTML(比动态创建元素更快)
    const editUrl = mw.util.getUrl(pageTitle, { action: 'edit' });
    const historyUrl = mw.util.getUrl(pageTitle, { action: 'history' });
    const talkTitle = isTalkPage ? pageTitle.replace(/^Talk:/, '') : `Talk:${pageTitle}`;
    const talkUrl = mw.util.getUrl(talkTitle);
    const buttonsHtml = `
        <div id="custom-wiki-bottom-bar" style="
            margin: 2.5rem auto 1rem;
            padding: 1rem 0;
            border-top: 1px solid #e0e0e0;
            text-align: center;
            max-width: 800px;
        ">
            <a href="${editUrl}" style="
                display: inline-block;
                margin: 0 0.75rem;
                padding: 0.5rem 1.25rem;
                background-color: #3366cc;
                color: white;
                text-decoration: none;
                border-radius: 4px;
                font-size: 0.95rem;
                font-weight: 500;
                transition: opacity 0.2s;
            " onmouseover="this.style.opacity='0.85'" onmouseout="this.style.opacity='1'">
                ${isExistingPage ? '编辑页面' : '创建页面'}
            </a>
            <a href="${historyUrl}" style="
                display: inline-block;
                margin: 0 0.75rem;
                padding: 0.5rem 1.25rem;
                background-color: #72777d;
                color: white;
                text-decoration: none;
                border-radius: 4px;
                font-size: 0.95rem;
                font-weight: 500;
                transition: opacity 0.2s;
            " onmouseover="this.style.opacity='0.85'" onmouseout="this.style.opacity='1'">
                查看历史
            </a>
            <a href="${talkUrl}" style="
                display: inline-block;
                margin: 0 0.75rem;
                padding: 0.5rem 1.25rem;
                background-color: #202122;
                color: white;
                text-decoration: none;
                border-radius: 4px;
                font-size: 0.95rem;
                font-weight: 500;
                transition: opacity 0.2s;
            " onmouseover="this.style.opacity='0.85'" onmouseout="this.style.opacity='1'">
                ${isTalkPage ? '返回条目' : '讨论页'}
            </a>
        </div>
    `;
    // 4. 【关键】DOM 一就绪就插入,不等图片/样式/钩子
    function insertBar() {
        const content = document.querySelector('main#content, #content');
        if (content && !document.getElementById('custom-wiki-bottom-bar')) {
            content.insertAdjacentHTML('beforeend', buttonsHtml);
        }
    }
    // 优先用 DOMContentLoaded,比 MediaWiki 的钩子快得多
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', insertBar);
    } else {
        insertBar(); // 如果 DOM 已经就绪,直接执行
    }
})();