fix(M3): 词典直接使用 fetch 替代 messaging,修复 'Receiving end does not exist' 错误
This commit is contained in:
parent
83dc377efd
commit
64290b9dd1
@ -48,34 +48,4 @@ backgroundHandler.register('DICT.GET_LIST', async () => {
|
|||||||
return { dictionaries };
|
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');
|
console.log('[SaladDict] Message handlers registered');
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { DictionaryBase, createResult, createMeaning, createExample } from './base.js';
|
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)}`;
|
const url = `https://cn.bing.com/dict/search?q=${encodeURIComponent(trimmedWord)}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 通过 Background 发起 HTTP 请求
|
// 在 Background 中直接使用 fetch
|
||||||
const response = await messaging.sendToBackground('HTTP.GET', { url }, 10000);
|
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) {
|
if (!response.ok) {
|
||||||
throw new Error('获取词典数据失败');
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const html = await response.text();
|
||||||
|
|
||||||
// 解析 HTML 提取数据
|
// 解析 HTML 提取数据
|
||||||
return this._parseHtml(response.text, trimmedWord, url);
|
return this._parseHtml(html, trimmedWord, url);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[BingDictionary] Search failed:', error);
|
console.error('[BingDictionary] Search failed:', error);
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { DictionaryBase, createResult, createMeaning, createExample } from './base.js';
|
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`;
|
const url = `https://dict.youdao.com/result?word=${encodeURIComponent(trimmedWord)}&lang=en`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 通过 Background 发起 HTTP 请求
|
// 在 Background 中直接使用 fetch
|
||||||
const response = await messaging.sendToBackground('HTTP.GET', { url }, 10000);
|
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) {
|
if (!response.ok) {
|
||||||
throw new Error('获取词典数据失败');
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const html = await response.text();
|
||||||
|
|
||||||
// 解析 HTML 提取数据
|
// 解析 HTML 提取数据
|
||||||
return this._parseHtml(response.text, trimmedWord, url);
|
return this._parseHtml(html, trimmedWord, url);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[YoudaoDictionary] Search failed:', error);
|
console.error('[YoudaoDictionary] Search failed:', error);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user