forked from robertontiu/chrome-extension-volumest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
33 lines (27 loc) · 928 Bytes
/
content.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
// Stores the volume control function for the video found in the current tab
let volumeControlFn
// Listen for click on the volume icon
chrome.runtime.onMessage.addListener(
function(request) {
if(request.message === "set_volume_level" && typeof request.value === 'number') {
volumeControlFn = volumeControlFn || createVolumeControlFunction()
volumeControlFn(request.value)
}
}
);
// Creates the volume toggle function
function createVolumeControlFunction() {
const videoElement = document.querySelector("video")
if (!videoElement) {
return undefined
}
const audioCtx = new AudioContext()
const source = audioCtx.createMediaElementSource(videoElement)
const node = audioCtx.createGain()
node.gain.value = 1
source.connect(node)
node.connect(audioCtx.destination)
return (multiplier) => {
node.gain.value = multiplier
}
}