Repository navigation
Expand file tree
/
Copy paththeme_selector.js
More file actions
330 lines (283 loc) · 13.9 KB
/
Copy paththeme_selector.js
File metadata and controls
330 lines (283 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
(function() {
'use strict';
console.log('[🪼🎨Jellyfish Theme Selector] Script loaded');
const themes = {
'Default': '',
'Aurora': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/aurora.css");',
'Banana': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/banana.css");',
'Coal': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/coal.css");',
'Coral': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/coral.css");',
'Forest': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/forest.css");',
'Grass': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/grass.css");',
'Jellyblue': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/jellyblue.css");',
'Jellyflix': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/jellyflix.css");',
'Jellypurple': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/jellypurple.css");',
'Lavender': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/lavender.css");',
'Liquid Glass': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/liquidglass.css");',
'Midnight': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/midnight.css");',
'Mint': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/mint.css");',
'Ocean': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/ocean.css");',
'Peach': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/peach.css");',
'Watermelon': '@import url("https://cdn.jsdelivr.net/gh/n00bcodr/Jellyfish/colors/watermelon.css");'
};
// Set to true to have random themes ON by default, false to have it OFF.
const RANDOM_THEME_DEFAULT = false;
// ---Helper functions for Random Daily Theme ---
const isRandomThemeEnabled = (userId) => {
const setting = localStorage.getItem(`${userId}-randomThemeEnabled`);
// If the setting is not found in localStorage, return the default value.
if (setting === null) {
return RANDOM_THEME_DEFAULT;
}
// Otherwise, return the stored setting.
return setting === 'true';
};
const setRandomThemeEnabled = (userId, isEnabled) => localStorage.setItem(`${userId}-randomThemeEnabled`, isEnabled);
const getLastRandomDate = (userId) => localStorage.getItem(`${userId}-lastRandomThemeDate`);
const setLastRandomDate = (userId, date) => localStorage.setItem(`${userId}-lastRandomThemeDate`, date);
// Inject custom CSS for proper styling
const injectCustomCss = () => {
if (!document.getElementById('jellyfin-theme-selector-css')) {
const style = document.createElement('style');
style.id = 'jellyfin-theme-selector-css';
style.innerHTML = `
#theme-selector-body {
display: flex !important;
align-items: center;
justify-content: flex-start;
flex-direction: row;
flex-wrap: wrap;
gap: 1em;
padding: .4em .75em;
}
#theme-selector-select {
max-width: 200px !important;
min-width: 150px !important;
}
#random-theme-button {
padding: 0.5em 0.5em !important;
height: auto !important;
display: flex;
flex-wrap: wrap;
align-items: center;
background-color: transparent;
border-radius: 10px;
}
#random-theme-button.active {
background-color: #4CAF50;
color: white;
}
`;
document.head.appendChild(style);
console.log('[🪼🎨Jellyfish Theme Selector] Custom CSS injected');
}
};
// Extract user ID from the ApiClient
const extractUserId = () => {
try {
const userId = window.ApiClient?.getCurrentUserId?.();
if (userId) return userId;
} catch (e) {
console.error('[🪼🎨Jellyfish Theme Selector] Error extracting user ID:', e);
}
console.error('[🪼🎨Jellyfish Theme Selector] Could not extract user ID');
return null;
};
// Get/Set theme in localStorage
const getCurrentTheme = (userId) => localStorage.getItem(`${userId}-customCss`) || '';
const setTheme = (userId, themeValue, themeName = 'Default') => {
const key = `${userId}-customCss`;
if (themeValue) {
localStorage.setItem(key, themeValue);
console.log(`[🪼🎨Jellyfish Theme Selector] Theme set to: ${themeName}`);
} else {
localStorage.removeItem(key);
console.log('[🪼🎨Jellyfish Theme Selector] Theme cleared (default)');
}
};
// Show notification using Jellyfin's built-in system
const showNotification = (message) => {
try {
if (window.Dashboard?.alert) {
window.Dashboard.alert(message);
} else if (window.require) {
window.require(['toast'], (toast) => toast(message));
} else {
console.log(`[🪼🎨Jellyfish Theme Selector] Notification: ${message}`);
}
} catch (e) {
console.error('[🪼🎨Jellyfish Theme Selector] Failed to show notification:', e);
}
};
// Check for a pending notification after a page refresh
const checkPostRefreshNotification = () => {
const pendingNotification = sessionStorage.getItem('jellyfin-theme-applied');
if (pendingNotification) {
sessionStorage.removeItem('jellyfin-theme-applied');
setTimeout(() => showNotification(`Theme applied: ${pendingNotification}`), 1000);
}
};
// --- apply random theme if needed ---
const applyRandomThemeIfNeeded = () => {
const userId = extractUserId();
if (!userId || !isRandomThemeEnabled(userId)) return;
const today = new Date().toISOString().split('T')[0]; // Format: YYYY-MM-DD
const lastDate = getLastRandomDate(userId);
if (today !== lastDate) {
console.log('[🪼🎨Jellyfish Theme Selector] New day detected! Applying a random theme.');
const availableThemes = Object.keys(themes).filter(name => name !== 'Default');
const randomThemeName = availableThemes[Math.floor(Math.random() * availableThemes.length)];
const randomThemeValue = themes[randomThemeName];
setTheme(userId, randomThemeValue, randomThemeName);
setLastRandomDate(userId, today);
sessionStorage.setItem('jellyfin-theme-applied', `Random Daily (${randomThemeName})`);
window.location.reload();
} else {
console.log('[🪼🎨Jellyfish Theme Selector] Random theme already applied for today.');
}
};
// Create the theme selector element
const createThemeSelector = (userId) => {
const container = document.createElement('div');
container.className = 'theme-selector-container listItem-border';
container.id = 'jellyfin-theme-selector';
const listItem = document.createElement('div');
listItem.className = 'listItem';
listItem.id = 'theme-selector-item';
const icon = document.createElement('span');
icon.className = 'material-icons listItemIcon listItemIcon-transparent';
icon.id = 'theme-selector-icon';
icon.setAttribute('aria-hidden', 'true');
icon.textContent = 'palette';
const contentDiv = document.createElement('div');
contentDiv.className = 'listItemBody';
contentDiv.id = 'theme-selector-body';
const textLabel = document.createElement('div');
textLabel.className = 'listItemBodyText';
textLabel.id = 'theme-selector-label';
textLabel.textContent = 'Theme';
const select = document.createElement('select');
select.setAttribute('is', 'emby-select');
select.className = 'emby-select-withcolor emby-select';
select.id = 'theme-selector-select';
select.removeAttribute('label');
const currentThemeValue = getCurrentTheme(userId);
let selectedThemeName = 'Default';
for (const [name, value] of Object.entries(themes)) {
if (value === currentThemeValue) {
selectedThemeName = name;
break;
}
}
Object.keys(themes).forEach(themeName => {
const option = document.createElement('option');
option.value = themeName;
option.textContent = themeName;
if (themeName === selectedThemeName) option.selected = true;
select.appendChild(option);
});
// Manual theme change event
select.addEventListener('change', (e) => {
e.preventDefault();
e.stopPropagation();
const newThemeName = e.target.value;
setTheme(userId, themes[newThemeName], newThemeName);
// // Disable random daily theme if a manual selection is made
// if (isRandomThemeEnabled(userId)) {
// setRandomThemeEnabled(userId, false);
// console.log('[🪼🎨Jellyfish Theme Selector] Manual theme selected, disabling random daily theme.');
// showNotification('Random daily theme disabled.');
// }
sessionStorage.setItem('jellyfin-theme-applied', newThemeName);
setTimeout(() => window.location.reload(), 500);
});
const randomButton = document.createElement('button');
randomButton.setAttribute('is', 'emby-button');
randomButton.className = 'emby-button';
randomButton.id = 'random-theme-button';
const randomIcon = document.createElement('span');
randomIcon.className = 'material-icons';
randomIcon.textContent = 'shuffle';
randomIcon.setAttribute('aria-hidden', 'true');
const randomText = document.createElement('span');
const updateButtonState = () => {
if (isRandomThemeEnabled(userId)) {
randomButton.classList.add('active');
} else {
randomButton.classList.remove('active');
}
};
randomButton.appendChild(randomIcon);
randomButton.appendChild(randomText);
updateButtonState(); // Set initial state
randomButton.addEventListener('click', () => {
const newState = !isRandomThemeEnabled(userId);
setRandomThemeEnabled(userId, newState);
showNotification(`Random daily theme turned ${newState ? 'ON' : 'OFF'}.`);
console.log(`[🪼🎨Jellyfish Theme Selector] Random daily theme set to: ${newState}`);
updateButtonState();
// If just turned on, apply a theme immediately if it's a new day
if (newState) {
applyRandomThemeIfNeeded();
}
});
// Append all elements
contentDiv.appendChild(textLabel);
contentDiv.appendChild(randomButton);
contentDiv.appendChild(select);
listItem.appendChild(icon);
listItem.appendChild(contentDiv);
container.appendChild(listItem);
return container;
};
/**
* Finds the correct DOM location and injects the theme selector.
*/
const injectThemeSelector = () => {
const targetDiv = document.querySelector('.verticalSection .headerUsername');
if (!targetDiv) return false;
const parentSection = targetDiv.closest('.verticalSection');
if (!parentSection) return false;
const userId = extractUserId();
if (!userId) return false;
console.log('[🪼🎨Jellyfish Theme Selector] Creating theme selector element...');
const themeSelector = createThemeSelector(userId);
parentSection.appendChild(themeSelector);
console.log('[🪼🎨Jellyfish Theme Selector] Successfully injected!');
return true;
};
/**
* Main function to initialize the script.
*/
const initialize = () => {
if (typeof ApiClient === 'undefined' || typeof ApiClient.getCurrentUserId !== 'function') {
console.log('[🪼🎨Jellyfish Theme Selector] Waiting for ApiClient...');
setTimeout(initialize, 250);
return;
}
console.log('[🪼🎨Jellyfish Theme Selector] ApiClient is available. Starting persistent element monitoring.');
applyRandomThemeIfNeeded(); // Check for random theme on every page load
injectCustomCss();
checkPostRefreshNotification();
const observer = new MutationObserver(() => {
const onPreferencesPage = document.querySelector('.headerUsername') && document.querySelector('.lnkUserProfile');
const selectorExists = document.getElementById('jellyfin-theme-selector');
//If we are on the preferences page and the selector is not there, inject it.
if (onPreferencesPage && !selectorExists) {
console.log('[🪼🎨Jellyfish Theme Selector] Preferences page detected and selector is missing. Injecting...');
injectThemeSelector();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
console.log('[🪼🎨Jellyfish Theme Selector] Observer is now active and will monitor for navigation.');
};
// Start the script once the DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initialize);
} else {
initialize();
}
})();