diff --git a/src/views/Selection/index.js b/src/views/Selection/index.js index 363269d..4730bc9 100644 --- a/src/views/Selection/index.js +++ b/src/views/Selection/index.js @@ -3,6 +3,7 @@ import TranBtn from "./TranBtn"; import TranBox from "./TranBox"; import { shortcutRegister } from "../../libs/shortcut"; import { sleep } from "../../libs/utils"; +import { isGm } from "../../libs/client"; import { MSG_TRANSLATE_SELECTED, 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 ( <> {showBox && (