Files
kiss-translator/src/userscript.js

120 lines
3.4 KiB
JavaScript
Raw Normal View History

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";
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-29 00:42:11 +08:00
import { handlePing, injectScript } from "./libs/gm";
2023-08-05 15:32:51 +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-29 00:06:50 +08:00
// unsafeWindow.GM = GM;
// unsafeWindow.APP_NAME = process.env.REACT_APP_NAME;
const ping = btoa(Math.random()).slice(3, 11);
window.addEventListener(ping, handlePing);
2023-08-29 01:17:22 +08:00
// window.eval(`(${injectScript})("${ping}")`); // eslint-disable-line
const script = document.createElement("script");
script.textContent = `(${injectScript})("${ping}")`;
if (document.head) {
document.head.append(script);
}
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-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();
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" });
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} />
</CacheProvider>
</React.StrictMode>
);
2023-08-21 14:03:39 +08:00
// 注册菜单
2023-08-23 10:39:01 +08:00
if (isGm) {
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
}
// 同步订阅规则
trySyncAllSubRules(setting);
};
(async () => {
try {
await init();
} catch (err) {
const $err = document.createElement("div");
$err.innerText = `KISS-Translator: ${err.message}`;
$err.style.cssText = "background:red; color:#fff; z-index:10000;";
document.body.prepend($err);
}
2023-08-05 15:32:51 +08:00
})();