2023-08-05 20:11:02 +08:00
|
|
|
import React from "react";
|
|
|
|
|
import ReactDOM from "react-dom/client";
|
2023-08-05 23:56:16 +08:00
|
|
|
import Action from "./views/Action";
|
|
|
|
|
import createCache from "@emotion/cache";
|
|
|
|
|
import { CacheProvider } from "@emotion/react";
|
2023-08-16 22:38:58 +08:00
|
|
|
import { getSetting, getRules, matchRule, getFab } from "./libs";
|
2023-08-05 15:32:51 +08:00
|
|
|
import { Translator } from "./libs/translator";
|
2023-08-22 16:27:09 +08:00
|
|
|
import { trySyncAllSubRules } from "./libs/rules";
|
2023-08-23 10:39:01 +08:00
|
|
|
import { isGm } from "./libs/browser";
|
2023-08-26 11:43:00 +08:00
|
|
|
import { MSG_TRANS_TOGGLE, MSG_TRANS_PUTRULE } from "./config";
|
|
|
|
|
import { isIframe } from "./libs/iframe";
|
2023-08-05 15:32:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 入口函数
|
|
|
|
|
*/
|
2023-08-27 16:41:14 +08:00
|
|
|
const init = async () => {
|
2023-08-05 20:11:02 +08:00
|
|
|
// 设置页面
|
2023-08-06 21:12:01 +08:00
|
|
|
if (
|
2023-08-09 18:02:18 +08:00
|
|
|
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE_DEV) ||
|
2023-08-07 11:53:44 +08:00
|
|
|
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE) ||
|
|
|
|
|
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE2)
|
2023-08-06 21:12:01 +08:00
|
|
|
) {
|
2023-08-16 11:38:53 +08:00
|
|
|
unsafeWindow.GM = GM;
|
|
|
|
|
unsafeWindow.APP_NAME = process.env.REACT_APP_NAME;
|
2023-08-05 20:11:02 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 14:57:51 +08:00
|
|
|
// 翻译页面
|
2023-08-26 12:11:21 +08:00
|
|
|
const href = isIframe ? document.referrer : document.location.href;
|
2023-08-10 13:41:55 +08:00
|
|
|
const setting = await getSetting();
|
2023-08-09 14:57:51 +08:00
|
|
|
const rules = await getRules();
|
2023-08-26 12:11:21 +08:00
|
|
|
const rule = await matchRule(rules, href, setting);
|
2023-08-10 13:41:55 +08:00
|
|
|
const translator = new Translator(rule, setting);
|
2023-08-07 21:31:29 +08:00
|
|
|
|
2023-08-26 11:43:00 +08:00
|
|
|
if (isIframe) {
|
|
|
|
|
// iframe
|
|
|
|
|
window.addEventListener("message", (e) => {
|
|
|
|
|
const action = e?.data?.action;
|
|
|
|
|
switch (action) {
|
|
|
|
|
case MSG_TRANS_TOGGLE:
|
|
|
|
|
translator.toggle();
|
|
|
|
|
break;
|
|
|
|
|
case MSG_TRANS_PUTRULE:
|
|
|
|
|
translator.updateRule(e.data.args || {});
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 14:57:51 +08:00
|
|
|
// 浮球按钮
|
2023-08-16 22:38:58 +08:00
|
|
|
const fab = await getFab();
|
2023-08-07 21:31:29 +08:00
|
|
|
const $action = document.createElement("div");
|
|
|
|
|
$action.setAttribute("id", "kiss-translator");
|
2023-08-05 23:56:16 +08:00
|
|
|
document.body.parentElement.appendChild($action);
|
2023-08-24 14:57:54 +08:00
|
|
|
const shadowContainer = $action.attachShadow({ mode: "closed" });
|
2023-08-07 21:31:29 +08:00
|
|
|
const emotionRoot = document.createElement("style");
|
|
|
|
|
const shadowRootElement = document.createElement("div");
|
|
|
|
|
shadowContainer.appendChild(emotionRoot);
|
|
|
|
|
shadowContainer.appendChild(shadowRootElement);
|
|
|
|
|
const cache = createCache({
|
|
|
|
|
key: "css",
|
|
|
|
|
prepend: true,
|
|
|
|
|
container: emotionRoot,
|
|
|
|
|
});
|
|
|
|
|
ReactDOM.createRoot(shadowRootElement).render(
|
|
|
|
|
<React.StrictMode>
|
|
|
|
|
<CacheProvider value={cache}>
|
2023-08-16 22:38:58 +08:00
|
|
|
<Action translator={translator} fab={fab} />
|
2023-08-07 21:31:29 +08:00
|
|
|
</CacheProvider>
|
|
|
|
|
</React.StrictMode>
|
|
|
|
|
);
|
2023-08-21 14:03:39 +08:00
|
|
|
|
|
|
|
|
// 注册菜单
|
2023-08-23 10:39:01 +08:00
|
|
|
if (isGm) {
|
2023-08-27 16:41:14 +08:00
|
|
|
try {
|
|
|
|
|
GM.registerMenuCommand(
|
|
|
|
|
"Toggle Translate",
|
|
|
|
|
(event) => {
|
|
|
|
|
translator.toggle();
|
|
|
|
|
},
|
|
|
|
|
"Q"
|
|
|
|
|
);
|
|
|
|
|
GM.registerMenuCommand(
|
|
|
|
|
"Toggle Style",
|
|
|
|
|
(event) => {
|
|
|
|
|
translator.toggleStyle();
|
|
|
|
|
},
|
|
|
|
|
"C"
|
|
|
|
|
);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("[registerMenuCommand]", err);
|
|
|
|
|
}
|
2023-08-23 10:39:01 +08:00
|
|
|
}
|
2023-08-22 16:27:09 +08:00
|
|
|
|
|
|
|
|
// 同步订阅规则
|
|
|
|
|
trySyncAllSubRules(setting);
|
2023-08-27 16:41:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(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);
|
|
|
|
|
}
|
2023-08-05 15:32:51 +08:00
|
|
|
})();
|