fix(nav): reduce request tab flicker on home return

Hide the Request tab instead of removing it off-home, cache the button/panel, and reinject immediately when Jellyfin rebuilds the home tab bar.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Michael SloanandCursor committed 2026-08-02 21:38:28 -04:00
1 parent 7044023d98
commit db166bfc02
7 files changed
+93 -49

No files matched your search

+81 -37
View File
@@ -9,6 +9,9 @@
let ensuring = null;
let assetsPromise = null;
let guardAttached = false;
let cachedButton = null;
let cachedPanel = null;
let headerObserver = null;
function ensureStyle() {
if (document.getElementById(STYLE_ID)) {
@@ -20,6 +23,7 @@
style.textContent = [
".jf-request-tab-panel{padding:1rem 1.25rem 2rem;box-sizing:border-box;min-height:50vh;}",
".jf-request-tab-panel .req-embed{max-width:1400px;margin:0 auto;}",
`.headerTabs .emby-tab-button[${TAB_ATTR}].jf-request-tab-away{display:none!important;}`,
".jf-request-menu-item{align-items:center;color:inherit;display:flex!important;gap:1rem;min-height:3rem;padding:.75rem 1.25rem;text-decoration:none;cursor:pointer;background:none;border:0;width:100%;text-align:left;font:inherit;}",
".jf-request-menu-item:hover,.jf-request-menu-item:focus{color:#00a4dc;}",
".jf-request-menu-icon{align-items:center;display:inline-flex;font-size:1.45rem;justify-content:center;line-height:1;min-width:1.5rem;}",
@@ -103,7 +107,6 @@
function createTabPanel() {
const panel = document.createElement("div");
// Match SeerrFin: start without .hide; visibility is synced on tab change.
panel.className = "tabContent pageTabContent hide jf-request-tab-panel";
panel.setAttribute(TAB_ATTR, "request");
panel.id = "jellyfinRequestTab";
@@ -115,6 +118,18 @@
return panel;
}
function bindRequestButton(button) {
if (!button || button.dataset.jfRequestClickBound === "1") {
return;
}
button.dataset.jfRequestClickBound = "1";
button.addEventListener("click", () => {
setTimeout(showRequestPanel, 0);
setTimeout(showRequestPanel, 120);
});
}
function findNativeButtons(tabsSlider) {
return Array.from(tabsSlider.querySelectorAll(".emby-tab-button")).filter((btn) => {
if (btn.hasAttribute(TAB_ATTR)) {
@@ -147,7 +162,8 @@
}
function syncIndexes(tabsSlider, page) {
const buttons = Array.from(tabsSlider.querySelectorAll(".emby-tab-button"));
const buttons = Array.from(tabsSlider.querySelectorAll(".emby-tab-button"))
.filter((button) => !button.classList.contains("jf-request-tab-away"));
buttons.forEach((button, index) => {
button.setAttribute("data-index", String(index));
const key = button.getAttribute(TAB_ATTR);
@@ -215,7 +231,7 @@
function showRequestPanel() {
const page = document.getElementById("indexPage");
const button = document.querySelector(`.headerTabs [${TAB_ATTR}="request"]`);
const panel = page?.querySelector(`.tabContent[${TAB_ATTR}="request"]`);
const panel = page?.querySelector(`.tabContent[${TAB_ATTR}="request"]`) || cachedPanel;
if (!button || !panel) {
return;
}
@@ -235,13 +251,24 @@
return !!(button && button.getAttribute(TAB_ATTR) === "request");
}
function hideRequestTabAway() {
// Hide instead of remove so returning to Home can restore without a visible gap.
document.querySelectorAll(`.headerTabs [${TAB_ATTR}]`).forEach((node) => {
node.classList.add("jf-request-tab-away");
node.setAttribute("aria-hidden", "true");
if (!cachedButton && node.matches(".emby-tab-button")) {
cachedButton = node;
}
});
}
function attachPluginTabGuard(tabsEl) {
if (!tabsEl || guardAttached || tabsEl.dataset.jfRequestTabGuard === "true") {
if (!tabsEl || tabsEl.dataset.jfRequestTabGuard === "true") {
return;
}
guardAttached = true;
tabsEl.dataset.jfRequestTabGuard = "true";
guardAttached = true;
tabsEl.addEventListener("beforetabchange", (event) => {
if (!isHomeTabContext()) {
@@ -253,17 +280,15 @@
return;
}
// Jellyfin activates panels by NodeList order; keep data-index panels in sync.
syncPanelVisibility(index);
const selectedButton = tabsEl.querySelector(`.emby-tab-button[data-index="${index}"]`);
if (isRequestTabButton(selectedButton)) {
const panel = document.querySelector(`.tabContent[${TAB_ATTR}="request"]`);
const panel = document.querySelector(`.tabContent[${TAB_ATTR}="request"]`) || cachedPanel;
mountRequestUi(panel, false);
}
}, true);
// HomeView only knows Home/Favorites. Stop its handler for our tab so content is not cleared.
tabsEl.addEventListener("tabchange", (event) => {
if (!isHomeTabContext()) {
return;
@@ -281,7 +306,7 @@
event.stopImmediatePropagation();
syncPanelVisibility(index);
const panel = document.querySelector(`.tabContent[${TAB_ATTR}="request"]`);
const panel = document.querySelector(`.tabContent[${TAB_ATTR}="request"]`) || cachedPanel;
mountRequestUi(panel, true);
}, true);
}
@@ -360,7 +385,7 @@
insertSideMenuItem();
if (!isHomeTabContext()) {
document.querySelectorAll(`.headerTabs [${TAB_ATTR}]`).forEach((node) => node.remove());
hideRequestTabAway();
return;
}
@@ -379,24 +404,28 @@
attachPluginTabGuard(tabsEl);
let button = tabsSlider.querySelector(`[${TAB_ATTR}="request"]`);
if (!button) {
let button = tabsSlider.querySelector(`[${TAB_ATTR}="request"]`)
|| (cachedButton && cachedButton.isConnected ? cachedButton : null)
|| cachedButton;
if (!button || !button.isConnected) {
button = createTabButton();
}
if (button.dataset.jfRequestClickBound !== "1") {
button.dataset.jfRequestClickBound = "1";
button.addEventListener("click", () => {
setTimeout(showRequestPanel, 0);
setTimeout(showRequestPanel, 120);
});
}
cachedButton = button;
button.classList.remove("jf-request-tab-away", "hide");
button.removeAttribute("aria-hidden");
bindRequestButton(button);
let panel = page.querySelector(`.tabContent[${TAB_ATTR}="request"]`);
if (!panel) {
let panel = page.querySelector(`.tabContent[${TAB_ATTR}="request"]`)
|| (cachedPanel && cachedPanel.isConnected ? cachedPanel : null)
|| cachedPanel;
if (!panel || !panel.isConnected) {
panel = createTabPanel();
}
cachedPanel = panel;
// Insert the tab button before any async work so Home return does not flash without it.
const nativeButtons = findNativeButtons(tabsSlider);
const homeButton = nativeButtons[0];
const favoritesButton = nativeButtons[1] || nativeButtons[0];
@@ -404,7 +433,6 @@
const uniqueButtons = [...new Set(orderedButtons)];
placeInOrder(tabsSlider, uniqueButtons);
// Keep native panels first, then Request, without detaching home rows incorrectly.
const orderedPanels = [homeTab, favoritesTab, panel];
placeInOrder(page, orderedPanels);
@@ -418,8 +446,7 @@
}
}
// Preload assets so the first click is instant; do not force-show the panel.
await loadAssets().catch(() => { /* surfaced on mount */ });
loadAssets().catch(() => { /* surfaced on mount */ });
})().finally(() => {
ensuring = null;
});
@@ -427,28 +454,45 @@
return ensuring;
}
function observeHeaderTabs() {
const headerTabs = document.querySelector(".headerTabs");
if (!headerTabs || headerObserver) {
return;
}
let timer = null;
headerObserver = new MutationObserver(() => {
const onHome = isHomeTabContext();
const missing = onHome && !document.querySelector(`.headerTabs [${TAB_ATTR}="request"]:not(.jf-request-tab-away)`);
if (missing) {
// Jellyfin rebuilt the home tab bar — reinject immediately to avoid flicker.
ensureRequestTab();
return;
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => ensureRequestTab(), 80);
});
headerObserver.observe(headerTabs, { childList: true, subtree: true });
}
function boot() {
console.info("[Jellyfin.Plugin.Request] nav-loader active");
ensureRequestTab();
observeHeaderTabs();
document.addEventListener("viewshow", (event) => {
if (event.target && event.target.id === "indexPage") {
ensureRequestTab();
} else {
hideRequestTabAway();
}
});
window.addEventListener("hashchange", () => ensureRequestTab());
setInterval(() => ensureRequestTab(), 2000);
const headerTabs = document.querySelector(".headerTabs");
if (headerTabs) {
let timer = null;
new MutationObserver(() => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => ensureRequestTab(), 250);
}).observe(headerTabs, { childList: true, subtree: true });
}
setInterval(() => ensureRequestTab(), 2500);
}
if (document.readyState === "loading") {