MediaWiki:Common.js

Ztm0929留言 | 贡献2025年9月25日 (四) 13:14的版本 (增加 Powered by 图标的切换逻辑)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// Cloudflare Web Analytics
var script = document.createElement('script');
script.defer = true;
script.src = 'https://static.cloudflareinsights.com/beacon.min.js';
script.setAttribute('data-cf-beacon', '{"token": "6180aa28fe1943b48c4059a5056f4738"}');
document.head.appendChild(script);
// End Cloudflare Web Analytics

$(function () {
    // 只在 Special:Search 页面运行
    if (mw.config.get("wgCanonicalSpecialPageName") === "Search") {
        const searchTerm = mw.util.getParamValue("search");
        const googleUrl = "https://www.google.com/search?q=" + encodeURIComponent(searchTerm);

        const $googleLink = $("<p>").html(
            `你也可以到 <a href="${googleUrl}" target="_blank" rel="noopener">Google 中搜索“${searchTerm}”</a>。`
        );

        // 优先在有搜索结果时插入
        if ($(".mw-search-results").length) {
            $googleLink.insertAfter(".mw-search-results");
        }
        // 否则在没有结果提示后插入
        else if ($(".mw-search-nonefound").length) {
            $googleLink.insertAfter(".mw-search-nonefound");
        }
    }
});


// logo 切换逻辑
$(function () {
    function updateLogos() {
        var theme = mw.config.get('skin-theme-clientpref'),
            $ccIcon = $('#footer-copyrightico img'),
            $poweredByIcon = $('#footer-poweredbyico img');

        if (!$ccIcon.length || !$poweredByIcon.length) return;

        if (theme === 'dark') {
            $ccIcon.attr('src', '/resources/assets/cc-by-nc-sa-dark.svg');
            $poweredByIcon.attr('src', '/resources/assets/poweredby-mediawiki-dark.svg');
        } else if (theme === 'light') {
            $ccIcon.attr('src', '/resources/assets/cc-by-nc-sa.svg');
            $poweredByIcon.attr('src', '/resources/assets/poweredby-mediawiki.svg');
        } else if (theme === 'os') {
            // 自动跟随系统
            if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
                $ccIcon.attr('src', '/resources/assets/cc-by-nc-sa-dark.svg');
                $poweredByIcon.attr('src', '/resources/assets/poweredby-mediawiki-dark.svg');
            } else {
                $ccIcon.attr('src', '/resources/assets/cc-by-nc-sa.svg');
                $poweredByIcon.attr('src', '/resources/assets/poweredby-mediawiki.svg');
            }
        }
    }

    // 初始化时执行
    updateLogos();

    // 如果用户切换主题(无需刷新),监听变化
    mw.hook('user.preferencesSaved').add(updateLogos);

    // 监听系统颜色方案变化(适配 skin-theme-clientpref-os)
    window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateLogos);
});


// 移除深色模式反馈提示
$(function () {
    function removeBetaNotice() {
        var $node = $('#skin-theme-beta-notice');
        if ($node.length) {
            $node.remove();
            // 如果移除了,就断开观察器(可选)
            if (observer) {
                observer.disconnect();
            }
        }
    }

    // 第一次尝试
    removeBetaNotice();

    // 设置 MutationObserver 观察 body 或 document.body,监视 childList 增删
    var observer = new MutationObserver(function (mutations) {
        // 每次改变 DOM 时尝试移除
        removeBetaNotice();
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 作为兜底:在页面加载完成后一段时间再试一次
    $(window).on('load', function () {
        removeBetaNotice();
    });
    // 再加一个延时
    setTimeout(removeBetaNotice, 2000);
});