-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontent_main.js
54 lines (44 loc) · 1.38 KB
/
content_main.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
function getVideoElements() {
return [].slice.call(document.getElementsByTagName('video'))
}
function getAudioElements() {
return [].slice.call(document.getElementsByTagName('audio'))
}
function summary(sendResponse) {
var videos = getVideoElements()
var audios = getAudioElements()
var playbackRate = null
if (videos.length > 0) {
playbackRate = videos[0].playbackRate
} else if (audios.length > 0) {
playbackRate = audios[0].playbackRate
}
if (playbackRate !== null) {
sendResponse({
status: "success",
playbackRate: playbackRate,
})
}
}
function setElementPlaybackRate(el, newPlaybackRate) {
el.playbackRate = newPlaybackRate
}
function setPlaybackRate(request, sendResponse) {
var videos = getVideoElements()
var audios = getAudioElements()
var newPlaybackRate = request.newPlaybackRate
videos.forEach(function (video) {
setElementPlaybackRate(video, newPlaybackRate)
})
audios.forEach(function (audio) {
setElementPlaybackRate(audio, newPlaybackRate)
})
summary(sendResponse)
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.type === 'prc-get-summary') {
summary(sendResponse)
} else if (request.type === 'prc-set-playback-rate') {
setPlaybackRate(request, sendResponse)
}
})