feat(nav): embed request ui in home top tabs

Replace full-page navigation with a native emby-tab-button and pageTabContent panel so Request behaves like Home and Favorites.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Michael SloanandCursor committed 2026-08-02 17:00:07 -04:00
1 parent eb20e64c37
commit 0a5ea07838
10 files changed
+597 -461

No files matched your search

+266 -183
View File
@@ -1,17 +1,13 @@
(() => {
"use strict";
const REQUEST_URL = "/Requests/Page";
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";
function textOf(node) {
return [
node?.textContent,
node?.getAttribute?.("aria-label"),
node?.getAttribute?.("title"),
node?.getAttribute?.("data-title")
].filter(Boolean).join(" ").replace(/\s+/g, " ").trim().toLowerCase();
}
let ensuring = null;
let assetsPromise = null;
function ensureStyle() {
if (document.getElementById(STYLE_ID)) {
@@ -20,37 +16,175 @@
const style = document.createElement("style");
style.id = STYLE_ID;
style.textContent = ".jf-request-menu-item{align-items:center;color:inherit;display:flex!important;gap:1rem;min-height:3rem;padding:.75rem 1.25rem;text-decoration:none}.jf-request-menu-item:hover,.jf-request-menu-item:focus,.jf-request-tab:hover,.jf-request-tab:focus{color:#00a4dc;text-decoration:none}.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}.jf-request-tab{align-items:center;color:inherit;display:inline-flex;text-decoration:none}";
style.textContent = [
".jf-request-tab-panel{padding:1rem 1.25rem 2rem;box-sizing:border-box;}",
".jf-request-tab-panel .req-embed{max-width:1400px;margin:0 auto;}",
".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 controlsIn(container) {
return Array.from(container.querySelectorAll("a, button, [role='button'], [role='menuitem'], [role='link']"));
function isHomeTabContext() {
const hash = window.location.hash || "";
const onHomeHash = hash === ""
|| hash === "#/home"
|| hash === "#/home.html"
|| hash.indexOf("#/home?") === 0
|| hash.indexOf("#/home.html?") === 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 openRequestPage(event) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation?.();
window.location.assign(REQUEST_URL);
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 wireLink(link) {
link.href = REQUEST_URL;
link.addEventListener("click", openRequestPage, true);
return link;
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 labelMatches(node, labels) {
const label = textOf(node);
return labels.some((value) => label === value || label.includes(value));
function createTabPanel() {
const panel = document.createElement("div");
panel.className = "tabContent pageTabContent hide jf-request-tab-panel";
panel.setAttribute(TAB_ATTR, "request");
const mount = document.createElement("div");
mount.className = "sections jf-request-sections";
mount.setAttribute("data-role", "request-mount");
panel.appendChild(mount);
return panel;
}
function isDrawerElement(node) {
return Boolean(node?.closest?.(".mainDrawer,.mainDrawer-scrollContainer,.drawerContent,.MuiDrawer-paper"));
function findNativeButtons(tabsSlider) {
return Array.from(tabsSlider.querySelectorAll(".emby-tab-button")).filter((btn) => !btn.hasAttribute(TAB_ATTR));
}
function sideMenuContainers() {
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"));
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;
}
// Native home/favorites panels.
if (index === 0) {
page.querySelector("#homeTab")?.setAttribute("data-index", "0");
} else if (index === 1) {
page.querySelector("#favoritesTab")?.setAttribute("data-index", "1");
}
});
}
async function mountRequestUi(panel) {
const mount = panel.querySelector('[data-role="request-mount"]');
if (!mount) {
return;
}
await loadAssets();
if (window.JellyfinRequestUI) {
window.JellyfinRequestUI.mount(mount, { embed: 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",
@@ -58,182 +192,131 @@
".navMenu",
".MuiDrawer-paper"
];
const found = new Set();
for (const selector of selectors) {
for (const candidate of document.querySelectorAll(selector)) {
if (candidate.classList?.contains("mainDrawerHandle")) {
for (const container of document.querySelectorAll(selector)) {
if (container.querySelector(".jf-request-menu-item")) {
continue;
}
const labels = controlsIn(candidate).map(textOf);
const hasNormalNav = labels.includes("home")
|| labels.includes("favorites")
|| labels.includes("favourites")
|| labels.some((label) => label.includes("home"));
if (hasNormalNav) {
found.add(candidate);
}
}
}
return Array.from(found);
}
function topNavContainers() {
const found = new Set();
const selectors = [
".sectionTabs",
".emby-tabs",
".tabs",
".headerTabs",
".viewMenuBar",
".skinHeader .tabs"
];
for (const selector of selectors) {
for (const candidate of document.querySelectorAll(selector)) {
if (isDrawerElement(candidate)) {
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 controls = controlsIn(candidate);
const hasHome = controls.some((item) => labelMatches(item, ["home"]));
const hasFavorites = controls.some((item) => labelMatches(item, ["favorites", "favourites"]));
if (hasHome && hasFavorites) {
found.add(candidate);
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 = '<span class="material-icons jf-request-menu-icon" aria-hidden="true">add_circle_outline</span><span class="jf-request-menu-label">Request</span>';
link.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
activateRequestTab();
});
if (after.parentElement === container) {
after.insertAdjacentElement("afterend", link);
} else {
after.parentElement?.insertAdjacentElement("afterend", link);
}
}
}
}
for (const home of controlsIn(document).filter((item) => !isDrawerElement(item) && labelMatches(item, ["home"]))) {
let candidate = home.parentElement;
for (let depth = 0; candidate && depth < 4; depth += 1, candidate = candidate.parentElement) {
const controls = controlsIn(candidate);
const hasFavorites = controls.some((item) => labelMatches(item, ["favorites", "favourites"]));
if (hasFavorites && controls.length <= 12) {
found.add(candidate);
break;
function ensureRequestTab() {
if (ensuring) {
return ensuring;
}
ensuring = (async () => {
ensureStyle();
insertSideMenuItem();
if (!isHomeTabContext()) {
document.querySelectorAll(`.headerTabs [${TAB_ATTR}]`).forEach((node) => node.remove());
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;
}
let button = tabsSlider.querySelector(`[${TAB_ATTR}="request"]`);
if (!button) {
button = createTabButton();
}
let panel = page.querySelector(`.tabContent[${TAB_ATTR}="request"]`);
if (!panel) {
panel = createTabPanel();
}
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);
// Keep native panels first, then Request, without detaching home rows incorrectly.
const orderedPanels = [homeTab, favoritesTab, panel];
placeInOrder(page, orderedPanels);
syncIndexes(tabsSlider, page);
if (typeof tabsEl.refresh === "function") {
try {
tabsEl.refresh();
} catch {
// best-effort
}
}
}
return Array.from(found).sort((left, right) => controlsIn(left).length - controlsIn(right).length);
await mountRequestUi(panel);
})().finally(() => {
ensuring = null;
});
return ensuring;
}
function createMenuItem(reference) {
const link = document.createElement("a");
link.className = "jf-request-menu-item";
link.setAttribute("aria-label", "Requests");
wireLink(link);
const icon = document.createElement("span");
icon.className = "material-icons jf-request-menu-icon";
icon.setAttribute("aria-hidden", "true");
icon.textContent = "add_circle_outline";
const label = document.createElement("span");
label.className = "jf-request-menu-label";
label.textContent = "Requests";
link.append(icon, label);
const referenceClass = reference?.getAttribute("class");
if (referenceClass) {
link.setAttribute("class", `${referenceClass} jf-request-menu-item`);
}
const referenceRole = reference?.getAttribute("role");
if (referenceRole) {
link.setAttribute("role", referenceRole);
}
return link;
}
function createTopTab(reference) {
const tag = reference?.tagName?.toLowerCase() === "button" ? "button" : "a";
const link = document.createElement(tag);
link.className = "jf-request-tab";
link.textContent = "Request";
link.setAttribute("aria-label", "Request");
link.setAttribute("data-jellyfin-request", "request-tab");
if (tag === "button") {
link.type = "button";
}
wireLink(link);
const referenceClass = reference?.getAttribute("class");
if (referenceClass) {
link.setAttribute("class", `${referenceClass} jf-request-tab`);
}
const referenceRole = reference?.getAttribute("role");
if (referenceRole) {
link.setAttribute("role", referenceRole);
}
const referenceTabIndex = reference?.getAttribute("tabindex");
if (referenceTabIndex) {
link.setAttribute("tabindex", referenceTabIndex);
}
link.classList.remove("selected", "active", "is-active", "current");
link.removeAttribute("aria-selected");
return link;
}
function insertTopTab() {
ensureStyle();
const existingTabs = Array.from(document.querySelectorAll(".jf-request-tab"));
existingTabs.slice(1).forEach((node) => node.remove());
if (existingTabs.length > 0) {
return;
}
for (const container of topNavContainers()) {
const controls = controlsIn(container);
const after = controls.find((item) => /^(favorites|favourites)$/i.test(textOf(item)))
|| controls.find((item) => /^home$/i.test(textOf(item)) || textOf(item).includes("home"));
if (!after) {
continue;
function boot() {
ensureRequestTab();
document.addEventListener("viewshow", (event) => {
if (event.target && event.target.id === "indexPage") {
ensureRequestTab();
}
});
window.addEventListener("hashchange", () => ensureRequestTab());
setInterval(() => ensureRequestTab(), 1500);
after.insertAdjacentElement("afterend", createTopTab(after));
return;
const headerTabs = document.querySelector(".headerTabs");
if (headerTabs) {
new MutationObserver(() => ensureRequestTab()).observe(headerTabs, { childList: true, subtree: true });
}
}
function insertSideMenuItem() {
ensureStyle();
for (const container of sideMenuContainers()) {
if (container.querySelector(".jf-request-menu-item")) {
continue;
}
const controls = controlsIn(container);
const after = controls.find((item) => /^(favorites|favourites)$/i.test(textOf(item)))
|| controls.find((item) => /^home$/i.test(textOf(item)) || textOf(item).includes("home"))
|| controls[0];
if (!after) {
continue;
}
const link = createMenuItem(after);
if (after.parentElement === container) {
after.insertAdjacentElement("afterend", link);
} else {
after.parentElement?.insertAdjacentElement("afterend", link);
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
function insertEntries() {
insertTopTab();
insertSideMenuItem();
}
insertEntries();
setInterval(insertEntries, 1000);
new MutationObserver(insertEntries).observe(document.documentElement, { childList: true, subtree: true });
})();