-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
35 lines (31 loc) · 1.28 KB
/
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
34
35
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
let elements = Array.from(document.querySelectorAll('audio, video')).filter(media => !media.paused)
let promises = []
elements.forEach(elem => {
let stream = createStream(elem)
let audioStream = new MediaStream(stream.getAudioTracks())
let dataPromise = recordStream(audioStream, Number(request.time)).then(data => Array.from(data))
promises.push(dataPromise)
})
Promise.allSettled(promises).then(arr => sendResponse(arr.map(r => r.value)))
return true
})
function recordStream(stream, ms){
return new Promise(resolve => {
let data = []
let recorder = new MediaRecorder(stream)
recorder.ondataavailable = e => data.push(e.data)
recorder.onstop = _ => data[0].arrayBuffer().then(ab => resolve(new Uint8Array(ab)))
recorder.start()
setTimeout(() => recorder.stop(), ms)
})
}
function createStream(elem){
let stream = elem.captureStream ? elem.captureStream() : elem.mozCaptureStream()
if (!elem.audioCtx && !elem.captureStream){
elem.audioCtx = new AudioContext()
let source = elem.audioCtx.createMediaElementSource(elem)
source.connect(elem.audioCtx.destination)
}
return stream
}