-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathembed.js
More file actions
164 lines (141 loc) · 3.6 KB
/
Copy pathembed.js
File metadata and controls
164 lines (141 loc) · 3.6 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
let debug = false
function log(...args) {
if (debug) {
console.log('🛏️', ...args)
}
}
//#region Default config
/** @type {import("./types").EmbedConfig} */
let config = {
debug: false,
enabled: true,
hideEmbedPauseOverlay: true,
hideEmbedShareButton: true,
hideEndCards: true,
hideEndVideos: true,
hideInfoPanels: true,
removePink: false,
}
//#endregion
//#region Utility functions
function addStyle(css = '') {
let $style = document.createElement('style')
if (css) {
$style.textContent = css
}
document.head.appendChild($style)
return $style
}
/**
* @param {string} str
* @return {string}
*/
function dedent(str) {
str = str.replace(/^[ \t]*\r?\n/, '')
let indent = /^[ \t]+/m.exec(str)
if (indent) str = str.replace(new RegExp('^' + indent[0], 'gm'), '')
return str.replace(/(\r?\n)[ \t]+$/, '$1')
}
//#endregion
//#region CSS
const configureCss = (() => {
/** @type {HTMLStyleElement} */
let $style
return function configureCss() {
if (!config.enabled) {
log('removing stylesheet')
$style?.remove()
$style = null
return
}
let cssRules = []
let hideCssSelectors = []
if (config.hideEmbedPauseOverlay) {
hideCssSelectors.push('.ytp-pause-overlay-container')
}
if (config.hideEmbedShareButton) {
hideCssSelectors.push('.ytp-share-button')
}
if (config.hideEndCards) {
hideCssSelectors.push('.ytp-ce-element')
}
if (config.hideEndVideos) {
hideCssSelectors.push('.videowall-endscreen')
}
if (config.hideInfoPanels) {
hideCssSelectors.push('.ytp-info-panel-preview')
}
if (config.removePink) {
cssRules.push(`
.ytp-play-progress {
background: #f03 !important;
}
`)
}
if (hideCssSelectors.length > 0) {
cssRules.push(`
${hideCssSelectors.join(',\n')} {
display: none !important;
}
`)
}
let css = cssRules.map(dedent).join('\n')
if ($style == null) {
$style = addStyle(css)
} else {
$style.textContent = css
}
}
})()
//#endregion
//#region Main
function main() {
if (config.enabled) {
configureCss()
}
}
/** @param {Partial<import("./types").EmbedConfig>} changes */
function configChanged(changes) {
if (!changes.hasOwnProperty('enabled')) {
log('config changed', changes)
configureCss()
return
}
log(`${changes.enabled ? 'en' : 'dis'}abling extension functionality`)
configureCss()
}
/** @param {{[key: string]: chrome.storage.StorageChange}} storageChanges */
function onConfigChange(storageChanges) {
/** @type {Partial<import("./types").SiteConfig>} */
let configChanges = Object.fromEntries(
Object.entries(storageChanges)
.filter(([key]) => config.hasOwnProperty(key))
.map(([key, {newValue}]) => [key, newValue])
)
if (Object.keys(configChanges).length == 0) return
if ('debug' in configChanges) {
log('disabling debug mode')
debug = configChanges.debug
log('enabled debug mode')
return
}
Object.assign(config, configChanges)
configChanged(configChanges)
}
chrome.storage.local.get((storedConfig) => {
Object.assign(
config,
Object.fromEntries(
Object.entries(storedConfig).filter(([key]) => config.hasOwnProperty(key))
)
)
debug = config.debug
log('initial config', config)
chrome.storage.local.onChanged.addListener(onConfigChange)
main()
})
// XXX chrome.storage.local.get() callback isn't getting called in Safari - run with default settings
if (navigator.userAgent.includes('Safari/') && !/Chrom(e|ium)\//.test(navigator.userAgent)) {
main()
}
//#endregion