fix: change runtime.onMessage to async fn
This commit is contained in:
@@ -115,91 +115,42 @@ browser.runtime.onStartup.addListener(async () => {
|
|||||||
/**
|
/**
|
||||||
* 监听消息
|
* 监听消息
|
||||||
*/
|
*/
|
||||||
browser.runtime.onMessage.addListener(
|
browser.runtime.onMessage.addListener(async ({ action, args }) => {
|
||||||
({ action, args }, sender, sendResponse) => {
|
switch (action) {
|
||||||
switch (action) {
|
case MSG_FETCH:
|
||||||
case MSG_FETCH:
|
const { input, opts } = args;
|
||||||
const { input, opts } = args;
|
return await fetchData(input, opts);
|
||||||
fetchData(input, opts)
|
case MSG_FETCH_LIMIT:
|
||||||
.then((data) => {
|
const { interval, limit } = args;
|
||||||
sendResponse({ data });
|
return fetchPool.update(interval, limit);
|
||||||
})
|
case MSG_FETCH_CLEAR:
|
||||||
.catch((error) => {
|
return fetchPool.clear();
|
||||||
sendResponse({ error: error.message, cause: error.cause });
|
case MSG_OPEN_OPTIONS:
|
||||||
});
|
return await browser.runtime.openOptionsPage();
|
||||||
break;
|
case MSG_SAVE_RULE:
|
||||||
case MSG_FETCH_LIMIT:
|
return await saveRule(args);
|
||||||
const { interval, limit } = args;
|
case MSG_INJECT_JS:
|
||||||
fetchPool.update(interval, limit);
|
return await browser.scripting.executeScript({
|
||||||
sendResponse({ data: "ok" });
|
target: { tabId: await getCurTabId(), allFrames: true },
|
||||||
break;
|
func: injectInlineJs,
|
||||||
case MSG_FETCH_CLEAR:
|
args: [args],
|
||||||
fetchPool.clear();
|
world: "MAIN",
|
||||||
sendResponse({ data: "ok" });
|
});
|
||||||
break;
|
case MSG_INJECT_CSS:
|
||||||
case MSG_OPEN_OPTIONS:
|
return await browser.scripting.executeScript({
|
||||||
browser.runtime.openOptionsPage();
|
target: { tabId: await getCurTabId(), allFrames: true },
|
||||||
sendResponse({ data: "ok" });
|
func: injectInternalCss,
|
||||||
break;
|
args: [args],
|
||||||
case MSG_SAVE_RULE:
|
world: "MAIN",
|
||||||
saveRule(args);
|
});
|
||||||
sendResponse({ data: "ok" });
|
case MSG_CONTEXT_MENUS:
|
||||||
break;
|
return await addContextMenus(args.contextMenuType);
|
||||||
case MSG_INJECT_JS:
|
case MSG_COMMAND_SHORTCUTS:
|
||||||
getCurTabId()
|
return await browser.commands.getAll();
|
||||||
.then((tabId) =>
|
default:
|
||||||
browser.scripting.executeScript({
|
throw new Error(`message action is unavailable: ${action}`);
|
||||||
target: { tabId: tabId, allFrames: true },
|
|
||||||
func: injectInlineJs,
|
|
||||||
args: [args],
|
|
||||||
world: "MAIN",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.then(() => {
|
|
||||||
sendResponse({ data: "ok" });
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
sendResponse({ error: error.message });
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case MSG_INJECT_CSS:
|
|
||||||
getCurTabId()
|
|
||||||
.then((tabId) =>
|
|
||||||
browser.scripting.executeScript({
|
|
||||||
target: { tabId: tabId, allFrames: true },
|
|
||||||
func: injectInternalCss,
|
|
||||||
args: [args],
|
|
||||||
world: "MAIN",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.then(() => {
|
|
||||||
sendResponse({ data: "ok" });
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
sendResponse({ error: error.message });
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case MSG_CONTEXT_MENUS:
|
|
||||||
const { contextMenuType } = args;
|
|
||||||
addContextMenus(contextMenuType);
|
|
||||||
sendResponse({ data: "ok" });
|
|
||||||
break;
|
|
||||||
case MSG_COMMAND_SHORTCUTS:
|
|
||||||
browser.commands
|
|
||||||
.getAll()
|
|
||||||
.then((commands) => {
|
|
||||||
sendResponse({ data: commands });
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
sendResponse({ error: error.message });
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
sendResponse({ error: `message action is unavailable: ${action}` });
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
);
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听快捷键
|
* 监听快捷键
|
||||||
|
|||||||
@@ -138,13 +138,14 @@ export const fetchData = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!res?.ok) {
|
if (!res?.ok) {
|
||||||
const cause = {
|
const msg = {
|
||||||
|
url: input,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
};
|
};
|
||||||
if (res.headers.get("Content-Type")?.includes("json")) {
|
if (res.headers.get("Content-Type")?.includes("json")) {
|
||||||
cause.body = await res.json();
|
msg.response = await res.json();
|
||||||
}
|
}
|
||||||
throw new Error(`response: [${res.status}] ${res.statusText}`, { cause });
|
throw new Error(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 插入缓存
|
// 插入缓存
|
||||||
@@ -178,11 +179,7 @@ export const fetchPolyfill = async (input, opts) => {
|
|||||||
|
|
||||||
// 插件
|
// 插件
|
||||||
if (isExt && !isBg()) {
|
if (isExt && !isBg()) {
|
||||||
const res = await sendBgMsg(MSG_FETCH, { input, opts });
|
return await sendBgMsg(MSG_FETCH, { input, opts });
|
||||||
if (res.error) {
|
|
||||||
throw new Error(res.error, { cause: res.cause });
|
|
||||||
}
|
|
||||||
return res.data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 油猴/网页/BackgroundPage
|
// 油猴/网页/BackgroundPage
|
||||||
@@ -196,10 +193,7 @@ export const fetchPolyfill = async (input, opts) => {
|
|||||||
*/
|
*/
|
||||||
export const updateFetchPool = async (interval, limit) => {
|
export const updateFetchPool = async (interval, limit) => {
|
||||||
if (isExt) {
|
if (isExt) {
|
||||||
const res = await sendBgMsg(MSG_FETCH_LIMIT, { interval, limit });
|
await sendBgMsg(MSG_FETCH_LIMIT, { interval, limit });
|
||||||
if (res.error) {
|
|
||||||
throw new Error(res.error);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
fetchPool.update(interval, limit);
|
fetchPool.update(interval, limit);
|
||||||
}
|
}
|
||||||
@@ -210,10 +204,7 @@ export const updateFetchPool = async (interval, limit) => {
|
|||||||
*/
|
*/
|
||||||
export const clearFetchPool = async () => {
|
export const clearFetchPool = async () => {
|
||||||
if (isExt) {
|
if (isExt) {
|
||||||
const res = await sendBgMsg(MSG_FETCH_CLEAR);
|
await sendBgMsg(MSG_FETCH_CLEAR);
|
||||||
if (res.error) {
|
|
||||||
throw new Error(res.error);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
fetchPool.clear();
|
fetchPool.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { loadingSvg } from "../../libs/svg";
|
|||||||
|
|
||||||
export default function LoadingIcon() {
|
export default function LoadingIcon() {
|
||||||
return (
|
return (
|
||||||
<div
|
<span
|
||||||
style={{
|
style={{
|
||||||
display: "inline-block",
|
display: "inline-block",
|
||||||
width: "1.2em",
|
width: "1.2em",
|
||||||
|
|||||||
@@ -50,16 +50,22 @@ function TestButton({ translator, api }) {
|
|||||||
alert.success(i18n("test_success"));
|
alert.success(i18n("test_success"));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// alert.error(`${i18n("test_failed")}: ${err.message}`);
|
// alert.error(`${i18n("test_failed")}: ${err.message}`);
|
||||||
|
let msg = err.message;
|
||||||
|
try {
|
||||||
|
msg = JSON.stringify(JSON.parse(err.message), null, 2);
|
||||||
|
} catch (err) {
|
||||||
|
// skip
|
||||||
|
}
|
||||||
alert.error(
|
alert.error(
|
||||||
<>
|
<>
|
||||||
<div>{`${i18n("test_failed")}: ${err.message}`}</div>
|
<div>{i18n("test_failed")}</div>
|
||||||
<pre
|
<pre
|
||||||
style={{
|
style={{
|
||||||
maxWidth: 400,
|
maxWidth: 400,
|
||||||
overflow: "auto",
|
overflow: "auto",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{JSON.stringify(err.cause || {}, null, 2)}
|
{msg}
|
||||||
</pre>
|
</pre>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@@ -124,7 +130,9 @@ function ApiFields({ translator }) {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
multiline={mulkeysTranslators.includes(translator)}
|
multiline={mulkeysTranslators.includes(translator)}
|
||||||
helperText={
|
helperText={
|
||||||
mulkeysTranslators.includes(translator) ? i18n("mulkeys_help") : ""
|
mulkeysTranslators.includes(translator)
|
||||||
|
? i18n("mulkeys_help")
|
||||||
|
: ""
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -119,11 +119,9 @@ export default function Popup({ setShowPopup, translator: tran }) {
|
|||||||
const commands = {};
|
const commands = {};
|
||||||
if (isExt) {
|
if (isExt) {
|
||||||
const res = await sendBgMsg(MSG_COMMAND_SHORTCUTS);
|
const res = await sendBgMsg(MSG_COMMAND_SHORTCUTS);
|
||||||
if (!res.error) {
|
res.forEach(({ name, shortcut }) => {
|
||||||
res.data.forEach(({ name, shortcut }) => {
|
commands[name] = shortcut;
|
||||||
commands[name] = shortcut;
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const shortcuts = tran.setting.shortcuts;
|
const shortcuts = tran.setting.shortcuts;
|
||||||
if (shortcuts) {
|
if (shortcuts) {
|
||||||
|
|||||||
Reference in New Issue
Block a user