userscript...

This commit is contained in:
Gabe Yuan
2023-08-05 23:56:16 +08:00
parent cc04e817da
commit 063d7c9ff0
2 changed files with 53 additions and 1 deletions

View File

@@ -1,6 +1,9 @@
import React from "react"; import React from "react";
import ReactDOM from "react-dom/client"; import ReactDOM from "react-dom/client";
import Options from "./views/Options"; import Options from "./views/Options";
import Action from "./views/Action";
import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
import { browser } from "./libs/browser"; import { browser } from "./libs/browser";
import { import {
@@ -13,6 +16,33 @@ import { getSetting } from "./libs";
import { transPool } from "./libs/pool"; import { transPool } from "./libs/pool";
import { Translator } from "./libs/translator"; import { Translator } from "./libs/translator";
/**
* 自定义元素
*/
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>
);
}
}
/** /**
* 入口函数 * 入口函数
*/ */
@@ -28,9 +58,15 @@ import { Translator } from "./libs/translator";
return; return;
} }
// 插入按钮
const actionName = "kiss-action";
customElements.define(actionName, ActionElement);
const $action = document.createElement(actionName);
document.body.parentElement.appendChild($action);
// 翻译页面
const { fetchInterval, fetchLimit } = await getSetting(); const { fetchInterval, fetchLimit } = await getSetting();
transPool.update(fetchInterval, fetchLimit); transPool.update(fetchInterval, fetchLimit);
const rules = await getRules(); const rules = await getRules();
const rule = matchRule(rules, document.location.href); const rule = matchRule(rules, document.location.href);
const translator = new Translator(rule); const translator = new Translator(rule);

16
src/views/Action/index.js Normal file
View File

@@ -0,0 +1,16 @@
import Box from "@mui/material/Box";
import Fab from "@mui/material/Fab";
import AddIcon from "@mui/icons-material/Add";
import ThemeProvider from "../../hooks/Theme";
export default function Action() {
return (
<ThemeProvider>
<Box style={{ position: "fixed", top: 16, right: 16, zIndex: 10001 }}>
<Fab color="primary">
<AddIcon />
</Fab>
</Box>
</ThemeProvider>
);
}