(() => {
"use strict";
const TAB_ATTR = "data-jellyfin-request-tab";
const STYLE_ID = "jellyfin-plugin-request-nav-style";
const ASSET_CSS = "/Requests/Assets/request.css";
const ASSET_JS = "/Requests/Assets/request.js";
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)) {
return;
}
const style = document.createElement("style");
style.id = STYLE_ID;
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;}",
".jf-request-menu-label{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}"
].join("");
document.head.appendChild(style);
}
function isHomeTabContext() {
const hash = (window.location.hash || "").toLowerCase();
const onHomeHash = hash === ""
|| hash === "#"
|| hash === "#/home"
|| hash === "#/home.html"
|| hash === "#home"
|| hash.indexOf("#/home?") === 0
|| hash.indexOf("#/home.html?") === 0
|| hash.indexOf("#home?") === 0;
if (!onHomeHash) {
return false;
}
const page = document.getElementById("indexPage");
if (!page || page.classList.contains("hide")) {
return false;
}
const visiblePage = document.querySelector(".page:not(.hide)");
return !visiblePage || visiblePage.id === "indexPage";
}
function loadAssets() {
if (assetsPromise) {
return assetsPromise;
}
assetsPromise = new Promise((resolve, reject) => {
if (!document.querySelector(`link[href="${ASSET_CSS}"]`)) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = ASSET_CSS;
document.head.appendChild(link);
}
if (window.JellyfinRequestUI) {
resolve();
return;
}
const existing = document.querySelector(`script[src="${ASSET_JS}"]`);
if (existing) {
existing.addEventListener("load", () => resolve(), { once: true });
existing.addEventListener("error", () => reject(new Error("Failed to load request.js")), { once: true });
return;
}
const script = document.createElement("script");
script.src = ASSET_JS;
script.onload = () => resolve();
script.onerror = () => reject(new Error("Failed to load request.js"));
document.head.appendChild(script);
});
return assetsPromise;
}
function createTabButton() {
const titleEl = document.createElement("div");
titleEl.className = "emby-button-foreground";
titleEl.textContent = "Request";
const button = document.createElement("button");
button.type = "button";
button.setAttribute("is", "empty-button");
button.className = "emby-tab-button emby-button";
button.setAttribute(TAB_ATTR, "request");
button.setAttribute("aria-label", "Request");
button.appendChild(titleEl);
return button;
}
function createTabPanel() {
const panel = document.createElement("div");
panel.className = "tabContent pageTabContent hide jf-request-tab-panel";
panel.setAttribute(TAB_ATTR, "request");
panel.id = "jellyfinRequestTab";
const mount = document.createElement("div");
mount.className = "sections jf-request-sections";
mount.setAttribute("data-role", "request-mount");
panel.appendChild(mount);
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)) {
return false;
}
const id = btn.id || "";
return id.indexOf("customTabButton_") !== 0;
});
}
function placeInOrder(parent, nodes) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
const previous = i > 0 ? nodes[i - 1] : null;
if (!previous) {
if (node.parentNode !== parent) {
parent.appendChild(node);
}
continue;
}
const alreadyAfter = node.parentNode === parent
&& (previous.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0;
if (!alreadyAfter) {
parent.insertBefore(node, previous.nextSibling);
}
}
}
function syncIndexes(tabsSlider, page) {
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);
if (key) {
const panel = page.querySelector(`.tabContent[${TAB_ATTR}="${key}"]`);
if (panel) {
panel.setAttribute("data-index", String(index));
}
return;
}
if (index === 0) {
page.querySelector("#homeTab")?.setAttribute("data-index", "0");
} else if (index === 1) {
page.querySelector("#favoritesTab")?.setAttribute("data-index", "1");
}
});
}
function syncPanelVisibility(selectedIndex) {
const page = document.getElementById("indexPage");
if (!page || isNaN(selectedIndex)) {
return;
}
page.querySelectorAll(".tabContent[data-index]").forEach((panel) => {
const panelIndex = parseInt(panel.getAttribute("data-index"), 10);
const active = panelIndex === selectedIndex;
panel.classList.toggle("hide", !active);
panel.classList.toggle("is-active", active);
});
}
async function mountRequestUi(panel, forceRemount) {
if (!panel) {
return;
}
panel.classList.remove("hide");
panel.classList.add("is-active");
const mount = panel.querySelector('[data-role="request-mount"]');
if (!mount) {
return;
}
try {
await loadAssets();
} catch (error) {
mount.textContent = "Failed to load Request UI.";
console.error("[Jellyfin.Plugin.Request]", error);
return;
}
if (!window.JellyfinRequestUI) {
mount.textContent = "Request UI failed to initialize.";
return;
}
const needsForce = forceRemount || !mount.querySelector(".req-root");
window.JellyfinRequestUI.mount(mount, { embed: true, force: needsForce });
}
function showRequestPanel() {
const page = document.getElementById("indexPage");
const button = document.querySelector(`.headerTabs [${TAB_ATTR}="request"]`);
const panel = page?.querySelector(`.tabContent[${TAB_ATTR}="request"]`) || cachedPanel;
if (!button || !panel) {
return;
}
const index = parseInt(button.getAttribute("data-index"), 10);
if (!isNaN(index)) {
syncPanelVisibility(index);
} else {
panel.classList.remove("hide");
panel.classList.add("is-active");
}
mountRequestUi(panel, true);
}
function isRequestTabButton(button) {
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 || tabsEl.dataset.jfRequestTabGuard === "true") {
return;
}
tabsEl.dataset.jfRequestTabGuard = "true";
guardAttached = true;
tabsEl.addEventListener("beforetabchange", (event) => {
if (!isHomeTabContext()) {
return;
}
const index = parseInt(event.detail && event.detail.selectedTabIndex, 10);
if (isNaN(index)) {
return;
}
syncPanelVisibility(index);
const selectedButton = tabsEl.querySelector(`.emby-tab-button[data-index="${index}"]`);
if (isRequestTabButton(selectedButton)) {
const panel = document.querySelector(`.tabContent[${TAB_ATTR}="request"]`) || cachedPanel;
mountRequestUi(panel, false);
}
}, true);
tabsEl.addEventListener("tabchange", (event) => {
if (!isHomeTabContext()) {
return;
}
const index = parseInt(event.detail && event.detail.selectedTabIndex, 10);
if (isNaN(index)) {
return;
}
const selectedButton = tabsEl.querySelector(`.emby-tab-button[data-index="${index}"]`);
if (!isRequestTabButton(selectedButton)) {
return;
}
event.stopImmediatePropagation();
syncPanelVisibility(index);
const panel = document.querySelector(`.tabContent[${TAB_ATTR}="request"]`) || cachedPanel;
mountRequestUi(panel, true);
}, true);
}
function activateRequestTab() {
if (!isHomeTabContext()) {
window.location.hash = "#/home";
setTimeout(activateRequestTab, 250);
return;
}
ensureRequestTab().then(() => {
const button = document.querySelector(`.headerTabs [${TAB_ATTR}="request"]`);
if (button) {
button.click();
}
});
}
function insertSideMenuItem() {
ensureStyle();
const selectors = [
".mainDrawer-scrollContainer",
".mainDrawer .scrollContainer",
".drawerContent",
".navMenu",
".MuiDrawer-paper"
];
for (const selector of selectors) {
for (const container of document.querySelectorAll(selector)) {
if (container.querySelector(".jf-request-menu-item")) {
continue;
}
const controls = Array.from(container.querySelectorAll("a, button, [role='button'], [role='menuitem']"));
const labels = controls.map((node) => (node.textContent || "").trim().toLowerCase());
if (!labels.some((label) => label === "home" || label.includes("home"))) {
continue;
}
const after = controls.find((item) => /^(favorites|favourites)$/i.test((item.textContent || "").trim()))
|| controls.find((item) => /home/i.test(item.textContent || ""))
|| controls[0];
if (!after) {
continue;
}
const link = document.createElement("button");
link.type = "button";
link.className = `${after.className || ""} jf-request-menu-item`.trim();
link.setAttribute("aria-label", "Request");
link.innerHTML = '';
link.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
activateRequestTab();
});
if (after.parentElement === container) {
after.insertAdjacentElement("afterend", link);
} else {
after.parentElement?.insertAdjacentElement("afterend", link);
}
}
}
}
function ensureRequestTab() {
if (ensuring) {
return ensuring;
}
ensuring = (async () => {
ensureStyle();
insertSideMenuItem();
if (!isHomeTabContext()) {
hideRequestTabAway();
return;
}
const page = document.getElementById("indexPage");
const tabsSlider = document.querySelector(".headerTabs .emby-tabs-slider");
const tabsEl = document.querySelector('.headerTabs [is="emby-tabs"]');
if (!page || !tabsSlider || !tabsEl) {
return;
}
const homeTab = page.querySelector("#homeTab");
const favoritesTab = page.querySelector("#favoritesTab");
if (!homeTab || !favoritesTab) {
return;
}
attachPluginTabGuard(tabsEl);
let button = tabsSlider.querySelector(`[${TAB_ATTR}="request"]`)
|| (cachedButton && cachedButton.isConnected ? cachedButton : null)
|| cachedButton;
if (!button || !button.isConnected) {
button = createTabButton();
}
cachedButton = button;
button.classList.remove("jf-request-tab-away", "hide");
button.removeAttribute("aria-hidden");
bindRequestButton(button);
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];
const orderedButtons = [homeButton, favoritesButton, button].filter(Boolean);
const uniqueButtons = [...new Set(orderedButtons)];
placeInOrder(tabsSlider, uniqueButtons);
const orderedPanels = [homeTab, favoritesTab, panel];
placeInOrder(page, orderedPanels);
syncIndexes(tabsSlider, page);
if (typeof tabsEl.refresh === "function") {
try {
tabsEl.refresh();
} catch {
// best-effort
}
}
loadAssets().catch(() => { /* surfaced on mount */ });
})().finally(() => {
ensuring = null;
});
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(), 2500);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
})();