-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
111 lines (94 loc) · 3.46 KB
/
offscreen.js
File metadata and controls
111 lines (94 loc) · 3.46 KB
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
// offscreen.js (with better error handling & word tracking)
let currentUtterance = null;
let isPaused = false;
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.target !== 'offscreen') {
return false; // Not for us
}
console.log("Offscreen document received message:", message.type);
switch (message.type) {
case 'SPEAK_TEXT':
speakText(message.text, message.rate, message.voiceIndex).then(() => {
sendResponse({ success: true });
}).catch((error) => {
sendResponse({ success: false, error: error.message });
});
return true; // Keep message channel open for async response
case 'STOP_READING':
resetSpeech();
sendResponse({ success: true });
return true;
case 'TOGGLE_PLAYBACK':
const toggleResult = togglePlayback();
sendResponse({ success: true, isPaused: toggleResult.isPaused });
return true;
}
return false;
});
async function speakText(text, rate = 1.0) {
return new Promise((resolve, reject) => {
try {
// Stop any ongoing speech first
resetSpeech();
// Create and speak the new utterance
currentUtterance = new SpeechSynthesisUtterance(text);
currentUtterance.rate = rate;
currentUtterance.pitch = 1.0;
currentUtterance.volume = 1.0;
// Event listeners
currentUtterance.onstart = () => {
console.log('Speech started');
};
currentUtterance.onend = () => {
console.log('Speech ended');
resetSpeech();
resolve();
};
currentUtterance.onerror = (event) => {
if (event.error === 'interrupted') {
console.log("Speech was intentionally interrupted");
resolve(); // Don't treat interruption as error
} else {
console.error("Speech error:", event.error);
resetSpeech();
reject(new Error(`Speech error: ${event.error}`));
}
};
// Start speaking
speechSynthesis.speak(currentUtterance);
// Resolve immediately since we're starting the speech
// The onend/onerror handlers will handle completion
setTimeout(() => resolve(), 100);
} catch (error) {
console.error("Error in speakText:", error);
reject(error);
}
});
}
function resetSpeech() {
isPaused = false;
currentUtterance = null;
// Additional cleanup to ensure speech is really stopped
try {
speechSynthesis.cancel();
} catch (error) {
console.error("Error in final cleanup:", error);
}
}
function togglePlayback() {
if (speechSynthesis.speaking) {
if (isPaused) {
// Resume
speechSynthesis.resume();
isPaused = false;
} else {
// Pause
speechSynthesis.pause();
isPaused = true;
}
}
return { isPaused: isPaused };
}
// Let the background know we're ready when the offscreen document loads
console.log("Offscreen document loaded and ready");