From bb6bf77df7cfaa7d338e4bd86884bdfb1eef92c2 Mon Sep 17 00:00:00 2001 From: sienori Date: Tue, 31 Jul 2018 23:02:29 +0900 Subject: [PATCH] Add waiting time option --- simple-translate/_locales/en/messages.json | 10 ++++++++++ simple-translate/options/options.html | 16 ++++++++++++++++ simple-translate/popup/popup.js | 19 +++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/simple-translate/_locales/en/messages.json b/simple-translate/_locales/en/messages.json index a97835b..3a24aed 100644 --- a/simple-translate/_locales/en/messages.json +++ b/simple-translate/_locales/en/messages.json @@ -86,6 +86,16 @@ "secondTargetLangCaptionLabel": { "message": "Select the second target language." }, + "waitTimeLabel": { + "message": "Waiting time to translate" + }, + "waitTimeCaptionLabel": { + "message": + "Specify the waiting time from the input of a character to the start of translation. (millisecond)" + }, + "waitTime2CaptionLabel": { + "message": "If you translate it many times in a short time, it may become unusable for a while." + }, "menuLabel": { "message": "Context menu" diff --git a/simple-translate/options/options.html b/simple-translate/options/options.html index 93deb6b..50c27a9 100644 --- a/simple-translate/options/options.html +++ b/simple-translate/options/options.html @@ -170,6 +170,22 @@ + +
+ +
  • +
    +

    翻訳までの待ち時間

    +

    文字が入力されてから翻訳を開始するまでの待ち時間を指定します。(ミリ秒)

    +

    短時間に何回も翻訳すると,しばらくの間利用できなくなることがあります。

    +
    +
    + +
    +
  • + diff --git a/simple-translate/popup/popup.js b/simple-translate/popup/popup.js index cda9e53..70df143 100644 --- a/simple-translate/popup/popup.js +++ b/simple-translate/popup/popup.js @@ -124,7 +124,8 @@ textarea.addEventListener("paste", resize); textarea.addEventListener("keydown", resize); textarea.addEventListener("keyup", function(event) { - //if (event.keyCode == 13) resize(); + if (sourceWord == textarea.value) return; + resize(); inputText(); }); @@ -145,9 +146,23 @@ function textAreaClick() { textarea.select(); } +let inputTimer; //文字入力時の処理 -async function inputText() { +function inputText() { sourceWord = textarea.value; + const waitTime = S.get().waitTime; + + clearTimeout(inputTimer); + inputTimer = setTimeout(() => { + runTranslation(); + }, waitTime); +} + +async function runTranslation() { + if (sourceWord == "") { + showResult("", ""); + return; + } const resultData = await T.translate(sourceWord, "auto", langList.value); changeSecondLang(defaultTargetLang, resultData.sourceLanguage, resultData.percentage);