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);