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-05 15:32:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 入口函数
|
|
|
|
|
*/
|
|
|
|
|
(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
|
|
|
// skip iframe
|
2023-08-06 22:01:47 +08:00
|
|
|
if (window.self !== window.top) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 14:57:51 +08:00
|
|
|
// 翻译页面
|
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-20 19:27:29 +08:00
|
|
|
const rule = await matchRule(rules, document.location.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-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-07 21:31:29 +08:00
|
|
|
const shadowContainer = $action.attachShadow({ mode: "open" });
|
|
|
|
|
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) {
|
|
|
|
|
GM.registerMenuCommand(
|
|
|
|
|
"Toggle Translate",
|
|
|
|
|
(event) => {
|
|
|
|
|
translator.toggle();
|
|
|
|
|
},
|
|
|
|
|
"Q"
|
|
|
|
|
);
|
|
|
|
|
GM.registerMenuCommand(
|
|
|
|
|
"Toggle Style",
|
|
|
|
|
(event) => {
|
|
|
|
|
translator.toggleStyle();
|
|
|
|
|
},
|
|
|
|
|
"C"
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-08-22 16:27:09 +08:00
|
|
|
|
|
|
|
|
// 同步订阅规则
|
|
|
|
|
trySyncAllSubRules(setting);
|
2023-08-05 15:32:51 +08:00
|
|
|
})();
|