diff --git a/static/svrnty_nav.js b/static/svrnty_nav.js index f107e1e..fd6d1ee 100644 --- a/static/svrnty_nav.js +++ b/static/svrnty_nav.js @@ -11,6 +11,9 @@ "use strict"; if (window.__svrntyNavLoaded) return; window.__svrntyNavLoaded = true; + const LOG = (...a) => console.log("[svrnty-nav]", ...a); + const ERR = (...a) => console.error("[svrnty-nav]", ...a); + LOG("loaded", { readyState: document.readyState }); const TABS = [ { @@ -31,23 +34,36 @@ function _injectButtons() { const nav = document.querySelector(".sidebar-nav"); - if (!nav) return false; + if (!nav) { + LOG("_injectButtons: .sidebar-nav not found yet"); + return false; + } + let added = 0; TABS.forEach((t) => { if (nav.querySelector('[data-panel="' + t.id + '"]')) return; - const btn = document.createElement("button"); - btn.className = "nav-tab has-tooltip has-tooltip--bottom svrnty-nav-tab"; - btn.setAttribute("data-panel", t.id); - btn.setAttribute("data-label", t.label); - btn.setAttribute("data-tooltip", t.tooltip); - btn.setAttribute("aria-label", t.label); - btn.innerHTML = t.svg; - btn.addEventListener("click", () => { - if (typeof window.switchPanel === "function") { - window.switchPanel(t.id, { fromRailClick: true }); - } - }); - nav.appendChild(btn); + try { + const btn = document.createElement("button"); + btn.className = "nav-tab has-tooltip has-tooltip--bottom svrnty-nav-tab"; + btn.setAttribute("data-panel", t.id); + btn.setAttribute("data-label", t.label); + btn.setAttribute("data-tooltip", t.tooltip); + btn.setAttribute("aria-label", t.label); + btn.innerHTML = t.svg; + btn.addEventListener("click", () => { + LOG("button clicked:", t.id); + if (typeof window.switchPanel === "function") { + window.switchPanel(t.id, { fromRailClick: true }); + } else { + ERR("switchPanel is not a function — cannot route click"); + } + }); + nav.appendChild(btn); + added++; + } catch (e) { + ERR("failed to inject button", t.id, e); + } }); + LOG("_injectButtons: added", added, "of", TABS.length, "(nav children:", nav.children.length, ")"); return true; } @@ -81,13 +97,20 @@ return true; } + let _initAttempts = 0; function _init() { + _initAttempts++; const buttonsOk = _injectButtons(); const wrapOk = _wrapSwitchPanel(); if (!buttonsOk || !wrapOk) { - // DOM not ready yet — retry on next paint - requestAnimationFrame(_init); + if (_initAttempts < 100) { + requestAnimationFrame(_init); + } else { + ERR("_init gave up after", _initAttempts, "attempts. buttonsOk=", buttonsOk, "wrapOk=", wrapOk); + } + return; } + LOG("_init completed in", _initAttempts, "attempt(s)"); } if (document.readyState === "loading") {