From 64290b9dd1bfd7d80157f3da852d10b0fa330db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B2=A9=E5=B2=A9?= Date: Wed, 11 Feb 2026 14:56:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(M3):=20=E8=AF=8D=E5=85=B8=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20fetch=20=E6=9B=BF=E4=BB=A3=20messaging?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=20'Receiving=20end=20does=20not=20e?= =?UTF-8?q?xist'=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background/index.js | 30 ------------------------------ src/shared/dictionary/bing.js | 19 +++++++++++++------ src/shared/dictionary/youdao.js | 19 +++++++++++++------ 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/src/background/index.js b/src/background/index.js index 6aca3d7..60685ea 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -48,34 +48,4 @@ backgroundHandler.register('DICT.GET_LIST', async () => { return { dictionaries }; }); -// 注册 HTTP 请求处理器 -backgroundHandler.register('HTTP.GET', async (payload) => { - const { url, options = {} } = payload; - - console.log('[Background] HTTP.GET:', url); - - try { - const response = await fetch(url, { - method: 'GET', - headers: { - 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', - 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.0', - ...options.headers - }, - ...options - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const text = await response.text(); - return { text, status: response.status }; - } catch (error) { - console.error('[Background] HTTP.GET failed:', error); - throw new Error(`请求失败: ${error.message}`); - } -}); - console.log('[SaladDict] Message handlers registered'); diff --git a/src/shared/dictionary/bing.js b/src/shared/dictionary/bing.js index fd35f45..3f8f18d 100644 --- a/src/shared/dictionary/bing.js +++ b/src/shared/dictionary/bing.js @@ -4,7 +4,6 @@ */ import { DictionaryBase, createResult, createMeaning, createExample } from './base.js'; -import { messaging } from '../messaging.js'; /** * 必应词典实现 @@ -33,15 +32,23 @@ export class BingDictionary extends DictionaryBase { const url = `https://cn.bing.com/dict/search?q=${encodeURIComponent(trimmedWord)}`; try { - // 通过 Background 发起 HTTP 请求 - const response = await messaging.sendToBackground('HTTP.GET', { url }, 10000); + // 在 Background 中直接使用 fetch + const response = await fetch(url, { + method: 'GET', + headers: { + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8' + } + }); - if (!response?.text) { - throw new Error('获取词典数据失败'); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); } + const html = await response.text(); + // 解析 HTML 提取数据 - return this._parseHtml(response.text, trimmedWord, url); + return this._parseHtml(html, trimmedWord, url); } catch (error) { console.error('[BingDictionary] Search failed:', error); diff --git a/src/shared/dictionary/youdao.js b/src/shared/dictionary/youdao.js index 00c77da..6170d62 100644 --- a/src/shared/dictionary/youdao.js +++ b/src/shared/dictionary/youdao.js @@ -4,7 +4,6 @@ */ import { DictionaryBase, createResult, createMeaning, createExample } from './base.js'; -import { messaging } from '../messaging.js'; /** * 有道词典实现 @@ -33,15 +32,23 @@ export class YoudaoDictionary extends DictionaryBase { const url = `https://dict.youdao.com/result?word=${encodeURIComponent(trimmedWord)}&lang=en`; try { - // 通过 Background 发起 HTTP 请求 - const response = await messaging.sendToBackground('HTTP.GET', { url }, 10000); + // 在 Background 中直接使用 fetch + const response = await fetch(url, { + method: 'GET', + headers: { + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8' + } + }); - if (!response?.text) { - throw new Error('获取词典数据失败'); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); } + const html = await response.text(); + // 解析 HTML 提取数据 - return this._parseHtml(response.text, trimmedWord, url); + return this._parseHtml(html, trimmedWord, url); } catch (error) { console.error('[YoudaoDictionary] Search failed:', error);