-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
53 lines (43 loc) · 1.15 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
let prevText = "";
let isPlaying = false;
let playbackInterval;
function readAloud(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = "ja-JP";
window.speechSynthesis.speak(utterance);
}
function processParagraphs() {
const paragraphs = document.querySelectorAll("p");
let allText = "";
paragraphs.forEach((paragraph) => {
allText += paragraph.textContent;
});
const newText = allText.substring(prevText.length);
if (newText) {
readAloud(newText);
prevText = allText;
}
}
function togglePlayback(play) {
isPlaying = play;
if (isPlaying) {
playbackInterval = setInterval(processParagraphs, 1000);
} else {
clearInterval(playbackInterval);
window.speechSynthesis.cancel();
}
}
function isChatOpenAIUrl() {
return window.location.href.startsWith("https://chat.openai.com");
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (!isChatOpenAIUrl()) {
return;
}
if (request.type === "TOGGLE_PLAYBACK") {
togglePlayback(!isPlaying);
sendResponse(isPlaying);
} else if (request.type === "GET_PLAYBACK_STATE") {
sendResponse(isPlaying);
}
});