-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
114 lines (103 loc) · 2.51 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
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
let synth = window.speechSynthesis;
let lastSubtitle = 'start';
let run = false;
let all = '';
let utterance = new SpeechSynthesisUtterance();
function speak(text) {
if (synth.speaking) {
synth.cancel();
}
return new Promise((resolve, reject) => {
if (!text)
text = '';
utterance.text = text;
synth.speak(utterance);
utterance.onend = resolve;
utterance.onerror = reject;
});
}
async function checkSubtitles() {
let subtitles = document.querySelectorAll('.ytp-caption-segment');
if (subtitles.length > 0) {
let lastElement = subtitles[0];
let text = lastElement.textContent;
// console.log(text.search(lastSubtitle), text, lastSubtitle);
if (text.search(lastSubtitle) == -1) {
all += lastSubtitle;
}
lastSubtitle = text;
}
return true;
}
async function speakLoop() {
while (run) {
if (all != '') {
const curent = all;
all = '';
await speak(curent);
}
else {
await new Promise(resolve => setTimeout(resolve, 100));
}
}
// return true;
}
async function readLoop() {
while (run) {
await checkSubtitles();
await new Promise(resolve => setTimeout(resolve, 100));
}
return true;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'start') {
const voices = synth.getVoices();
const selectedVoice = voices.find((voice) => voice.voiceURI === request.voiceURI);
if (selectedVoice) {
utterance.voice = selectedVoice;
utterance.lang = selectedVoice.lang;
}
// utterance.rate = 1;
toggleMute();
toggleSubtitles(selectedVoice.lang);
run = true;
readLoop();
speakLoop();
sendResponse({ result: 'Started' });
} else if (request.action === 'stop') {
run = false;
synth.cancel();
sendResponse({ result: 'Stopped' });
}
return true;
});
// Mute the video
function toggleMute() {
const muteButton = document.querySelector('.ytp-mute-button');
if (muteButton) {
// if (muteButton.ariaPressed != 'true') {
// muteButton.click();
// }
}
}
// Activate the subtitles
function toggleSubtitles(lang) {
const captionsButton = document.querySelector('.ytp-subtitles-button');
if (captionsButton) {
if (captionsButton.ariaPressed != 'true') {
captionsButton.click();
}
}
setTimeout(() => {
const trackMenu = document.querySelector('.ytp-caption-window.ytp-caption-window-rollup');
if (trackMenu) {
const tracks = trackMenu.querySelectorAll('.ytp-caption-window-text');
for (const track of tracks) {
if (track.textContent.includes('Français')) {
track.click();
break;
}
}
}
}, 500);
}