From a6db23f5fbebaff4c2b467a7204a1f201725452f Mon Sep 17 00:00:00 2001 From: Nutr1t07 Date: Thu, 7 Jan 2021 17:37:07 +0800 Subject: [PATCH] Only translate text when specified key pressed (#254) --- src/_locales/en/messages.json | 24 +++++++++++++++++++++ src/content/index.js | 20 +++++++++++++++++ src/settings/defaultSettings.js | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 80ce00e..adb9a7d 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -95,6 +95,30 @@ "isDisabledInTextFieldsCaptionLabel": { "message": "Don't display translation button or panel when selecting text in a text field." }, + "ifOnlyTranslateWhenShiftPressedLabel": { + "message": "Translate on specified key pressed" + }, + "ifOnlyTranslateWhenShiftPressedCaptionLabel": { + "message": "Only display translation on Shift key pressed" + }, + "specifiedKeyLabel": { + "message": "Specified Key" + }, + "specifyKeyLabel": { + "message": "Specify Key" + }, + "shiftLabel": { + "message": "Shift" + }, + "ctrlLabel": { + "message": "Ctrl" + }, + "altLabel": { + "message": "Alt" + }, + "cmdLabel": { + "message": "Command" + }, "disableUrlListLabel": { "message": "URL list to disable translation" }, diff --git a/src/content/index.js b/src/content/index.js index 70d5200..cabff64 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -38,6 +38,26 @@ const handleMouseUp = async e => { if (isInContentEditable()) return; } + if (getSettings("ifOnlyTranslateWhenShiftPressed")) { + const spKey = getSettings("specifiedKey") + switch(spKey){ + case "shift": + if (!e.shiftKey) return; + break; + case "alt": + if (!e.altKey) return; + break; + case "ctrl": + if (!e.ctrlKey) return; + break; + case "cmd": + if (!e.metaKey) return; + break; + default: + break; + } + } + const clickedPosition = { x: e.clientX, y: e.clientY }; const selectedPosition = getSelectedPosition(); showTranslateContainer(selectedText, selectedPosition, clickedPosition); diff --git a/src/settings/defaultSettings.js b/src/settings/defaultSettings.js index 9de53a0..da9ca8d 100644 --- a/src/settings/defaultSettings.js +++ b/src/settings/defaultSettings.js @@ -87,6 +87,44 @@ export default [ } ] }, + { + title: "specifiedKeyLabel", + captions: [], + type: "none", + childElements:[ + { + id: "ifOnlyTranslateWhenShiftPressed", + title: "ifOnlyTranslateWhenShiftPressedLabel", + captions: ["ifOnlyTranslateWhenShiftPressedCaptionLabel"], + type: "checkbox", + default: false + }, + { + id: "specifiedKey", + title: "specifiedKeyLabel", + captions: [], + type: "select", + default: "shift", + options: [ + { + name: "shiftLabel", + value: "shift" + }, + { + name: "ctrlLabel", + value: "ctrl" + }, + { + name: "altLabel", + value: "alt" + }, + { + name: "cmdLabel", + value: "cmd" + }] + } + ] + }, { id: "ifChangeSecondLangOnPage", title: "ifChangeSecondLangLabel",