-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
194 lines (172 loc) · 5.93 KB
/
background.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
console.log("Background script loaded.");
const injectedTabs = new Set();
chrome.commands.onCommand.addListener(function(command) {
if (command === "read-selected-text") {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: "readSelectedText" });
}
});
}
});
function readSelectedText(tab) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: "getSelectedText" }, function(response) {
if (response && response.selectedText) {
const text = response.selectedText;
sendTextToBackend(text, requestSequence++);
}
});
}
});
}
let requestSequence = 0;
const audioQueue = [];
function sendTextToBackend(text, sequence) {
getAudioFromBackend(text, function(audioData) {
handleAudioResponse(audioData, sequence);
});
}
function getAudioFromBackend(text, callback) {
setTimeout(function() {
const audioData = `audio_for_${text}`;
callback(audioData);
}, Math.random() * 3000);
}
function handleAudioResponse(audioData, sequence) {
audioQueue.push({ sequence: sequence, audio: audioData });
audioQueue.sort((a, b) => a.sequence - b.sequence);
playNextAudio();
}
function playNextAudio() {
if (audioQueue.length > 0 && audioQueue[0].sequence === requestSequence - audioQueue.length) {
const audioItem = audioQueue.shift();
playAudio(audioItem.audio);
}
}
function playAudio(audioData) {
console.log(`Playing audio: ${audioData}`);
setTimeout(playNextAudio, 1000);
}
chrome.commands.onCommand.addListener(function(command) {
if (command === "read-selected-text") {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (tabs[0]) {
readSelectedText(tabs[0]);
}
});
}
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "readSelectedText") {
readSelectedText(tab);
}
});
function setSelectedCharacter(characterIndex, emotionName) {
chrome.storage.local.set({ selectedCharacterIndex: characterIndex, selectedEmotion: emotionName }, function() {
console.log(`Selected character index: ${characterIndex}, Emotion: ${emotionName}`);
updateContextMenuTitle(characterIndex, emotionName);
});
}
function updateContextMenus() {
chrome.storage.local.get('characters', function(items) {
const characters = items && items.characters ? items.characters : [];
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
id: "readSelectedText",
title: "Read Selected Text",
contexts: ["selection"]
});
chrome.contextMenus.create({
id: "stopAudio",
title: "Stop Audio",
contexts: ["selection"]
});
characters.forEach((character, index) => {
chrome.contextMenus.create({
id: `selectCharacter-${character.name}`,
title: `Select Character: ${character.name}`,
contexts: ["selection"]
});
if (character.emotions) {
character.emotions.forEach(emotion => {
chrome.contextMenus.create({
id: `selectEmotion-${character.name}-${emotion.name.replace(/ /g, '-')}`,
title: emotion.name,
parentId: `selectCharacter-${character.name}`,
contexts: ["selection"]
});
});
}
});
});
});
}
chrome.storage.onChanged.addListener(function(changes, areaName) {
if (areaName === 'local' && changes.characters) {
updateContextMenus();
}
});
// Initial loading of context menus
updateContextMenus();
chrome.contextMenus.onClicked.addListener((info, tab) => {
console.log("Context menu item clicked:", info);
if (info.menuItemId === "readSelectedText") {
if (!injectedTabs.has(tab.id)) {
chrome.tabs.executeScript(tab.id, {
file: "content.js"
}, () => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
injectedTabs.add(tab.id);
}
});
}
chrome.tabs.sendMessage(tab.id, { action: "readSelectedText" }, () => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
} else if (info.menuItemId === "stopAudio") {
chrome.tabs.sendMessage(tab.id, { action: "stopAudio" }, () => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
} else if (info.menuItemId.startsWith("selectEmotion-")) {
const parts = info.menuItemId.split("-");
if (parts.length >= 3 && parts[0] === "selectEmotion") {
const characterIndex = parts[1];
const emotionName = parts.slice(2).join(" ");
setSelectedCharacter(characterIndex, emotionName);
}
}
});
function updateContextMenuTitle(characterIndex, emotionName) {
const title = `Read Selected Text - ${characterIndex}/${emotionName}`;
chrome.contextMenus.update("readSelectedText", { title: title });
}
// Function to set default selections on startup
function setDefaultSelections() {
chrome.storage.local.get(['characters', 'selectedCharacterIndex', 'selectedEmotion'], function(items) {
const characters = items.characters || [];
const selectedCharacterIndex = items.selectedCharacterIndex;
const selectedEmotion = items.selectedEmotion;
if (characters.length === 0) {
console.log("No characters defined.");
return;
}
if (!selectedCharacterIndex || !selectedEmotion) {
const firstChar = characters[0];
const firstEmotion = firstChar.emotions ? firstChar.emotions[0].name : 'default';
setSelectedCharacter(firstChar.name, firstEmotion);
updateContextMenuTitle(firstChar.name, firstEmotion);
return;
}
updateContextMenuTitle(selectedCharacterIndex, selectedEmotion);
});
}
// Call setDefaultSelections when background script loads
setDefaultSelections();