-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
71 lines (62 loc) · 2.04 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
65
66
67
68
69
70
71
let utterance = new SpeechSynthesisUtterance();
let tabId;
let playButton = document.getElementById("playButton");
let stopButton = document.getElementById("stopButton");
let rateButton = document.getElementById("rate");
playButton.addEventListener("click", playTextToSpeech);
stopButton.addEventListener("click", stopTextToSpeech);
rateButton.addEventListener("input", onChangeRate);
document.querySelector("#volume").addEventListener("input", () => {
const volume = document.querySelector("#volume").value;
let utterance = new SpeechSynthesisUtterance();
utterance.volume = volume;
document.querySelector("#volume-label").innerHTML = volume;
});
function onChangeRate() {
const rate = document.querySelector("#rate").value;
let utterance = new SpeechSynthesisUtterance();
utterance.rate = rate;
document.querySelector("#rate-label").innerHTML = rate;
}
function playTextToSpeech() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
tabId = tabs[0].id;
chrome.scripting.executeScript({
target: { tabId: tabId },
function: playArticleContent,
});
});
}
function stopTextToSpeech() {
if (tabId) {
chrome.scripting.executeScript({
target: { tabId: tabId },
function: stopArticleContent,
});
}
}
function playArticleContent() {
let articleElement = document.querySelector("article");
console.log({articleElement});
if (articleElement) {
let utterance = new SpeechSynthesisUtterance();
let text = articleElement.textContent;
utterance.text = text;
speechSynthesis.speak(utterance);
if(window.speechSynthesis.speaking){
window.speechSynthesis.pause();
}
if(window.speechSynthesis.paused){
window.speechSynthesis.resume();
}
// code to pause if utterance has been started
// document.querySelector("#pause").addEventListener("click", () => {
// window.speechSynthesis.pause();
// });
// window.speechSynthesis.resume();
}
}
function stopArticleContent() {
console.log("stop");
window.speechSynthesis.cancel();
}