打开/关闭搜索
搜索
打开/关闭菜单
564
15
4
3119
导航
首页
随机页面
特殊页面
参与编写
上传文件
打开/关闭外观设置菜单
通知
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。
user-interface-preferences
个人工具
登录
查看“︁MediaWiki:Common.js”︁的源代码
MediaWiki界面页面
更多操作
←
MediaWiki:Common.js
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
此页面为本wiki上的软件提供界面文本,并受到保护以防止滥用。 如欲修改所有wiki的翻译,请访问
translatewiki.net
上的MediaWiki本地化项目。
您无权编辑此JavaScript页面,因为编辑此页面可能会影响所有访问者。
此页面已受到保护,以防止编辑或其他操作。
您可以查看和复制此页面的源代码。
/* 这里的任何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 () { // 获取当前“实际使用”的主题:'dark' 或 'light' function getEffectiveTheme() { var cls = document.documentElement.classList; if (cls.contains('skin-theme-clientpref-night')) return 'dark'; if (cls.contains('skin-theme-clientpref-day')) return 'light'; if (cls.contains('skin-theme-clientpref-os')) { if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { return 'dark'; } return 'light'; } // 兜底:根据系统偏好判断 if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { return 'dark'; } return 'light'; } // 如果元素存在就设置 src(并记录日志) function setSrcIfExists(selector, src) { var $el = $(selector); if ($el.length) { $el.attr('src', src); console.log('[logo-switch] set', selector, '->', src); } else { console.log('[logo-switch] not found', selector); } } // 主更新函数 —— 每次都重新 select 元素 function updateLogos() { var theme = getEffectiveTheme(); if (theme === 'dark') { setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa-dark.svg'); setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki-dark.svg'); setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw-dark.svg'); } else { setSrcIfExists('#footer-copyrightico img', '/resources/assets/cc-by-nc-sa.svg'); setSrcIfExists('#footer-poweredbyico img', '/resources/assets/poweredby-mediawiki.svg'); setSrcIfExists('#footer-poweredbysmwico img', '/resources/assets/poweredby-smw.svg'); } } // 首次运行 updateLogos(); // 监听 <html> class 变化(用户在 UI 切换浅/深/自动) var htmlObserver = new MutationObserver(function (mutations) { for (var i = 0; i < mutations.length; i++) { if (mutations[i].attributeName === 'class') { updateLogos(); break; } } }); htmlObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); // 监听 body 的 DOM 变动,防止 footer 异步插入或被替换 var bodyObserver = new MutationObserver(function () { updateLogos(); }); bodyObserver.observe(document.body, { childList: true, subtree: true }); // 监听系统主题变化(适用于 skin-theme-clientpref-os) if (window.matchMedia) { var mq = window.matchMedia('(prefers-color-scheme: dark)'); if (typeof mq.addEventListener === 'function') { mq.addEventListener('change', updateLogos); } else if (typeof mq.addListener === 'function') { // 兼容旧浏览器 mq.addListener(updateLogos); } } // 如果存在 MediaWiki 的 hook(例如偏好保存后),也触发一次 if (window.mw && mw.hook) { mw.hook('user.preferencesSaved').add(updateLogos); } // 兜底:页面 load 后、以及延迟 1.5s 再试一次 $(window).on('load', updateLogos); setTimeout(updateLogos, 1500); }); // 移除深色模式反馈提示 $(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); }); // 正文底部额外搜索链接 mw.hook('wikipage.content').add(function ($content) { // 只在默认命名空间生效,且排除首页 if (mw.config.get('wgNamespaceNumber') !== 0 || mw.config.get('wgIsMainPage')) { return; // 不满足条件就直接退出 } // 获取当前条目的名称 var pageName = mw.config.get('wgPageName'); var title = pageName.replace(/_/g, ' '); // 构造搜索链接 var googleLink = $('<a>') .attr('href', 'https://www.google.com/search?q=' + encodeURIComponent(title)) .attr('target', '_blank') .text('在谷歌中搜索 ' + title); var baiduLink = $('<a>') .attr('href', 'https://www.baidu.com/s?wd=' + encodeURIComponent(title)) .attr('target', '_blank') .css('margin-left', '15px') .text('在百度中搜索 ' + title); // 包一层 div,放到底部 var container = $('<div>') .addClass('search-links') .css({ marginTop: '20px', textAlign: 'center' }) .append(googleLink) .append(baiduLink); // 插入到页面内容的底部 $content.append(container); });
返回
MediaWiki:Common.js
。
查看“︁MediaWiki:Common.js”︁的源代码
MediaWiki界面页面