MediaWiki:Common.js
MediaWiki界面页面
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-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");
}
}
});
document.addEventListener("DOMContentLoaded", async () => {
// 选择要替换的 SVG img
const poweredSvgImg = document.querySelector('#footer-poweredbyico img[alt="Powered by MyWiki"]');
if (!poweredSvgImg) return;
const resp = await fetch(poweredSvgImg.src);
const svgText = await resp.text();
// 创建内联 SVG
const wrapper = document.createElement("a");
wrapper.href = "https://ztm0929.me/";
wrapper.className = "cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled";
wrapper.target = "_blank";
wrapper.innerHTML = svgText;
// 替换原来的 img
poweredSvgImg.replaceWith(wrapper);
// 设置默认颜色
setLogoMode(document.body.classList.contains('skin-theme-clientpref-night') ? 'night' : 'day');
// 可选:监听用户点击按钮切换主题
document.querySelector('#toggle-theme')?.addEventListener('click', () => {
const mode = document.body.classList.contains('skin-theme-clientpref-night') ? 'day' : 'night';
document.body.classList.toggle('skin-theme-clientpref-night');
document.body.classList.toggle('skin-theme-clientpref-day');
setLogoMode(mode);
});
});
// 动态修改 SVG 填充颜色
function setLogoMode(mode) {
const svg = document.querySelector('#footer-poweredbyico a svg');
if (!svg) return;
const fills = {
day: ['#666', '#333', '#1a1a1a', '#000'], // path1~path4
night: ['#999', '#ccc', '#e6e6e6', '#fff']
};
['path1','path2','path3','path4'].forEach((cls, idx) => {
const el = svg.querySelector('.' + cls);
if (el) el.setAttribute('fill', fills[mode][idx]);
});
}