Skip to content

Commit

Permalink
fix bing chat writing
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Jan 27, 2024
1 parent e2e3f5b commit 236613d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
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.115
- fix bing chat writing
- 0.1.114
- fix non english writing conflict
- 0.1.113
Expand Down
28 changes: 16 additions & 12 deletions src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ function startMouseoverDetector() {
enableMouseoverTextEvent();
addEventHandler("mouseoverText", async function (event) {
// if no selected text
if (setting["translateWhen"].includes("mouseover") && !selectedText) {
var mouseoverText = event?.mouseoverText?.[getDetectType()];
var mouseoverRange = event?.mouseoverText?.[getDetectType() + "_range"];
if (
setting["translateWhen"].includes("mouseover") &&
!selectedText &&
event?.mouseoverText
) {
var mouseoverText = event.mouseoverText[getDetectType()];
var mouseoverRange = event.mouseoverText[getDetectType() + "_range"];
await processWord(mouseoverText, "mouseover", mouseoverRange);
}
});
Expand Down Expand Up @@ -101,7 +105,8 @@ async function processWord(word, actionType, range) {
if (
!checkWindowFocus() ||
checkMouseTargetIsTooltip() ||
activatedWord == word
activatedWord == word ||
!util.isExtensionOnline()
) {
return;
} else if (!word) {
Expand All @@ -122,17 +127,17 @@ async function processWord(word, actionType, range) {
setting["translateReverseTarget"]
);

//if translated text is empty, hide tooltip
// if translation is not recent one, do not update
if (
//if translated text is empty, hide tooltip
if (activatedWord != word) {
return;
} else if (
!translatedText ||
sourceLang == targetLang ||
setting["langExcludeList"].includes(sourceLang)
) {
hideTooltip();
return;
} else if (activatedWord != word) {
return;
}

//if tooltip is on or activation key is pressed, show tooltip
Expand Down Expand Up @@ -313,7 +318,6 @@ async function translateWriting() {
if (isBroken) {
return;
}

insertText(translatedText);
}

Expand All @@ -330,12 +334,12 @@ async function getWritingText() {

async function makeNonEnglishTypingFinish() {
//refocus input text to prevent prev typing
var ele = document.activeElement;
var ele = util.getActiveElement();
await util.wait(10);
window.getSelection().removeAllRanges();
ele.blur();
ele?.blur();
await util.wait(10);
ele.focus();
ele?.focus();
document.execCommand("selectAll", false, null);
await util.wait(100);
}
Expand Down
4 changes: 3 additions & 1 deletion src/event/mouseover.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var clientY = 0;
var _win;
var _isIframe = false;
var styleElement;
const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"];

export function enableMouseoverTextEvent(_window = window) {
_win = _window;
Expand Down Expand Up @@ -92,7 +93,8 @@ function getTextFromRange(range) {

function expandRange(range, type) {
try {
if (type == "container") {
if (type == "container" && range.startContainer) {
console.log(range.startContainer);
range.setStartBefore(range.startContainer);
range.setEndAfter(range.startContainer);
range.setStart(range.startContainer, 0);
Expand Down
20 changes: 18 additions & 2 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ export var defaultData = {
popupCount: "0",
};

const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"];

var rtlLangList = [
"ar", //Arabic
"iw", //Hebrew
Expand Down Expand Up @@ -934,3 +932,21 @@ export async function waitUntilForever(fn) {
export async function wait(time) {
await new Promise((r) => setTimeout(r, time));
}

export function isExtensionOnline() {
return chrome.runtime?.id;
}

export function getActiveElement(root = document) {
const activeEl = root.activeElement;

if (!activeEl) {
return null;
}

if (activeEl.shadowRoot) {
return getActiveElement(activeEl.shadowRoot);
} else {
return activeEl;
}
}

0 comments on commit 236613d

Please sign in to comment.