GM registerMenuCommand: open transbox, translate selected

This commit is contained in:
Gabe Yuan
2023-11-22 14:45:01 +08:00
parent 64d2481e93
commit c629a1252c

View File

@@ -3,6 +3,7 @@ import TranBtn from "./TranBtn";
import TranBox from "./TranBox"; import TranBox from "./TranBox";
import { shortcutRegister } from "../../libs/shortcut"; import { shortcutRegister } from "../../libs/shortcut";
import { sleep } from "../../libs/utils"; import { sleep } from "../../libs/utils";
import { isGm } from "../../libs/client";
import { import {
MSG_TRANSLATE_SELECTED, MSG_TRANSLATE_SELECTED,
MSG_OPEN_TRANBOX, MSG_OPEN_TRANBOX,
@@ -104,6 +105,41 @@ export default function Slection({ tranboxSetting, transApis }) {
}; };
}, []); }, []);
useEffect(() => {
if (!isGm) {
return;
}
// 注册菜单
try {
const menuCommandIds = [];
menuCommandIds.push(
GM.registerMenuCommand(
"Translate Selected Text (Alt+S)",
(event) => {
handleTranSelected();
},
"S"
),
GM.registerMenuCommand(
"Open Translate Popup (Alt+B)",
(event) => {
setShowBox((pre) => !pre);
},
"B"
)
);
return () => {
menuCommandIds.forEach((id) => {
GM.unregisterMenuCommand(id);
});
};
} catch (err) {
console.log("[registerMenuCommand]", err);
}
}, [handleTranSelected]);
return ( return (
<> <>
{showBox && ( {showBox && (