Files
kiss-translator/src/userscript.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-08-05 20:11:02 +08:00
import React from "react";
import ReactDOM from "react-dom/client";
import Options from "./views/Options";
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-05 20:11:02 +08:00
2023-08-05 15:32:51 +08:00
import {
MSG_TRANS_TOGGLE,
MSG_TRANS_GETRULE,
MSG_TRANS_PUTRULE,
2023-08-06 21:12:01 +08:00
MSG_TRANS_CURRULE,
EVENT_KISS,
2023-08-05 15:32:51 +08:00
} from "./config";
import { getRules, matchRule } from "./libs";
import { getSetting } from "./libs";
import { transPool } from "./libs/pool";
import { Translator } from "./libs/translator";
2023-08-05 23:56:16 +08:00
/**
* 自定义元素
*/
class ActionElement extends HTMLElement {
connectedCallback() {
const shadowContainer = this.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}>
<Action />
</CacheProvider>
</React.StrictMode>
);
}
}
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 (
document.location.href.includes("http://localhost:3000/options.html") ||
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-05 20:11:02 +08:00
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Options />
</React.StrictMode>
);
return;
}
2023-08-06 22:01:47 +08:00
// iframe
if (window.self !== window.top) {
return;
}
2023-08-05 23:56:16 +08:00
// 插入按钮
const actionName = "kiss-action";
customElements.define(actionName, ActionElement);
const $action = document.createElement(actionName);
document.body.parentElement.appendChild($action);
// 翻译页面
2023-08-05 15:32:51 +08:00
const { fetchInterval, fetchLimit } = await getSetting();
transPool.update(fetchInterval, fetchLimit);
const rules = await getRules();
const rule = matchRule(rules, document.location.href);
const translator = new Translator(rule);
// 监听消息
2023-08-06 21:12:01 +08:00
window.addEventListener(EVENT_KISS, (e) => {
const action = e?.detail?.action;
const args = e?.detail?.args || {};
2023-08-05 15:32:51 +08:00
switch (action) {
case MSG_TRANS_TOGGLE:
translator.toggle();
break;
case MSG_TRANS_GETRULE:
2023-08-06 21:12:01 +08:00
window.dispatchEvent(
new CustomEvent(EVENT_KISS, {
detail: {
action: MSG_TRANS_CURRULE,
args: translator.rule,
},
})
);
2023-08-05 15:32:51 +08:00
break;
case MSG_TRANS_PUTRULE:
translator.updateRule(args);
break;
default:
2023-08-06 22:01:47 +08:00
// console.log(`[entry] kissEvent action skip: ${action}`);
2023-08-05 15:32:51 +08:00
}
});
})();