catch global error and display on top of page

This commit is contained in:
Gabe Yuan
2023-08-27 16:41:14 +08:00
parent c4980d9eb7
commit e1d74aae6a
2 changed files with 42 additions and 16 deletions

View File

@@ -12,7 +12,7 @@ import { isIframe } from "./libs/iframe";
/** /**
* 入口函数 * 入口函数
*/ */
(async () => { const init = async () => {
const href = isIframe ? document.referrer : document.location.href; const href = isIframe ? document.referrer : document.location.href;
const setting = await getSetting(); const setting = await getSetting();
const rules = await getRules(); const rules = await getRules();
@@ -38,4 +38,15 @@ import { isIframe } from "./libs/iframe";
} }
return { data: translator.rule }; return { data: translator.rule };
}); });
};
(async () => {
try {
await init();
} catch (err) {
const $err = document.createElement("div");
$err.innerText = `KISS-Translator Error: ${err.message}`;
$err.style.cssText = "background:red; color:#fff; z-index:10000;";
document.body.prepend($err);
}
})(); })();

View File

@@ -13,7 +13,7 @@ import { isIframe } from "./libs/iframe";
/** /**
* 入口函数 * 入口函数
*/ */
(async () => { const init = async () => {
// 设置页面 // 设置页面
if ( if (
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE_DEV) || document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE_DEV) ||
@@ -74,6 +74,7 @@ import { isIframe } from "./libs/iframe";
// 注册菜单 // 注册菜单
if (isGm) { if (isGm) {
try {
GM.registerMenuCommand( GM.registerMenuCommand(
"Toggle Translate", "Toggle Translate",
(event) => { (event) => {
@@ -88,8 +89,22 @@ import { isIframe } from "./libs/iframe";
}, },
"C" "C"
); );
} catch (err) {
console.log("[registerMenuCommand]", err);
}
} }
// 同步订阅规则 // 同步订阅规则
trySyncAllSubRules(setting); trySyncAllSubRules(setting);
};
(async () => {
try {
await init();
} catch (err) {
const $err = document.createElement("div");
$err.innerText = `KISS-Translator Error: ${err.message}`;
$err.style.cssText = "background:red; color:#fff; z-index:10000;";
document.body.prepend($err);
}
})(); })();