MediaWiki:Common.js
来自Mindustry中文wiki
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 点击按钮复制内容的脚本 */
$(function() {
$('.copy-button').click(function() {
var text = $(this).attr('data-text');
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val(text).select();
document.execCommand("copy");
$temp.remove();
alert("复制成功!");
});
});
/* Mindustry 顶部导航栏
$(document).ready(function() {
// 1. 定义菜单内容 (直接写死白色字体,防止看不见)
var myMenu = `
<div id="mindustry-nav" style="display: flex; align-items: center; height: 100%; margin-left: 20px; z-index: 999;">
<a href="/index.php/首页" style="color: #fff !important; font-weight: bold; font-size: 16px; margin-right: 20px; text-decoration: none;">首页</a>
<a href="/index.php/单位" style="color: #fff !important; font-weight: bold; font-size: 16px; margin-right: 20px; text-decoration: none;">单位</a>
<a href="/index.php/建筑" style="color: #fff !important; font-weight: bold; font-size: 16px; margin-right: 20px; text-decoration: none;">建筑</a>
<a href="/index.php/逻辑" style="color: #fff !important; font-weight: bold; font-size: 16px; margin-right: 20px; text-decoration: none;">逻辑</a>
</div>
`;
// 2. 尝试插入到 Logo 所在的容器中
// Vector 2022 的 Logo 容器通常叫 .vector-header-start
var target = $('.vector-header-start');
// 如果找不到,尝试找 .mw-logo (旧版兼容)
if (target.length === 0) {
target = $('.mw-logo').parent();
}
// 执行插入
target.append(myMenu);
// 3. 调试信息 (按F12看控制台用)
console.log("Mindustry 导航栏脚本已执行");
});
mw.loader.using(['mediawiki.util'], function () {
if (mw.config.get('skin') !== 'timeless') return;
const navRoot = document.querySelector('#mw-site-navigation #p-
navigation .mw-portlet-body > ul');
if (!navRoot) return;
const storageKey = 'mdt.sidebar.tree.v1';
let persisted = {};
try { persisted = JSON.parse(localStorage.getItem(storageKey) ||
'{}'); } catch (e) {}
const currentPage = mw.config.get('wgPageName'); // underscores
function titleFromHref(href) {
try {
const url = new URL(href, location.origin);
const t = url.searchParams.get('title');
if (t) return t.replace(/ /g, '_');
const marker = '/index.php/';
const i = url.pathname.indexOf(marker);
if (i !== -1) return decodeURIComponent(url.pathname.slice(i +
marker.length)).replace(/ /g, '_');
} catch (e) {}
return null;
}
function keyForGroup(li) {
return li.id || li.dataset.mdtKey || (li.querySelector(':scope >
a')?.getAttribute('href') ?? '');
}
function setOpen(li, open) {
li.classList.toggle('mdt-nav__group--open', open);
const key = keyForGroup(li);
if (key) {
persisted[key] = open;
localStorage.setItem(storageKey, JSON.stringify(persisted));
}
}
const items = Array.from(navRoot.querySelectorAll(':scope > li'));
let currentGroup = null;
let sublist = null;
for (const li of items) {
const a = li.querySelector(':scope > a');
if (!a) continue;
const raw = a.textContent.trim();
const m = raw.match(/^(-{2,})\s+(.*)$/);
if (m) {
const depth = Math.min(Math.floor(m[1].length / 2), 3);
a.textContent = m[2];
li.classList.add('mdt-nav__item', 'mdt-nav__item--sub', `mdt-
nav__item--depth-${depth}`);
if (!currentGroup) continue;
if (!sublist) {
sublist = document.createElement('ul');
sublist.className = 'mdt-nav__sublist';
currentGroup.appendChild(sublist);
currentGroup.classList.add('mdt-nav__group');
}
sublist.appendChild(li);
} else {
li.classList.add('mdt-nav__item');
currentGroup = li;
sublist = null;
}
}
const groups = Array.from(navRoot.querySelectorAll(':scope > li.mdt-
nav__group'));
for (const li of groups) {
const a = li.querySelector(':scope > a');
const key = li.id || a?.getAttribute('href') ||
Math.random().toString(36);
li.dataset.mdtKey = key;
const toggle = document.createElement('button');
toggle.type = 'button';
toggle.className = 'mdt-nav__toggle';
toggle.setAttribute('aria-label', '展开/收起');
li.insertBefore(toggle, li.querySelector('.mdt-nav__sublist'));
if (typeof persisted[key] === 'boolean') setOpen(li,
persisted[key]);
toggle.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
setOpen(li, !li.classList.contains('mdt-nav__group--open'));
});
}
let activeLink = null;
for (const a of navRoot.querySelectorAll('a')) {
const t = titleFromHref(a.href);
if (t && t === currentPage) { activeLink = a; break; }
}
if (activeLink) {
activeLink.classList.add('mdt-nav__link--active');
const activeLi = activeLink.closest('li');
if (activeLi) activeLi.classList.add('mdt-nav__item--active');
const group = activeLink.closest('li.mdt-nav__group');
if (group) {
group.classList.add('mdt-nav__group--active');
setOpen(group, true); // 自动展开包含当前页面的分组
}
}
});
