MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 20: | Line 20: | ||
}); | }); | ||
}); | }); | ||
(function () { | |||
const current = location.pathname.replace('/wiki/', ''); | |||
document.querySelectorAll('#mw-panel a').forEach(link => { | |||
const href = link.getAttribute('href'); | |||
if (!href || !href.includes('/wiki/')) return; | |||
if (href.endsWith(current)) { | |||
link.classList.add('active-page'); | |||
// auto open parent section | |||
const portal = link.closest('.portal'); | |||
if (portal) { | |||
const body = portal.querySelector('.body'); | |||
if (body) body.style.display = 'block'; | |||
} | |||
} | |||
}); | |||
})(); | |||
(function () { | |||
const h1 = document.querySelector('#firstHeading'); | |||
if (!h1) return; | |||
const crumb = document.createElement('div'); | |||
crumb.className = 'gitbook-breadcrumb'; | |||
crumb.innerHTML = `<a href="/wiki/Main_Page">Home</a> / ${h1.textContent}`; | |||
document.querySelector('#content').prepend(crumb); | |||
})(); | |||
(function () { | |||
const links = Array.from(document.querySelectorAll('#mw-panel a[href*="/wiki/"]')); | |||
const current = location.pathname.replace('/wiki/', ''); | |||
const index = links.findIndex(a => a.href.endsWith(current)); | |||
if (index === -1) return; | |||
const nav = document.createElement('div'); | |||
nav.className = 'gitbook-prevnext'; | |||
if (links[index - 1]) { | |||
nav.innerHTML += `<a class="prev" href="${links[index - 1].href}">← ${links[index - 1].textContent}</a>`; | |||
} | |||
if (links[index + 1]) { | |||
nav.innerHTML += `<a class="next" href="${links[index + 1].href}">${links[index + 1].textContent} →</a>`; | |||
} | |||
document.querySelector('#content').appendChild(nav); | |||
})(); | |||
(function () { | |||
// Detect Main Page | |||
const isHome = | |||
document.body.classList.contains('mw-mainpage') || | |||
document.body.classList.contains('page-Main_Page'); | |||
// Stop auto-scroll on Home | |||
if (isHome) return; | |||
// Target scroll position (after breadcrumb / hero) | |||
const content = document.getElementById('content'); | |||
if (!content) return; | |||
// Small delay to wait layout ready | |||
setTimeout(() => { | |||
const y = content.getBoundingClientRect().top + window.scrollY - 20; | |||
window.scrollTo({ | |||
top: y, | |||
behavior: 'smooth' | |||
}); | |||
}, 150); | |||
})(); | |||
Latest revision as of 02:42, 19 December 2025
mw.loader.using('mediawiki.util').then(() => {
function forceSidebarOpen() {
document.querySelectorAll('.vector-menu-checkbox').forEach(cb => {
cb.checked = true;
});
}
forceSidebarOpen();
setTimeout(forceSidebarOpen, 300);
setTimeout(forceSidebarOpen, 800);
});
document.querySelectorAll('#mw-panel .portal h3').forEach(h => {
h.style.cursor = 'pointer';
h.addEventListener('click', () => {
const content = h.nextElementSibling;
content.style.display =
content.style.display === 'none' ? 'block' : 'none';
});
});
(function () {
const current = location.pathname.replace('/wiki/', '');
document.querySelectorAll('#mw-panel a').forEach(link => {
const href = link.getAttribute('href');
if (!href || !href.includes('/wiki/')) return;
if (href.endsWith(current)) {
link.classList.add('active-page');
// auto open parent section
const portal = link.closest('.portal');
if (portal) {
const body = portal.querySelector('.body');
if (body) body.style.display = 'block';
}
}
});
})();
(function () {
const h1 = document.querySelector('#firstHeading');
if (!h1) return;
const crumb = document.createElement('div');
crumb.className = 'gitbook-breadcrumb';
crumb.innerHTML = `<a href="/wiki/Main_Page">Home</a> / ${h1.textContent}`;
document.querySelector('#content').prepend(crumb);
})();
(function () {
const links = Array.from(document.querySelectorAll('#mw-panel a[href*="/wiki/"]'));
const current = location.pathname.replace('/wiki/', '');
const index = links.findIndex(a => a.href.endsWith(current));
if (index === -1) return;
const nav = document.createElement('div');
nav.className = 'gitbook-prevnext';
if (links[index - 1]) {
nav.innerHTML += `<a class="prev" href="${links[index - 1].href}">← ${links[index - 1].textContent}</a>`;
}
if (links[index + 1]) {
nav.innerHTML += `<a class="next" href="${links[index + 1].href}">${links[index + 1].textContent} →</a>`;
}
document.querySelector('#content').appendChild(nav);
})();
(function () {
// Detect Main Page
const isHome =
document.body.classList.contains('mw-mainpage') ||
document.body.classList.contains('page-Main_Page');
// Stop auto-scroll on Home
if (isHome) return;
// Target scroll position (after breadcrumb / hero)
const content = document.getElementById('content');
if (!content) return;
// Small delay to wait layout ready
setTimeout(() => {
const y = content.getBoundingClientRect().top + window.scrollY - 20;
window.scrollTo({
top: y,
behavior: 'smooth'
});
}, 150);
})();