Skip to content

Commit

Permalink
change writing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Jan 26, 2024
1 parent c986ab9 commit f870334
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions doc/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 41 additions & 34 deletions src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,8 @@ async function translateWriting() {
) {
return;
}

// get writing text
var writingText = getWritingText();
var writingText = await getWritingText();
if (!writingText) {
return;
}
Expand All @@ -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", {
Expand All @@ -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==========================================================================================
Expand Down Expand Up @@ -379,7 +396,6 @@ function handleMousemove(e) {
setMouseStatus(e);
setTooltipPosition(e.clientX, e.clientY);
ocrView.checkImage(mouseTarget, setting, keyDownList);
checkWritingBox();
}

function handleTouchstart(e) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -726,7 +731,6 @@ async function checkGoogleDocs() {
if (!util.isGoogleDoc()) {
return;
}

interceptGoogleDocKeyEvent();
}

Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,7 @@ export async function waitUntilForever(fn) {
timeout: WAIT_FOREVER,
});
}

export async function wait(time) {
await new Promise((r) => setTimeout(r, time));
}

0 comments on commit f870334

Please sign in to comment.