From 49d7a15b151a712b9d1fb042204ce16d71edd320 Mon Sep 17 00:00:00 2001 From: HighImKevin Date: Wed, 4 Mar 2026 14:34:24 -0500 Subject: [PATCH] Fix loading/saving JS Injector plugins --- kefinTweaks-plugin.js | 46 +++++++++++++++++++++++----------- scripts/utils.js | 58 +++++++++++++++++-------------------------- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/kefinTweaks-plugin.js b/kefinTweaks-plugin.js index d4319d1..9245686 100644 --- a/kefinTweaks-plugin.js +++ b/kefinTweaks-plugin.js @@ -10,6 +10,22 @@ const MODAL_ID = 'kefinTweaksSourceModal'; let versionsCache = null; + function getAuthHeader() { + const token = ApiClient.accessToken(); + const client = typeof ApiClient.applicationName === 'function' ? ApiClient.applicationName() : 'Jellyfin Web'; + const device = typeof ApiClient.deviceName === 'function' ? ApiClient.deviceName() : (navigator.userAgent.includes('Chrome') ? 'Chrome' : 'Browser'); + const deviceId = typeof ApiClient.deviceId === 'function' ? ApiClient.deviceId() : ''; + const version = ApiClient._appVersion || ApiClient._serverVersion || ''; + const parts = [ + `Client="${encodeURIComponent(client)}"`, + `Device="${encodeURIComponent(device)}"`, + `DeviceId="${encodeURIComponent(deviceId)}"`, + `Version="${encodeURIComponent(version)}"`, + `Token="${encodeURIComponent(token)}"` + ]; + return `MediaBrowser ${parts.join(', ')}`; + } + // jsDelivr URL patterns const JSDELIVR_BASE = 'https://cdn.jsdelivr.net/gh/ranaldsgift/KefinTweaks'; const GITHUB_REPO = 'ranaldsgift/KefinTweaks'; @@ -31,16 +47,15 @@ // Find JavaScript Injector plugin async function findJavaScriptInjectorPlugin() { try { - if (!window.ApiClient || !window.ApiClient._serverAddress || !window.ApiClient.accessToken) { + if (!window.ApiClient || !window.ApiClient._serverAddress || window.ApiClient.accessToken() === null) { return null; } const server = ApiClient._serverAddress; - const token = ApiClient.accessToken(); const response = await fetch(`${server}/Plugins`, { headers: { - 'X-Emby-Token': token + 'Authorization': getAuthHeader() } }); @@ -74,11 +89,10 @@ } const server = ApiClient._serverAddress; - const token = ApiClient.accessToken(); const response = await fetch(`${server}/Plugins/${pluginId}/Configuration`, { headers: { - 'X-Emby-Token': token + 'Authorization': getAuthHeader() } }); @@ -198,12 +212,11 @@ window.KefinTweaksConfig = ${JSON.stringify(config, null, 2)};`; } const server = ApiClient._serverAddress; - const token = ApiClient.accessToken(); const response = await fetch(`${server}/Plugins/${pluginId}/Configuration`, { method: 'POST', headers: { - 'X-Emby-Token': token, + 'Authorization': getAuthHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify(injectorConfig) @@ -877,12 +890,11 @@ window.KefinTweaksConfig = ${JSON.stringify(config, null, 2)};`; // Save the updated configuration const server = ApiClient._serverAddress; - const token = ApiClient.accessToken(); const response = await fetch(`${server}/Plugins/${pluginId}/Configuration`, { method: 'POST', headers: { - 'X-Emby-Token': token, + 'Authorization': getAuthHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify(injectorConfig) @@ -1315,6 +1327,7 @@ window.KefinTweaksConfig = ${JSON.stringify(config, null, 2)};`; if (hash && hash.includes('dashboard/plugins')) { const pluginsPage = document.querySelector('#pluginsPage:not(.hide)'); if (!pluginsPage) { + console.log('[KefinTweaks Installer] Plugins page not found'); return; } @@ -1324,13 +1337,16 @@ window.KefinTweaksConfig = ${JSON.stringify(config, null, 2)};`; // Support for Jellyfin 10.11.X installedPlugins = document.querySelector('#pluginsPage:not(.hide)>div>div>div:last-child>div'); } - - if (pluginsPage && installedPlugins) { - // Small delay to ensure DOM is ready - setTimeout(() => { - addKefinTweaksPluginCard(installedPlugins); - }, 100); + + if (!installedPlugins || !pluginsPage) { + console.log('[KefinTweaks Installer] Installed plugins or plugins page not found'); + return; } + + // Small delay to ensure DOM is ready + setTimeout(() => { + addKefinTweaksPluginCard(installedPlugins); + }, 100); } } diff --git a/scripts/utils.js b/scripts/utils.js index e23fe5c..a2dd6c2 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -50,6 +50,26 @@ WARN('Emby.Page.onViewShow not found - utils may not work correctly'); } } + + /** + * Build MediaBrowser Authorization header + * @returns {string} - Authorization header + */ + function getAuthHeader() { + const token = ApiClient.accessToken(); + const client = typeof ApiClient.applicationName === 'function' ? ApiClient.applicationName() : 'Jellyfin Web'; + const device = typeof ApiClient.deviceName === 'function' ? ApiClient.deviceName() : (navigator.userAgent.includes('Chrome') ? 'Chrome' : 'Browser'); + const deviceId = typeof ApiClient.deviceId === 'function' ? ApiClient.deviceId() : ''; + const version = ApiClient._appVersion || ApiClient._serverVersion || ''; + const parts = [ + `Client="${encodeURIComponent(client)}"`, + `Device="${encodeURIComponent(device)}"`, + `DeviceId="${encodeURIComponent(deviceId)}"`, + `Version="${encodeURIComponent(version)}"`, + `Token="${encodeURIComponent(token)}"` + ]; + return `MediaBrowser ${parts.join(', ')}`; + } // Array to store registered handlers const handlers = []; @@ -417,39 +437,7 @@ throw new Error('No config provided and window.KefinTweaksConfig is not available'); } - // Find JavaScript Injector plugin - const server = window.ApiClient.serverAddress(); - const token = window.ApiClient.accessToken(); - - if (!server || !token) { - throw new Error('Server address or access token not available'); - } - - let pluginsResponse = await fetch(`${server}/Plugins`, { - headers: { 'X-Emby-Token': token } - }); - - // Handle 401 Unauthorized - retry once after waiting if waitForLogin is enabled - if (pluginsResponse.status === 401 && shouldWaitForLogin) { - WARN('Received 401 Unauthorized, waiting for login and retrying...'); - const loggedIn = await waitForLogin(5000); // Wait up to 5 more seconds - if (loggedIn) { - // Retry with fresh token - const freshToken = window.ApiClient.accessToken(); - pluginsResponse = await fetch(`${server}/Plugins`, { - headers: { 'X-Emby-Token': freshToken } - }); - } - } - - if (!pluginsResponse.ok) { - if (pluginsResponse.status === 401) { - throw new Error('Unauthorized - user may not be logged in or session expired'); - } - throw new Error(`Failed to get plugins: ${pluginsResponse.status} ${pluginsResponse.statusText}`); - } - - const pluginsData = await pluginsResponse.json(); + const pluginsData = await apiHelper.getPlugins(); const pluginsList = Array.isArray(pluginsData) ? pluginsData : (pluginsData.Items || []); const plugin = pluginsList.find(p => p.Name === 'JavaScript Injector' || p.Name === 'JS Injector'); @@ -463,7 +451,7 @@ // Get current injector config const configUrl = `${server}/Plugins/${pluginId}/Configuration`; const configResponse = await fetch(configUrl, { - headers: { 'X-Emby-Token': token } + headers: { 'Authorization': getAuthHeader() } }); if (!configResponse.ok) { @@ -506,7 +494,7 @@ window.KefinTweaksConfig = ${JSON.stringify(configToSave, null, 2)};`; const saveResponse = await fetch(configUrl, { method: 'POST', headers: { - 'X-Emby-Token': token, + 'Authorization': getAuthHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify(injectorConfig)