MediaWiki:Common.js: Difference between revisions

From EstoriaRO Wiki
Jump to navigation Jump to search
Created page with "Any JavaScript here will be loaded for all users on every page load.: mw.loader.using('mediawiki.util').then(() => { const openMenu = () => { const toggle = document.querySelector('.vector-main-menu-toggle'); const menu = document.getElementById('vector-main-menu'); if (toggle && menu && menu.getAttribute('aria-expanded') !== 'true') { toggle.click(); } }; // Jalankan saat load openMenu(); // Jalanka..."
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using('mediawiki.util').then(() => {
mw.loader.using('mediawiki.util').then(() => {
     const openMenu = () => {
     function forceSidebarOpen() {
         const toggle = document.querySelector('.vector-main-menu-toggle');
         document.querySelectorAll('.vector-menu-checkbox').forEach(cb => {
         const menu = document.getElementById('vector-main-menu');
            cb.checked = true;
         });
    }


         if (toggle && menu && menu.getAttribute('aria-expanded') !== 'true') {
    forceSidebarOpen();
             toggle.click();
    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';
            }
         }
         }
     };
     });
})();


    // Jalankan saat load
    openMenu();


     // Jalankan ulang kalau Vector render ulang
(function () {
     setTimeout(openMenu, 500);
    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);
})();