MediaWiki:Common.js:修订间差异
外观
小 移除深色模式反馈提示 |
小 增加延时尝试以确保渲染后再移除 |
||
第64行: | 第64行: | ||
// 移除深色模式反馈提示 | // 移除深色模式反馈提示 | ||
$(function () { | $(function () { | ||
$('#skin-theme-beta-notice').remove(); | 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); | |||
}); | }); |
2025年9月25日 (四) 12:29的版本
/* 这里的任何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 updateCopyrightIcon() {
var $icon = $('#footer-copyrightico img');
if ($icon.length === 0) return;
var htmlClass = document.documentElement.classList;
// 默认浅色
var src = '/resources/assets/cc-by-nc-sa.svg';
if (htmlClass.contains('skin-theme-clientpref-night')) {
src = '/resources/assets/cc-by-nc-sa-dark.svg';
} else if (htmlClass.contains('skin-theme-clientpref-os')) {
// 自动模式,根据操作系统偏好选择
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
src = '/resources/assets/cc-by-nc-sa-dark.svg';
}
}
$icon.attr('src', src);
}
// 页面加载时执行一次
updateCopyrightIcon();
// 监听深浅模式切换
var observer = new MutationObserver(updateCopyrightIcon);
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
});
// 移除深色模式反馈提示
$(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);
});