-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththemeSwitchMain.js
683 lines (618 loc) · 27.8 KB
/
themeSwitchMain.js
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
(function () {
const svgChevDN =
'<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-down" class="svg-inline--fa fa-chevron-down fa-icon collapse-icon fa-fw" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M201.4 406.6c12.5 12.5 32.8 12.5 45.3 0l192-192c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L224 338.7 54.6 169.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l192 192z"></path></svg>';
const svgChevUP =
'<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-right" class="svg-inline--fa fa-chevron-right fa-icon collapse-icon fa-fw" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path fill="currentColor" d="M310.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-192 192c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L242.7 256 73.4 86.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l192 192z"></path></svg>';
const svgBTN =
'<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 102 123" width="20" height="20"><title>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0.1-0.7 0.2-0.3 0.1-0.7 0.2c-1.2 0.5-2.4 1.2-3.4 2.2-1 0.9-1.8 2-2.4 3.3-0.5 1.2-0.8 2.6-0.8 4-0.1 1.3 0.1 2.7 0.6 4z"/><path id="Layer" fill-rule="evenodd" class="s0" d="m100.3 17.7c2.4 7.9 1.5 18.1-6.8 21.8q-0.8 0.4-1.6 0.6-0.9 0.2-1.7 0.2-0.8 0.1-1.7 0-0.8-0.1-1.6-0.4-1.4-0.4-2.7-1.1-1.3-0.8-2.4-1.8-1-0.9-1.9-2.1-0.9-1.2-1.5-2.5c-5.6-12.7 10.4-20.2 11.1-32.4 3.6 4.2 8.5 10.5 10.8 17.7zm-0.1 45.2l-4 54.6q0 0.5-0.1 1-0.1 0.5-0.3 1-0.2 0.5-0.4 1-0.3 0.5-0.6 0.9c-0.7 0.6-1.4 1-2.3 1.3-0.8 0.2-1.7 0.2-2.6 0-0.8-0.2-1.6-0.6-2.3-1.2-0.6-0.6-1.1-1.3-1.4-2.1q-0.1-0.4-0.2-0.9-0.1-0.4-0.2-0.8 0-0.4-0.1-0.8 0-0.5 0-0.9l-5.4-52.9q2.4 0.5 4.9 0.7 2.5 0.3 5 0.3 2.5 0 5-0.3 2.5-0.3 5-0.9zm-5.5-17.8c2.5 7.5 2.8 8.2 6 14.5-6.5 1.8-13.4 1.5-20.4 0l4.3-14.5c3.2 1.6 7.7 1 10.2 0z"/><path id="Layer" fill-rule="evenodd" class="s4" d="m40.8 101.6c1.3 3.4 4.5 5.7 8.2 5.8 3.7 0.1 7.1-2 8.6-5.4 1.5-3.3 0.8-7.3-1.7-9.9-2.6-2.7-6.5-3.6-9.9-2.3-1.1 0.5-2.1 1.1-3 1.9-0.9 0.9-1.6 1.8-2 2.9-0.5 1.1-0.8 2.3-0.8 3.5-0.1 1.2 0.2 2.4 0.6 3.5z"/></svg>';
let menuCreated = false;
function menuOrderListReset(element) {
const defaultOrder = [
"Scenes",
"Images",
"Movies",
"Markers",
"Galleries",
"Performers",
"Studios",
"Tags",
];
const ul = element.querySelector("ul");
const lis = Array.from(ul.querySelectorAll("li"));
// Sort the li elements based on their text content
lis.sort((a, b) => {
const aIndex = defaultOrder.indexOf(a.textContent);
const bIndex = defaultOrder.indexOf(b.textContent);
return aIndex - bIndex;
});
// Append the sorted li elements to the ul in their new order
lis.forEach((li) => ul.appendChild(li));
}
function menuOrderResetButton(elementToAppend) {
if (!document.getElementById("themeSwitchPlugin-menuOrderReset")) {
const resetButton = document.createElement("button");
resetButton.id = "themeSwitchPlugin-menuOrderReset";
resetButton.className = "btn btn-primary";
resetButton.innerHTML = "Reset Menu Order";
resetButton.addEventListener("click", function () {
localStorage.removeItem(
"themeSwitchPlugin-menu-changeOrderOfNavButtons"
);
document
.getElementById("themeSwitchPlugin-menu-changeOrderOfNavButtons")
.remove();
menuOrderListReset(elementToAppend);
resetButton.remove();
});
elementToAppend.appendChild(resetButton);
}
}
function buldDragCSS(themename, key) {
const container = document.querySelector(".draggable-ul-container");
const list = container.querySelectorAll("li");
let css = "";
for (let i = 0; i < list.length; i++) {
const item = list[i];
const key = item.getAttribute("data-rb-event-key");
const order = i - list.length;
css += `div.nav-link[data-rb-event-key="${key}"] { order: ${order}; }`;
}
const style = document.createElement("style");
style.type = "text/css";
style.id = key;
style.innerHTML = `nav .navbar-nav:first-child {
display: flex;
flex-direction: row;
}
${css}`;
const existing = document.getElementById(key);
if (existing) {
existing.remove();
}
document.getElementsByTagName("head")[0].appendChild(style);
const data = {
name: themename,
css: css,
key: key,
id: key,
applied: "true",
};
localStorage.setItem(key, JSON.stringify(data));
menuOrderResetButton(container.parentElement);
}
function enableDragSort(listClass) {
const sortableLists = document.getElementsByClassName(listClass);
Array.prototype.map.call(sortableLists, (list) => {
enableDragList(list);
});
}
function enableDragList(list) {
Array.prototype.map.call(list.children, (item) => {
enableDragItem(item);
});
}
function enableDragItem(item) {
item.setAttribute("draggable", true);
item.addEventListener("touchstart", handleTouchStart, { passive: true });
item.addEventListener("touchmove", handleTouchMove, { passive: true });
item.addEventListener("touchend", handleTouchEnd, { passive: true });
item.ondrag = handleDrag;
item.ondragend = handleDrop;
}
let touchEndY = null;
function handleTouchStart(event) {
event.preventDefault();
touchStartY = event.touches[0].clientY;
}
function handleTouchMove(event) {
event.preventDefault();
touchEndY = event.changedTouches[0].clientY;
const selectedItem = event.target,
list = selectedItem.parentNode;
selectedItem.classList.add("drag-sort-active");
let swapItem =
document.elementFromPoint(
selectedItem.getBoundingClientRect().x,
touchEndY
) === null
? selectedItem
: document.elementFromPoint(
selectedItem.getBoundingClientRect().x,
touchEndY
);
if (list === swapItem.parentNode) {
swapItem =
swapItem !== selectedItem.nextSibling ? swapItem : swapItem.nextSibling;
list.insertBefore(selectedItem, swapItem);
}
}
function handleTouchEnd(event) {
event.preventDefault();
touchEndY = event.changedTouches[0].clientY;
handleDragTouch(event.target);
}
function handleDragTouch(item) {
const selectedItem = item,
list = selectedItem.parentNode;
selectedItem.classList.remove("drag-sort-active");
let swapItem =
document.elementFromPoint(
selectedItem.getBoundingClientRect().x,
touchEndY
) === null
? selectedItem
: document.elementFromPoint(
selectedItem.getBoundingClientRect().x,
touchEndY
);
if (list === swapItem.parentNode) {
swapItem =
swapItem !== selectedItem.nextSibling ? swapItem : swapItem.nextSibling;
list.insertBefore(selectedItem, swapItem);
}
setTimeout(() => {
buldDragCSS(
"Change the order of navigation bar buttons",
"themeSwitchPlugin-menu-changeOrderOfNavButtons"
);
}, 100);
}
function handleDrag(item) {
const selectedItem = item.target,
list = selectedItem.parentNode,
x = event.clientX,
y = event.clientY;
selectedItem.classList.add("drag-sort-active");
let swapItem =
document.elementFromPoint(x, y) === null
? selectedItem
: document.elementFromPoint(x, y);
if (list === swapItem.parentNode) {
swapItem =
swapItem !== selectedItem.nextSibling ? swapItem : swapItem.nextSibling;
list.insertBefore(selectedItem, swapItem);
}
}
function handleDrop(item) {
item.target.classList.remove("drag-sort-active");
setTimeout(() => {
buldDragCSS(
"Change the order of navigation bar buttons",
"themeSwitchPlugin-menu-changeOrderOfNavButtons"
);
}, 100);
}
(() => {
enableDragSort("drag-sort-enable");
})();
waitForElementClass("draggable-ul-container", function () {
enableDragSort("draggable-ul-container");
});
function setObject(key, category, active) {
return new Promise((resolve, reject) => {
try {
localStorage.setItem(
key,
JSON.stringify({ category: category, active: active })
);
resolve();
} catch (error) {
reject(error);
}
});
}
function checkActive(key) {
let object = JSON.parse(localStorage.getItem(key));
if (object && object.active === true) {
return true;
} else {
return false;
}
}
function applyStyleToHead(key, css) {
const styleElement = document.createElement("style");
styleElement.setAttribute("type", "text/css");
const cssTextNode = document.createTextNode(css);
styleElement.id = key;
styleElement.appendChild(cssTextNode);
document.getElementsByTagName("head")[0].appendChild(styleElement);
}
function applyCSS(category, key, css, set) {
if (category === "Themes") {
// Turn Off old Theme
let regex = /(themeSwitchPlugin-theme-.*)/;
for (let i = 0; i < localStorage.length; i++) {
let storageKey = localStorage.key(i);
let match = storageKey.match(regex);
if (
match &&
storageKey !== key &&
JSON.parse(localStorage.getItem(storageKey)).active === true
) {
setObject(storageKey, category, false);
let element = document.getElementById(storageKey);
if (element) {
element.remove();
}
}
}
const theme = JSON.parse(localStorage.getItem(key));
if (!theme && key != "themeSwitchPlugin-theme-default") {
setObject(key, category, true).then(() => {
applyStyleToHead(key, css);
});
} else if (!theme && key === "themeSwitchPlugin-theme-default") {
setObject(key, category, true);
} else if (theme && key === "themeSwitchPlugin-theme-default") {
setObject(key, category, true);
} else if (theme && key !== "themeSwitchPlugin-theme-default") {
setObject(key, category, true).then(() => {
applyStyleToHead(key, css);
});
}
} else {
// CSS Other than themes
const storageObject = JSON.parse(localStorage.getItem(key));
if (!storageObject || storageObject.active === false) {
setObject(key, category, true).then(() => {
applyStyleToHead(key, css);
});
} else if (storageObject.active && !document.getElementById(key)) {
applyStyleToHead(key, css);
} else {
setObject(key, category, false).then(() => {
document.getElementById(key).remove();
});
}
}
}
function expandCollapse(category, name, span, collapse) {
let categorySpanClick = document.getElementById(span);
let categoryCollapseClick = document.getElementById(collapse);
let expanded =
categoryCollapseClick.getAttribute("aria-expanded") === "true";
if (expanded) {
categoryCollapseClick.setAttribute("aria-expanded", "false");
categoryCollapseClick.classList.add("expanding");
setTimeout(function () {
categoryCollapseClick.classList.remove("show");
categoryCollapseClick.classList.remove("expanding");
categorySpanClick.innerHTML = svgChevUP + category;
}, 300);
} else {
categoryCollapseClick.setAttribute("aria-expanded", "true");
categoryCollapseClick.classList.add("expanding");
setTimeout(function () {
categoryCollapseClick.classList.add("show");
categoryCollapseClick.classList.remove("expanding");
categorySpanClick.innerHTML = svgChevDN + category;
}, 300);
}
}
function createBTNMenu() {
return new Promise(function (resolve, reject) {
waitForElementClass("top-nav", function () {
if (!document.getElementById("themeSwitchPlugin")) {
const pluginDiv = document.createElement("div");
pluginDiv.className = "mr-2 dropdown";
pluginDiv.innerHTML =
'<button id="themeSwitchPlugin" aria-haspopup="true" aria-expanded="false" type="button" class="dropdown-toggle minimal d-flex align-items-center h-100 btn btn-primary" title="Theme Switcher">' +
svgBTN +
"</button>";
const themesDiv = document.createElement("div");
themesDiv.className = "dropdown-menu theme-plugin-menu";
document.addEventListener("click", function (event) {
const isClickInside =
pluginDiv.contains(event.target) ||
themesDiv.contains(event.target);
const isClickInsideThemesDiv = themesDiv.contains(event.target);
const themeSwitchPlugin =
document.getElementById("themeSwitchPlugin");
const expanded =
themeSwitchPlugin.getAttribute("aria-expanded") === "true";
if (expanded && !isClickInsideThemesDiv) {
themeSwitchPlugin.setAttribute("aria-expanded", "false");
themesDiv.classList.remove("show");
themesDiv.style =
"position: absolute; to0px; left: 0px; margin: 0px; opacity: 0; pointer-events: none;";
} else if (!expanded && isClickInside) {
themeSwitchPlugin.setAttribute("aria-expanded", "true");
themesDiv.classList.add("show");
themesDiv.style =
"position: absolute; top: 133%; left: 0px; margin: 0px; opacity: 1; pointer-events: auto; width: max-content; min-width: 20rem; left: -575%;";
} else if (!isClickInside && !isClickInsideThemesDiv) {
themeSwitchPlugin.setAttribute("aria-expanded", "false");
themesDiv.classList.remove("show");
themesDiv.style =
"position: absolute; top: 0px; left: 0px; margin: 0px; opacity: 0; pointer-events: none;";
}
});
const accordion = document.createElement("div");
accordion.className = "criterion-list accordion";
accordion.style = "max-width: 20rem !important;";
const header = document.createElement("div");
header.innerHTML = "Theme/CSS Switcher";
header.className = "modal-header";
accordion.append(header);
Object.entries(themeSwitchCSS).forEach(
([category, themesInCategory], i) => {
const categoryDiv = document.createElement("div");
categoryDiv.className = "card";
categoryDiv.setAttribute("data-type", category);
categoryDiv.setAttribute(
"id",
"themeSwitchPlugin-card-" + category
);
categoryDiv.style = "padding: 2px !important;";
const categoryHeader = document.createElement("div");
categoryHeader.className = "card-header";
categoryHeader.style = "padding: 0.375rem 0.625rem !important;";
const categorySpan = document.createElement("span");
categorySpan.innerHTML = svgChevUP + category;
categorySpan.setAttribute(
"id",
"themeSwitchPlugin-span-" + category
);
const collapseDiv = document.createElement("div");
collapseDiv.className = "collapse";
collapseDiv.setAttribute("aria-expanded", "false");
collapseDiv.setAttribute(
"id",
"themeSwitchPlugin-collapse-" + category
);
categoryDiv.addEventListener(
"click",
(function () {
return function (event) {
if (!event.target.closest(".collapse")) {
expandCollapse(
category,
"themeSwitchPlugin-card-" + category,
"themeSwitchPlugin-span-" + category,
"themeSwitchPlugin-collapse-" + category
);
}
};
})(i),
{ capture: true }
);
const cardBody = document.createElement("div");
cardBody.className = "card-body";
const editorDiv = document.createElement("div");
editorDiv.className = "criterion-editor";
const blankDiv = document.createElement("div");
const optionListFilter = document.createElement("div");
optionListFilter.className = "option-list-filter";
blankDiv.append(optionListFilter);
editorDiv.append(blankDiv);
cardBody.append(editorDiv);
collapseDiv.append(cardBody);
categoryHeader.append(categorySpan, collapseDiv);
const fieldset = document.createElement("fieldset");
fieldset.className = "checkbox-switch";
// Loop over themes in each category
Object.entries(themesInCategory).forEach(([themeId, theme]) => {
if (category === "Navigation") {
} else {
const forRow = document.createElement("div");
forRow.className = "checkbox-switch-form-row";
const legend = document.createElement("legend");
legend.innerHTML = theme.displayName;
legend.className = "legend-right";
const input = document.createElement("input");
if (category === "Themes") {
input.type = "radio";
input.name = "themeGroup";
const active = checkActive(theme.key);
input.checked = active;
} else {
input.type = "checkbox";
const active = checkActive(theme.key);
input.checked = active;
}
const themeData = {
category: category,
key: theme.key,
css: theme.styles,
};
input.setAttribute("id", category + "-" + theme.key);
input.addEventListener(
"click",
(function (category, key, css) {
return function () {
applyCSS(category, key, css);
};
})(themeData.category, themeData.key, themeData.css),
false
);
const label = document.createElement("label");
label.setAttribute("for", category + "-" + theme.key);
label.title = "Turn " + theme.displayName + " on/off";
label.className = "checkbox-right";
// Append the legend, input, and label elements to the fieldset element
forRow.appendChild(legend);
forRow.appendChild(input);
forRow.appendChild(label);
fieldset.appendChild(forRow);
optionListFilter.appendChild(fieldset);
categoryDiv.append(categoryHeader);
accordion.append(categoryDiv);
}
});
Object.entries(themesInCategory).forEach(([themeId, theme]) => {
if (category === "Navigation") {
const forRow = document.createElement("div");
forRow.className = "switch-form-row";
const legend = document.createElement("legend");
legend.innerHTML = theme.displayName;
legend.className = "legend-left";
const draggableStylesheetContent = localStorage.getItem(
"themeSwitchPlugin-menu-changeOrderOfNavButtons"
);
if (draggableStylesheetContent) {
// Read the stylesheet content and create an array called "elements"
var reset = true;
var navMenuItems = [];
const css = JSON.parse(draggableStylesheetContent).css;
const regex = /data-rb-event-key="\/([^"]+)"/g;
let match;
while ((match = regex.exec(css))) {
const key = match[1];
if (match[1] === "scenes/markers") {
const name = "Markers";
navMenuItems.push({ name, key });
} else {
const name =
match[1].charAt(0).toUpperCase() + match[1].slice(1);
navMenuItems.push({ name, key });
}
}
navMenuItems.sort((a, b) => a.order - b.order);
} else {
var navMenuItems = [
{ name: "Scenes", key: "scenes" },
{ name: "Images", key: "images" },
{ name: "Movies", key: "movies" },
{ name: "Markers", key: "scenes/markers" },
{ name: "Galleries", key: "galleries" },
{ name: "Performers", key: "performers" },
{ name: "Studios", key: "studios" },
{ name: "Tags", key: "tags" },
];
}
// const label = document.createElement("label");
legend.setAttribute("for", category + "-" + theme.key);
optionListFilter.appendChild(legend);
const formCheck = document.createElement("ul");
formCheck.className = "draggable-ul-container";
for (let i = 0; i < navMenuItems.length; i++) {
const li = document.createElement("li");
li.className = "draggable-li";
li.setAttribute(
"data-rb-event-key",
"/" + navMenuItems[i].key
);
li.setAttribute("draggable", true);
const newSpan = document.createElement("span");
newSpan.className = "grippy";
li.appendChild(newSpan);
const textNode = document.createTextNode(
navMenuItems[i].name
);
li.appendChild(textNode);
formCheck.appendChild(li);
}
optionListFilter.appendChild(formCheck);
if (reset) {
menuOrderResetButton(optionListFilter);
}
fieldset.appendChild(forRow);
optionListFilter.appendChild(fieldset);
categoryDiv.append(categoryHeader);
accordion.append(categoryDiv);
}
});
}
);
themesDiv.append(accordion);
pluginDiv.append(themesDiv);
waitForElementClass("navbar-buttons", function () {
const mainDiv =
document.getElementsByClassName("navbar-buttons")[0];
const secondLastChild =
mainDiv.childNodes[mainDiv.childNodes.length - 4];
mainDiv.insertBefore(pluginDiv, secondLastChild);
// Resolving the promise once everything is set up
menuCreated = true;
resolve();
});
} else {
// Rejecting the promise if the element already exists
reject(new Error("Element already exists"));
}
});
});
}
function returnCSS(key) {
for (const [, categoryThemes] of Object.entries(themeSwitchCSS)) {
for (const [, theme] of Object.entries(categoryThemes)) {
if (key === theme.key) {
return theme.styles;
}
}
}
}
function init() {
// Apply active Themes and stylesheets
let selectedTheme;
const regex = /(themeSwitchPlugin(?!-theme-default).*)/;
const defaultRegex = /(themeSwitchPlugin-theme-default)/;
let defaultFound = false;
let appliedThemeOtherThanDefault = [];
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
let defaultMatch = key.match(defaultRegex);
if (defaultMatch) {
defaultFound = true;
}
let match = key.match(regex);
if (match) {
selectedTheme = JSON.parse(localStorage.getItem(key));
if (selectedTheme.active === true) {
appliedThemeOtherThanDefault.push("True");
const css = returnCSS(key);
applyCSS(selectedTheme.category, key, css, true);
}
}
}
if (!defaultFound) {
let setDefaultActive;
if (appliedThemeOtherThanDefault.length > 0) {
setDefaultActive = false;
} else {
setDefaultActive = true;
}
setObject("themeSwitchPlugin-theme-default", "Themes", setDefaultActive);
}
}
const StashPages = [
"stash:page:scenes",
"stash:page:scene",
"stash:page:images",
"stash:page:image",
"stash:page:movies",
"stash:page:movie",
"stash:page:markers",
"stash:page:galleries",
"stash:page:performers",
"stash:page:performer",
"stash:page:studios",
"stash:page:studio",
"stash:page:tags",
"stash:page:tag",
"stash:page:settings",
"stash:page:stats",
"stash:page:home",
];
function createMenuAndInit() {
if (!menuCreated) {
createBTNMenu()
.then(function () {
console.log("Theme Plugin: Menu created successfully");
})
.catch(function (error) {
console.error("Theme Plugin: Error creating menu:", error.message);
});
menuCreated = true;
}
init();
}
for (var i = 0; i < StashPages.length; i++) {
const page = StashPages[i];
stash.addEventListener(page, createMenuAndInit);
}
// Reset menuCreated flag on hard refresh
window.addEventListener("beforeunload", function () {
menuCreated = false;
});
})();