From 4a6f907c7338d414a1f1260dd6474bbedb606f64 Mon Sep 17 00:00:00 2001 From: sienori Date: Sun, 20 Oct 2019 19:14:48 +0900 Subject: [PATCH] Add option to disable translation in text fields --- src/_locales/en/messages.json | 6 ++++++ src/content/index.js | 11 +++++++++++ src/settings/defaultSettings.js | 11 +++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index d4efb55..78573d4 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -85,6 +85,12 @@ "ifChangeSecondLangOnPageCaptionLabel": { "message": "Detects the language of the selected text, and if it is the same as the default target language, translate it into the second language." }, + "isDisabledInTextFieldsLabel": { + "message": "Disable translation in text fields" + }, + "isDisabledInTextFieldsCaptionLabel": { + "message": "Don't display translation button or panel when selecting text in a text field." + }, "disableUrlListLabel": { "message": "URL list to disable translation" }, diff --git a/src/content/index.js b/src/content/index.js index 9c3cbdd..70d5200 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -34,6 +34,10 @@ const handleMouseUp = async e => { prevSelectedText = selectedText; if (selectedText.length === 0) return; + if (getSettings("isDisabledInTextFields")) { + if (isInContentEditable()) return; + } + const clickedPosition = { x: e.clientX, y: e.clientY }; const selectedPosition = getSelectedPosition(); showTranslateContainer(selectedText, selectedPosition, clickedPosition); @@ -82,6 +86,13 @@ const getSelectedPosition = () => { return selectedPosition; }; +const isInContentEditable = () => { + const element = document.activeElement; + if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") return true; + if (element.contentEditable === "true") return true; + return false; +}; + const handleKeyDown = e => { if (e.key === "Escape") { removeTranslatecontainer(); diff --git a/src/settings/defaultSettings.js b/src/settings/defaultSettings.js index f85e2c1..7131764 100644 --- a/src/settings/defaultSettings.js +++ b/src/settings/defaultSettings.js @@ -94,14 +94,21 @@ export default [ type: "checkbox", default: false }, + { + id: "isDisabledInTextFields", + title: "isDisabledInTextFieldsLabel", + captions: ["isDisabledInTextFieldsCaptionLabel"], + type: "checkbox", + default: false, + new: true + }, { id: "disableUrlList", title: "disableUrlListLabel", captions: ["disableUrlListCaptionLabel"], type: "textarea", default: "", - placeholder: "https://example.com/*\nhttps://example.net/*", - new: true + placeholder: "https://example.com/*\nhttps://example.net/*" } ] },