Files
kiss-translator/src/userscript.js

111 lines
3.3 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-31 20:57:51 +08:00
import {
getSettingWithDefault,
getRulesWithDefault,
getFabWithDefault,
} from "./libs/storage";
2023-08-05 15:32:51 +08:00
import { Translator } from "./libs/translator";
2023-08-30 18:05:37 +08:00
import { trySyncAllSubRules } from "./libs/subRules";
2023-08-26 11:43:00 +08:00
import { MSG_TRANS_TOGGLE, MSG_TRANS_PUTRULE } from "./config";
import { isIframe } from "./libs/iframe";
2023-09-02 15:54:51 +08:00
import { handlePing, injectScript } from "./libs/gm";
2023-08-31 00:18:57 +08:00
import { matchRule } from "./libs/rules";
2023-09-02 14:14:27 +08:00
import { genEventName } from "./libs/utils";
2023-09-08 16:56:00 +08:00
import { webfix } from "./libs/webfix";
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-09-02 15:54:51 +08:00
if (GM?.info?.script?.grant?.includes("unsafeWindow")) {
2023-08-31 20:57:51 +08:00
unsafeWindow.GM = GM;
2023-09-02 15:54:51 +08:00
unsafeWindow.APP_INFO = {
name: process.env.REACT_APP_NAME,
version: process.env.REACT_APP_VERSION,
};
2023-08-31 20:57:51 +08:00
} else {
2023-09-02 14:14:27 +08:00
const ping = genEventName();
2023-08-31 20:57:51 +08:00
window.addEventListener(ping, handlePing);
// window.eval(`(${injectScript})("${ping}")`); // eslint-disable-line
const script = document.createElement("script");
script.textContent = `(${injectScript})("${ping}")`;
2023-08-29 01:17:22 +08:00
document.head.append(script);
}
2023-08-31 20:57:51 +08:00
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-31 00:18:57 +08:00
const setting = await getSettingWithDefault();
const rules = await getRulesWithDefault();
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-09-08 16:56:00 +08:00
webfix(href, 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-31 00:18:57 +08:00
const fab = await getFabWithDefault();
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
// 同步订阅规则
trySyncAllSubRules(setting);
};
(async () => {
try {
await init();
} catch (err) {
2023-09-05 16:06:48 +08:00
console.error("[KISS-Translator]", err);
const $err = document.createElement("div");
$err.innerText = `KISS-Translator: ${err.message}`;
2023-09-05 16:06:48 +08:00
$err.style.cssText = "background:red; color:#fff;";
document.body.prepend($err);
}
2023-08-05 15:32:51 +08:00
})();