-
Notifications
You must be signed in to change notification settings - Fork 35
/
popup.js
256 lines (251 loc) · 10.4 KB
/
popup.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
'use strict'
let tid = 0
const frameMap = new Map()
const elementsList = document.getElementById('elements-list')
const allElements = document.getElementById('all-elements')
const indivElements = document.getElementById('individual-elements')
const elementsTpl = document.getElementById('elements-tpl')
function applySettings (fid, elid, newSettings) {
return browser.tabs.executeScript(tid, { frameId: fid, code: `(function () {
const el = document.querySelector('[data-x-soundfixer-id="${elid}"]')
if (!el.xSoundFixerContext) {
el.xSoundFixerContext = new AudioContext()
el.xSoundFixerGain = el.xSoundFixerContext.createGain()
el.xSoundFixerPan = el.xSoundFixerContext.createStereoPanner()
el.xSoundFixerSplit = el.xSoundFixerContext.createChannelSplitter(2)
el.xSoundFixerMerge = el.xSoundFixerContext.createChannelMerger(2)
el.xSoundFixerSource = el.xSoundFixerContext.createMediaElementSource(el)
el.xSoundFixerSource.connect(el.xSoundFixerGain)
el.xSoundFixerGain.connect(el.xSoundFixerPan)
el.xSoundFixerPan.connect(el.xSoundFixerContext.destination)
el.xSoundFixerOriginalChannels = el.xSoundFixerContext.destination.channelCount
}
const newSettings = ${JSON.stringify(newSettings)}
if ('gain' in newSettings) {
el.xSoundFixerGain.gain.value = newSettings.gain
}
if ('pan' in newSettings) {
el.xSoundFixerPan.pan.value = newSettings.pan
}
if ('mono' in newSettings) {
el.xSoundFixerContext.destination.channelCount = newSettings.mono ? 1 : el.xSoundFixerOriginalChannels
}
if ('flip' in newSettings) {
el.xSoundFixerFlipped = newSettings.flip
el.xSoundFixerMerge.disconnect()
el.xSoundFixerPan.disconnect()
if (el.xSoundFixerFlipped) {
el.xSoundFixerPan.connect(el.xSoundFixerSplit)
el.xSoundFixerSplit.connect(el.xSoundFixerMerge, 0, 1)
el.xSoundFixerSplit.connect(el.xSoundFixerMerge, 1, 0)
el.xSoundFixerMerge.connect(el.xSoundFixerContext.destination)
} else {
el.xSoundFixerPan.connect(el.xSoundFixerContext.destination)
}
}
el.xSoundFixerSettings = {
gain: el.xSoundFixerGain.gain.value,
pan: el.xSoundFixerPan.pan.value,
mono: el.xSoundFixerContext.destination.channelCount == 1,
flip: el.xSoundFixerFlipped,
}
})()` })
}
browser.tabs.query({ currentWindow: true, active: true }).then(tabs => {
tid = tabs[0].id
return browser.webNavigation.getAllFrames({ tabId: tid }).then(frames =>
Promise.all(frames.map(frame => {
const fid = frame.frameId
return browser.tabs.executeScript(tid, { frameId: fid, code: `(function () {
const result = new Map()
for (const el of document.querySelectorAll('video, audio')) {
if (!el.hasAttribute('data-x-soundfixer-id')) {
el.setAttribute('data-x-soundfixer-id',
Math.random().toString(36).substr(2, 10))
}
result.set(el.getAttribute('data-x-soundfixer-id'), {
type: el.tagName.toLowerCase(),
isPlaying: (el.currentTime > 0 && !el.paused && !el.ended && el.readyState > 2),
settings: el.xSoundFixerSettings
})
}
return result
})()` }).then(result => frameMap.set(fid, result[0]))
.catch(err => console.error(`tab ${tid} frame ${fid}`, err))
}))
)
}).then(_ => {
elementsList.textContent = ''
let elCount = 0
for (const [fid, els] of frameMap) {
for (const [elid, el] of els) {
const settings = el.settings || {}
const node = document.createElement('li')
node.appendChild(document.importNode(elementsTpl.content, true))
node.dataset.fid = fid
node.dataset.elid = elid
node.querySelector('.element-label').textContent = `
${el.type.charAt(0).toUpperCase() + el.type.slice(1)}
${elCount + 1}
${fid ? `in frame ${fid}` : ''}
${el.isPlaying ? '' : '(not playing)'}
`
if (!el.isPlaying)
node.querySelector('.element-label').classList.add('element-not-playing')
const gain = node.querySelector('.element-gain')
const gainNumberInput = node.querySelector('.element-gain-num')
gain.value = settings.gain || 1
gain.parentElement.querySelector('.element-gain-num').value = '' + gain.value
gain.addEventListener('input', function () {
// We used a function expression thus gain === this
applySettings(fid, elid, { gain: this.value })
this.parentElement.querySelector('.element-gain-num').value = '' + this.value
})
gainNumberInput.addEventListener('input', function () {
if (+this.value > +this.getAttribute('max'))
this.value = this.getAttribute('max')
if (+this.value < +this.getAttribute('min'))
this.value = this.getAttribute('min')
applySettings(fid, elid, { gain: this.value })
this.parentElement.querySelector('.element-gain').value = '' + this.value
})
const pan = node.querySelector('.element-pan')
const panNumberInput = node.querySelector('.element-pan-num')
pan.value = settings.pan || 0
pan.parentElement.querySelector('.element-pan-num').value = '' + pan.value
pan.addEventListener('input', function () {
applySettings(fid, elid, { pan: this.value })
this.parentElement.querySelector('.element-pan-num').value = '' + this.value
})
panNumberInput.addEventListener('input', function () {
if (+this.value > +this.getAttribute('max'))
this.value = this.getAttribute('max')
if (+this.value < +this.getAttribute('min'))
this.value = this.getAttribute('min')
applySettings(fid, elid, { pan: this.value })
this.parentElement.querySelector('.element-pan').value = '' + this.value
})
const mono = node.querySelector('.element-mono')
mono.checked = settings.mono || false
mono.addEventListener('change', _ => {
applySettings(fid, elid, { mono: mono.checked })
})
const flip = node.querySelector('.element-flip')
flip.checked = settings.flip || false
flip.addEventListener('change', _ => {
applySettings(fid, elid, { flip: flip.checked })
})
node.querySelector('.element-reset').onclick = function () {
gain.value = 1
gain.parentElement.querySelector('.element-gain-num').value = '' + gain.value
pan.value = 0
pan.parentElement.querySelector('.element-pan-num').value = '' + pan.value
mono.checked = false
flip.checked = false
applySettings(fid, elid, { gain: 1, pan: 0, mono: false, flip: false })
}
elementsList.appendChild(node)
elCount += 1
}
}
if (elCount == 0) {
allElements.innerHTML = 'No audio/video found in the current tab. Note that some websites do not work because of cross-domain security restrictions.'
indivElements.remove()
} else {
const node = document.createElement('div')
node.appendChild(document.importNode(elementsTpl.content, true))
node.querySelector('.element-label').textContent = `All media on the page`
const gain = node.querySelector('.element-gain')
const gainNumberInput = node.querySelector('.element-gain-num')
gain.value = 1
gainNumberInput.value = '' + gain.value
function applyGain (value) {
for (const [fid, els] of frameMap) {
for (const [elid, el] of els) {
applySettings(fid, elid, { gain: value })
const egain = document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-gain`)
egain.value = value
egain.parentElement.querySelector('.element-gain-num').value = '' + value
}
}
gain.value = value
gainNumberInput.value = '' + value
}
gain.addEventListener('input', _ => applyGain(gain.value))
gainNumberInput.addEventListener('input', function () {
if (+this.value > +this.getAttribute('max'))
this.value = this.getAttribute('max')
if (+this.value < +this.getAttribute('min'))
this.value = this.getAttribute('min')
applyGain(+this.value)
})
const pan = node.querySelector('.element-pan')
const panNumberInput = node.querySelector('.element-pan-num')
pan.value = 0
panNumberInput.value = '' + pan.value
function applyPan (value) {
for (const [fid, els] of frameMap) {
for (const [elid, el] of els) {
applySettings(fid, elid, { pan: value })
const epan = document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-pan`)
epan.value = value
epan.parentElement.querySelector('.element-pan-num').value = '' + value
}
}
pan.value = value
panNumberInput.value = '' + value
}
pan.addEventListener('input', _ => applyPan(pan.value))
panNumberInput.addEventListener('input', function () {
if (+this.value > +this.getAttribute('max'))
this.value = this.getAttribute('max')
if (+this.value < +this.getAttribute('min'))
this.value = this.getAttribute('min')
applyPan(+this.value)
})
const mono = node.querySelector('.element-mono')
mono.checked = false
mono.addEventListener('change', _ => {
for (const [fid, els] of frameMap) {
for (const [elid, el] of els) {
applySettings(fid, elid, { mono: mono.checked })
const emono = document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-mono`)
emono.checked = mono.checked
}
}
})
const flip = node.querySelector('.element-flip')
flip.checked = false
flip.addEventListener('change', _ => {
for (const [fid, els] of frameMap) {
for (const [elid, el] of els) {
applySettings(fid, elid, { flip: flip.checked })
const eflip = document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-flip`)
eflip.checked = flip.checked
}
}
})
node.querySelector('.element-reset').onclick = function () {
gain.value = 1
gain.parentElement.querySelector('.element-gain-num').value = '' + gain.value
pan.value = 0
pan.parentElement.querySelector('.element-pan-num').value = '' + pan.value
mono.checked = false
flip.checked = false
for (const [fid, els] of frameMap) {
for (const [elid, el] of els) {
const egain = document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-gain`)
egain.value = 1
egain.parentElement.querySelector('.element-gain-num').value = '' + egain.value
const epan = document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-pan`)
epan.value = 0
epan.parentElement.querySelector('.element-pan-num').value = '' + epan.value
document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-mono`).checked = false
document.querySelector(`[data-fid="${fid}"][data-elid="${elid}"] .element-flip`).checked = false
applySettings(fid, elid, { gain: 1, pan: 0, mono: false, flip: false })
}
}
}
allElements.appendChild(node)
}
})