默认关闭语音播放功能
This commit is contained in:
1 parent
c43b5e5ac1
commit
1b8f833789
6 files changed
+176
-112
No files matched your search
+25
-13
@@ -1,23 +1,20 @@
|
||||
// background.js
|
||||
// background/background.js
|
||||
|
||||
// 监听来自 main.js 的消息
|
||||
chrome.runtime.onMessageExternal.addListener((message, sender, sendResponse) => {
|
||||
// 这种方式需要知道 Extension ID,更简单的方法是统一由 content.js 转发
|
||||
});
|
||||
|
||||
// 通用监听(推荐)
|
||||
// 监听来自 content.js 的消息转发
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.type === "AI_TRANSLATE") {
|
||||
// 1. 先从 storage 获取配置
|
||||
chrome.storage.sync.get(['aiConfig'], async (result) => {
|
||||
// 1. 同时获取 AI 配置信息和语音开关状态
|
||||
chrome.storage.sync.get(['aiConfig', 'voiceEnabled'], async (result) => {
|
||||
const config = result.aiConfig;
|
||||
const voiceEnabled = result.voiceEnabled || false; // 默认为关闭
|
||||
|
||||
if (!config || !config.apiKey) {
|
||||
sendResponse({ success: false, error: "未配置 API Key" });
|
||||
sendResponse({ success: false, error: "请先在扩展图标中配置 API Key" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 发起请求
|
||||
// 2. 向 AI 平台发起 fetch 请求
|
||||
const response = await fetch(`${config.apiUrl}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -34,12 +31,27 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.error?.message || "网络请求失败");
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
sendResponse({ success: true, data: data });
|
||||
|
||||
// 3. 将 AI 结果和语音开关状态一并返回给 content.js
|
||||
sendResponse({
|
||||
success: true,
|
||||
data: data,
|
||||
voiceEnabled: voiceEnabled
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error("AI Request Error:", err);
|
||||
sendResponse({ success: false, error: err.message });
|
||||
}
|
||||
});
|
||||
return true; // 保持异步
|
||||
|
||||
// 返回 true 表示我们将异步发送响应
|
||||
return true;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user