fix(M3): 词典直接使用 fetch 替代 messaging,修复 'Receiving end does not exist' 错误

This commit is contained in:
李岩岩 2026-02-11 14:56:31 +08:00
parent 83dc377efd
commit 64290b9dd1
3 changed files with 26 additions and 42 deletions

View File

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

View File

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

View File

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