diff --git a/config-overrides.js b/config-overrides.js index b56cc4c..2a974d8 100644 --- a/config-overrides.js +++ b/config-overrides.js @@ -84,6 +84,7 @@ const userscriptWebpack = (config, env) => { // @downloadURL ${process.env.REACT_APP_USERSCRIPT_DOWNLOADURL} // @updateURL ${process.env.REACT_APP_USERSCRIPT_DOWNLOADURL} // @grant GM.xmlHttpRequest +// @grant GM.registerMenuCommand // @grant GM.setValue // @grant GM.getValue // @grant GM.deleteValue diff --git a/public/_locales/en/messages.json b/public/_locales/en/messages.json index c1d3536..3d57ec9 100644 --- a/public/_locales/en/messages.json +++ b/public/_locales/en/messages.json @@ -4,5 +4,8 @@ }, "app_description": { "message": "A minimalist bilingual translation Extension & Greasemonkey Script" + }, + "toggle_translate": { + "message": "Toggle Translate" } } diff --git a/public/_locales/zh_CN/messages.json b/public/_locales/zh_CN/messages.json index 5ec9fca..ece6e08 100644 --- a/public/_locales/zh_CN/messages.json +++ b/public/_locales/zh_CN/messages.json @@ -4,5 +4,8 @@ }, "app_description": { "message": "一个简约的双语网页翻译扩展 & 油猴脚本" + }, + "toggle_translate": { + "message": "切换翻译" } } diff --git a/public/manifest.firefox.json b/public/manifest.firefox.json index 64ec79c..01d3310 100644 --- a/public/manifest.firefox.json +++ b/public/manifest.firefox.json @@ -15,6 +15,14 @@ "matches": [""] } ], + "commands": { + "toggleTranslate": { + "suggested_key": { + "default": "Alt+Q" + }, + "description": "__MSG_toggle_translate__" + } + }, "permissions": ["", "storage"], "icons": { "16": "images/logo16.png", diff --git a/public/manifest.json b/public/manifest.json index 7d0110d..a04bc1b 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -16,6 +16,14 @@ "matches": [""] } ], + "commands": { + "toggleTranslate": { + "suggested_key": { + "default": "Alt+Q" + }, + "description": "__MSG_toggle_translate__" + } + }, "permissions": ["storage"], "host_permissions": [""], "icons": { diff --git a/src/background.js b/src/background.js index 1f01c89..d5f2a45 100644 --- a/src/background.js +++ b/src/background.js @@ -3,6 +3,8 @@ import { MSG_FETCH, MSG_FETCH_LIMIT, MSG_FETCH_CLEAR, + MSG_TRANS_TOGGLE, + CMD_TOGGLE_TRANSLATE, DEFAULT_SETTING, DEFAULT_RULES, DEFAULT_SYNC, @@ -15,6 +17,7 @@ import storage from "./libs/storage"; import { getSetting } from "./libs"; import { syncAll } from "./libs/sync"; import { fetchData, fetchPool } from "./libs/fetch"; +import { sendTabMsg } from "./libs/msg"; /** * 插件安装 @@ -74,3 +77,16 @@ browser.runtime.onMessage.addListener( return true; } ); + +/** + * 监听快捷键 + */ +browser.commands.onCommand.addListener((command) => { + // console.log(`Command: ${command}`); + switch (command) { + case CMD_TOGGLE_TRANSLATE: + sendTabMsg(MSG_TRANS_TOGGLE); + break; + default: + } +}); diff --git a/src/config/index.js b/src/config/index.js index 4287bea..9476f6a 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -18,6 +18,8 @@ export const STOKEY_SYNC = `${APP_NAME}_sync`; export const STOKEY_FAB = `${APP_NAME}_fab`; export const STOKEY_RULESCACHE_PREFIX = `${APP_NAME}_rulescache_`; +export const CMD_TOGGLE_TRANSLATE = "toggleTranslate"; + export const CLIENT_WEB = "web"; export const CLIENT_CHROME = "chrome"; export const CLIENT_EDGE = "edge"; diff --git a/src/userscript.js b/src/userscript.js index f103393..dd7e3da 100644 --- a/src/userscript.js +++ b/src/userscript.js @@ -54,4 +54,14 @@ import { Translator } from "./libs/translator"; ); + + // 注册菜单 + GM.registerMenuCommand( + "Toggle Translate", + (event) => { + // console.log("Menu item selected", event); + translator.toggle(); + }, + "Q" + ); })(); diff --git a/src/views/Action/index.js b/src/views/Action/index.js index 2f883aa..7895b5b 100644 --- a/src/views/Action/index.js +++ b/src/views/Action/index.js @@ -112,7 +112,9 @@ export default function Action({ translator, fab }) { } > - + {showPopup && ( + + )}