Skip to content

Commit 6de565e

Browse files
author
ImprovedTube
authored
Merge pull request #3708 from Saurabh-2607/fix/theme-light-after-dark
Fix YouTube Light and Dark theme switching in Themes panel Fix #3704
2 parents 8d8601a + 4451a2e commit 6de565e

4 files changed

Lines changed: 115 additions & 9 deletions

File tree

js&css/web-accessible/functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ ImprovedTube.setPrefCookieValueByName = function (name, value) {
626626
let newPrefs = '';
627627
let ampersant = '';
628628

629-
if (name == 'f6' && prefs[name] & 1) {
629+
if (name == 'f6' && value != null && prefs[name] & 1) {
630630
// f6 holds other settings, possible values 80000 80001 400 401 1 none
631631
// make sure we remember 1 bit
632632
prefs[name] = value | 1;

menu/skeleton-parts/themes.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,32 @@ extension.skeleton.main.layers.section.themes.on.click.section = {
4646
variant: 'transparent-card',
4747

4848
default: function () {
49+
var theme = satus.storage.get('theme');
50+
4951
return {
5052
component: 'label',
51-
variant: satus.storage.get('theme') == 'dark' ? 'dark-theme' : 'default-theme',
52-
text: satus.storage.get('theme') == 'dark' ? 'youtubesDark' : 'youtubesLight',
53+
variant: 'default-theme',
54+
text: 'youtubesLight',
5355
radio: {
5456
component: 'radio',
5557
group: 'theme',
56-
value: satus.storage.get('theme') == 'dark' ? 'dark' : 'light',
57-
...(!satus.storage.get('theme') && { checked: true })
58+
value: 'light',
59+
...((!theme || theme == 'light') && { checked: true })
5860
}
5961
}
6062
},
6163
opposite: function () {
64+
var theme = satus.storage.get('theme');
65+
6266
return {
6367
component: 'label',
64-
variant: satus.storage.get('theme') == 'dark' ? 'default-theme' : 'dark-theme',
65-
text: satus.storage.get('theme') == 'dark' ? 'youtubesLight' : 'youtubesDark',
68+
variant: 'dark-theme',
69+
text: 'youtubesDark',
6670
radio: {
6771
component: 'radio',
6872
group: 'theme',
69-
value: satus.storage.get('theme') == 'dark' ? 'light' : 'dark',
70-
...(satus.storage.get('theme') == 'dark' && { checked: true })
73+
value: 'dark',
74+
...(theme == 'dark' && { checked: true })
7175
}
7276
}
7377
},

tests/unit/theme-cookie.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
global.ImprovedTube = {
2+
storage: {},
3+
elements: {},
4+
button_icons: {}
5+
};
6+
7+
global.document = {
8+
cookie: ''
9+
};
10+
11+
require('../../js&css/web-accessible/functions.js');
12+
13+
describe('Theme PREF cookie handling', () => {
14+
beforeEach(() => {
15+
document.cookie = '';
16+
ImprovedTube.setCookie = jest.fn(function (name, value) {
17+
document.cookie = name + '=' + value;
18+
});
19+
});
20+
21+
test('preserves the extra f6 bit when enabling dark theme', () => {
22+
document.cookie = 'PREF=f6=80001';
23+
24+
ImprovedTube.setPrefCookieValueByName('f6', 400);
25+
26+
expect(ImprovedTube.setCookie).toHaveBeenCalledWith('PREF', 'f6=401');
27+
});
28+
29+
test('removes the f6 theme preference when switching back to light', () => {
30+
document.cookie = 'PREF=f6=401';
31+
32+
ImprovedTube.setPrefCookieValueByName('f6', null);
33+
34+
expect(ImprovedTube.setCookie).toHaveBeenCalledWith('PREF', '');
35+
});
36+
});

tests/unit/themes-menu.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
global.extension = {
2+
skeleton: {
3+
main: {
4+
layers: {
5+
section: {}
6+
}
7+
}
8+
}
9+
};
10+
11+
global.satus = {
12+
storage: {
13+
get: jest.fn()
14+
}
15+
};
16+
17+
require('../../menu/skeleton-parts/themes.js');
18+
19+
describe('Themes menu radio state', () => {
20+
beforeEach(() => {
21+
jest.clearAllMocks();
22+
});
23+
24+
test('keeps YouTube light as the default option when there is no stored theme', () => {
25+
satus.storage.get.mockReturnValue(undefined);
26+
27+
const section = extension.skeleton.main.layers.section.themes.on.click.section;
28+
const currentTheme = section.default();
29+
const oppositeTheme = section.opposite();
30+
31+
expect(currentTheme.text).toBe('youtubesLight');
32+
expect(currentTheme.radio.value).toBe('light');
33+
expect(currentTheme.radio.checked).toBe(true);
34+
expect(oppositeTheme.text).toBe('youtubesDark');
35+
expect(oppositeTheme.radio.value).toBe('dark');
36+
expect(oppositeTheme.radio.checked).toBeUndefined();
37+
});
38+
39+
test('marks YouTube dark as checked when dark is the current theme', () => {
40+
satus.storage.get.mockReturnValue('dark');
41+
42+
const section = extension.skeleton.main.layers.section.themes.on.click.section;
43+
const currentTheme = section.default();
44+
const oppositeTheme = section.opposite();
45+
46+
expect(currentTheme.text).toBe('youtubesLight');
47+
expect(currentTheme.radio.value).toBe('light');
48+
expect(currentTheme.radio.checked).toBeUndefined();
49+
expect(oppositeTheme.text).toBe('youtubesDark');
50+
expect(oppositeTheme.radio.value).toBe('dark');
51+
expect(oppositeTheme.radio.checked).toBe(true);
52+
});
53+
54+
test('keeps YouTube light selected when light is explicitly stored', () => {
55+
satus.storage.get.mockReturnValue('light');
56+
57+
const section = extension.skeleton.main.layers.section.themes.on.click.section;
58+
const currentTheme = section.default();
59+
const oppositeTheme = section.opposite();
60+
61+
expect(currentTheme.radio.value).toBe('light');
62+
expect(currentTheme.radio.checked).toBe(true);
63+
expect(oppositeTheme.radio.value).toBe('dark');
64+
expect(oppositeTheme.radio.checked).toBeUndefined();
65+
});
66+
});

0 commit comments

Comments
 (0)