-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed.js
139 lines (121 loc) · 3.19 KB
/
embed.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
let debug = false
function log(...args) {
if (debug) {
console.log('🖼️', ...args)
}
}
//#region Default config
/** @type {Partial<import("./types").EmbedConfig>} */
let config = {
enabled: true,
hideEmbedEndVideos: true,
hideEmbedPauseOverlay: true,
hideEmbedShareButton: false,
}
//#endregion
//#region Utility functions
function addStyle(css = '') {
let $style = document.createElement('style')
$style.dataset.insertedBy = 'control-panel-for-youtube'
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.hideEmbedEndVideos) {
hideCssSelectors.push('.videowall-endscreen')
}
if (config.hideEmbedPauseOverlay) {
hideCssSelectors.push('.ytp-pause-overlay-container')
}
if (config.hideEmbedShareButton) {
hideCssSelectors.push('.ytp-share-button')
}
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) {
let configChanges = Object.fromEntries(
Object.entries(storageChanges)
.filter(([key]) => config.hasOwnProperty(key))
.map(([key, {newValue}]) => [key, newValue])
)
if (Object.keys(configChanges).length > 0) {
Object.assign(config, configChanges)
configChanged(configChanges)
}
}
chrome.storage.local.get((storedConfig) => {
Object.assign(
config,
Object.fromEntries(
Object.entries(storedConfig).filter(([key]) => config.hasOwnProperty(key))
)
)
log('initial config', config)
chrome.storage.local.onChanged.addListener(onConfigChange)
window.addEventListener('unload', () => {
chrome.storage.local.onChanged.removeListener(onConfigChange)
}, {once: true})
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