From f8703343c3f2dd16ed7b4699223adb6505f1e722 Mon Sep 17 00:00:00 2001 From: daniel k Date: Fri, 26 Jan 2024 18:40:56 +0900 Subject: [PATCH] change writing logic --- .github/workflows/publish.yml | 4 +- doc/description.md | 2 + src/contentScript.js | 75 +++++++++++++++++++---------------- src/util/index.js | 4 ++ 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 8ff2f57f..c38c1529 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -23,6 +23,8 @@ jobs: run: echo ${{env.RELEASE_FILE}} - name: Publish to store - uses: PlasmoHQ/bpp@v3.5.0 + uses: PlasmoHQ/bpp@v3 with: keys: ${{ secrets.EDGE_BPP_KEYS }} + edge-file: "${{env.RELEASE_FILE}}" + edge-notes: "https://github.com/ttop32/MouseTooltipTranslator" diff --git a/doc/description.md b/doc/description.md index 06f2b747..7188cc47 100644 --- a/doc/description.md +++ b/doc/description.md @@ -19,6 +19,8 @@ Mouseover Translate Any Language At Once English, Russian, Japanese, Chinese and so on # Change Log +- 0.1.113 + - change writing text logic - 0.1.112 - fix tooltip highlight (request by marcello-pietrobon) - 0.1.111 diff --git a/src/contentScript.js b/src/contentScript.js index 361b2fe1..b7f6164d 100644 --- a/src/contentScript.js +++ b/src/contentScript.js @@ -297,9 +297,8 @@ async function translateWriting() { ) { return; } - // get writing text - var writingText = getWritingText(); + var writingText = await getWritingText(); if (!writingText) { return; } @@ -311,37 +310,56 @@ async function translateWriting() { setting["translateTarget"] ); //skip no translation or is too late to respond - if (isBroken || writingText != getWritingText()) { + if (isBroken) { return; } insertText(translatedText); } -function getWritingText() { +async function getWritingText() { // get current selected text, if no select, select all to get all - if (window.getSelection().type == "Caret") { - document.execCommand("selectAll", false, null); + if (window.getSelection().type != "Caret") { + return getSelectionText(true); } - return getSelectionText(true); + document.execCommand("selectAll", false, null); + var text = getSelectionText(true); + await makeNonEnglishTypingFinish(); + return text; } -function insertText(inputText) { - if (!inputText) { - return; - } - document.execCommand("insertHTML", false, inputText); - pasteTextGoogleDoc(inputText); +async function makeNonEnglishTypingFinish() { + //refocus input text to prevent prev typing + var ele = document.activeElement; + await util.wait(10); + ele.blur(); + await util.wait(10); + ele.focus(); + document.execCommand("selectAll", false, null); } -function pasteTextGoogleDoc(text) { - if (!util.isGoogleDoc()) { +async function insertText(text) { + if (!text) { return; + } else if (matchUrl(document.location.href, "discord.com")) { + pasteTextInputBox(text); + } else if (util.isGoogleDoc()) { + pasteTextGoogleDoc(text); + } else { + document.execCommand("insertHTML", false, text); } +} +function pasteTextInputBox(text) { + var ele = document.activeElement; + pasteText(ele, text); +} +function pasteTextGoogleDoc(text) { // https://github.com/matthewsot/docs-plus var el = document.getElementsByClassName("docs-texteventtarget-iframe")[0]; el = el.contentDocument.querySelector("[contenteditable=true]"); - + pasteText(el, text); +} +function pasteText(ele, text) { var data = new DataTransfer(); data.setData("text/plain", text); var paste = new ClipboardEvent("paste", { @@ -350,8 +368,7 @@ function pasteTextGoogleDoc(text) { dataType: "text/plain", }); paste.docs_plus_ = true; - - el.dispatchEvent(paste); + ele.dispatchEvent(paste); } // Listener - detect mouse move, key press, mouse press, tab switch========================================================================================== @@ -379,7 +396,6 @@ function handleMousemove(e) { setMouseStatus(e); setTooltipPosition(e.clientX, e.clientY); ocrView.checkImage(mouseTarget, setting, keyDownList); - checkWritingBox(); } function handleTouchstart(e) { @@ -393,6 +409,8 @@ function handleKeydown(e) { hideTooltip(); } else if (e.code == "Escape") { requestStopTTS(); + } else if (e.key == "HangulMode") { + return; } else if (e.key == "Alt") { e.preventDefault(); // prevent alt site unfocus } @@ -443,19 +461,6 @@ function setTooltipPosition(x, y) { tooltipContainer?.css("transform", `translate(${x}px,${y}px)`); } -const checkWritingBox = debounce(delayTime, () => { - // if mouse target is not writing box or already bound, return - // make key bind for preventDefault - var $writingField = $(util.writingField); - if (!$writingField.is(mouseTarget) || $writingField.data("mttBound")) { - return; - } - $writingField - .data("mttBound", true) - .on("keydown", handleKeydown) - .on("keyup", handleKeyup); -}); - function checkMouseOnceMoved(x, y) { if ( mouseMoved == false && @@ -726,7 +731,6 @@ async function checkGoogleDocs() { if (!util.isGoogleDoc()) { return; } - interceptGoogleDocKeyEvent(); } @@ -770,7 +774,10 @@ function destructor() { function addEventHandler(eventName, callbackFunc) { //record event for later event signal kill - return window.addEventListener(eventName, callbackFunc, { signal }); + return window.addEventListener(eventName, callbackFunc, { + capture: true, + signal, + }); } function removePrevElement() { diff --git a/src/util/index.js b/src/util/index.js index 8b2562f2..948bf2d0 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -930,3 +930,7 @@ export async function waitUntilForever(fn) { timeout: WAIT_FOREVER, }); } + +export async function wait(time) { + await new Promise((r) => setTimeout(r, time)); +}