From c629a1252cd246baeedddacc6b876d7e1028b7bc Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Wed, 22 Nov 2023 14:45:01 +0800 Subject: [PATCH] GM registerMenuCommand: open transbox, translate selected --- src/views/Selection/index.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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 && (