-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
64 lines (56 loc) · 1.67 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
let read = false;
function handleMessageError(error) {
if (error) {
console.log(`Error: ${error}`);
}
}
document.getElementById('status').addEventListener('click', () => {
const voiceURI = document.getElementById('voice-select').value;
if (read) {
document.getElementById('status').textContent = 'start';
read = false;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: 'stop' }, {}, handleMessageError);
});
displaySubtitles(true);
}
else {
document.getElementById('status').textContent = 'stop';
read = true;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: 'start', voiceURI }, {}, handleMessageError);
});
displaySubtitles(false);
}
});
window.addEventListener('load', () => {
});
// Choice language and voice
function fillVoiceList() {
const select = document.getElementById('voice-select');
const voices = speechSynthesis.getVoices();
if (!select)
return;
voices.forEach((voice) => {
const option = document.createElement('option');
option.textContent = `${voice.name} (${voice.lang})`;
option.value = voice.voiceURI;
select.appendChild(option);
});
}
// Wait for the voices to be loaded
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = fillVoiceList;
}
// Fill the voice options
fillVoiceList();
// Display the subtitles
function displaySubtitles(run) {
const captionsWindow = document.querySelector('.ytp-caption-window');
if (captionsWindow) {
if (run)
captionsWindow.style.opacity = '0 !important';
else
captionsWindow.style.opacity = '1';
}
}