forked from FriedChickenButt/youtube-webos
-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathconfig.js
More file actions
188 lines (166 loc) · 4.25 KB
/
Copy pathconfig.js
File metadata and controls
188 lines (166 loc) · 4.25 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
const CONFIG_KEY = 'ytaf-configuration';
const configOptions = new Map([
['enableAdBlock', { default: true, desc: 'Enable ad blocking' }],
['upgradeThumbnails', { default: false, desc: 'Upgrade thumbnail quality' }],
[
'removeShorts',
{ default: false, desc: 'Remove Shorts from subscriptions' }
],
['enableSponsorBlock', { default: true, desc: 'Enable SponsorBlock' }],
[
'enableSponsorBlockSponsor',
{ default: true, desc: 'Skip sponsor segments' }
],
['enableSponsorBlockIntro', { default: true, desc: 'Skip intro segments' }],
['enableSponsorBlockOutro', { default: true, desc: 'Skip outro segments' }],
[
'enableSponsorBlockInteraction',
{
default: true,
desc: 'Skip interaction reminder segments'
}
],
[
'enableSponsorBlockSelfPromo',
{
default: true,
desc: 'Skip self promotion segments'
}
],
[
'enableSponsorBlockMusicOfftopic',
{
default: true,
desc: 'Skip non-music segments in music videos'
}
],
[
'enableSponsorBlockPreview',
{
default: false,
desc: 'Skip recaps and previews'
}
],
[
'hideLogo',
{
default: false,
desc: 'Hide YouTube logo'
}
],
[
'showWatch',
{
default: false,
desc: 'Display time in UI'
}
],
[
'forceHighResVideo',
{
default: false,
desc: 'Force max resolution video playback'
}
],
[
'enableScrollSeek',
{
default: true,
desc: 'Enable video seeking with scroll-wheel'
}
],
[
'removeEndscreen',
{
default: false,
desc: 'Remove end screens from video'
}
]
]);
const defaultConfig = (() => {
let ret = {};
for (const [k, v] of configOptions) {
ret[k] = v.default;
}
return ret;
})();
/** @type {Record<string, DocumentFragment>} as const */
const configFrags = (() => {
let ret = {};
for (const k of configOptions.keys()) {
ret[k] = new DocumentFragment();
}
return ret;
})();
function loadStoredConfig() {
const storage = window.localStorage.getItem(CONFIG_KEY);
if (storage === null) {
console.info('Config not set; using defaults.');
return null;
}
try {
return JSON.parse(storage);
} catch (err) {
console.warn('Error parsing stored config:', err);
return null;
}
}
// Use defaultConfig as a prototype so writes to localConfig don't change it.
let localConfig = loadStoredConfig() ?? Object.create(defaultConfig);
function configExists(key) {
return configOptions.has(key);
}
export function configGetDesc(key) {
if (!configExists(key)) {
throw new Error('tried to get desc for unknown config key: ' + key);
}
return configOptions.get(key).desc;
}
export function configRead(key) {
if (!configExists(key)) {
throw new Error('tried to read unknown config key: ' + key);
}
if (localConfig[key] === undefined) {
console.warn(
'Populating key',
key,
'with default value',
defaultConfig[key]
);
localConfig[key] = defaultConfig[key];
}
return localConfig[key];
}
export function configWrite(key, value) {
if (!configExists(key)) {
throw new Error('tried to write unknown config key: ' + key);
}
const oldValue =
localConfig[key] !== undefined ? localConfig[key] : defaultConfig[key];
console.info('Changing key', key, 'from', oldValue, 'to', value);
localConfig[key] = value;
window.localStorage[CONFIG_KEY] = JSON.stringify(localConfig);
configFrags[key].dispatchEvent(
new CustomEvent('ytafConfigChange', {
detail: { key, newValue: value, oldValue }
})
);
}
/**
* Add a listener for changes in the value of a specified config option
* @param {string} key Config option to monitor
* @param {(evt: Event) => void} callback Function to be called on change
*/
export function configAddChangeListener(key, callback) {
const frag = configFrags[key];
frag.addEventListener('ytafConfigChange', callback);
}
/**
* Remove a listener for changes in the value of a specified config option
* @param {string} key Config option to monitor
* @param {(evt: Event) => void} callback Function to be called on change
*/
export function configRemoveChangeListener(key, callback) {
const frag = configFrags[key];
frag.removeEventListener('ytafConfigChange', callback);
}