Skip to content

Commit 7aa8e9a

Browse files
committed
fix reddit site conflict
1 parent 3c9b9d0 commit 7aa8e9a

File tree

7 files changed

+80
-38
lines changed

7 files changed

+80
-38
lines changed

doc/description.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Mouseover Translate Any Language At Once
1919
English, Russian, Japanese, Chinese and so on
2020

2121
# Change Log
22+
- 0.1.137
23+
- Fix reddit site conflict
2224
- 0.1.136
2325
- Fix Japanese furigana recognition (request by cspotcode)
2426
- Add speech recognition translator

src/background.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ function addMessageListener() {
5858
} else if (request.type === "requestBase64") {
5959
var base64Url = await util.getBase64(request.url);
6060
sendResponse({ base64Url });
61+
} else if (request.type === "createOffscreen") {
62+
await util.createOffscreen();
63+
sendResponse({});
6164
}
6265
})();
6366
return true;

src/contentScript.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,13 +917,12 @@ function initSpeechRecognition() {
917917
listenEngine.continuous = true;
918918
listenEngine.interimResults = true;
919919
initSpeechRecognitionLang();
920-
var ignore_onend;
921920

922921
listenEngine.onstart = function () {
923922
listening = true;
924923
};
925924
listenEngine.onerror = function (event) {
926-
ignore_onend = true;
925+
console.log(event);
927926
};
928927
listenEngine.onend = function () {
929928
listening = false;

src/event/mouseover.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ async function getMouseoverText(x, y) {
5555
var textElement;
5656
var range =
5757
caretRangeFromPoint(x, y, _win.document) ||
58+
caretRangeFromPointOnDocument(x, y) ||
5859
caretRangeFromPointOnShadowDom(x, y);
5960

6061
//get google doc select
@@ -155,7 +156,9 @@ export function caretRangeFromPointOnShadowDom(x, y) {
155156
}
156157

157158
function getRangeFromTextNodes(x, y, textNodes) {
158-
// text node that position in x y
159+
//filter no text
160+
var textNodes = textNodes.filter((textNode) => textNode.textContent.trim());
161+
// filter no pos overlap
159162
var textNodes = textNodes.filter((textNode) =>
160163
checkXYInElement(getTextRange(textNode), x, y)
161164
);
@@ -182,7 +185,7 @@ export function getAllTextNodes(el) {
182185
function textNodesUnder(el) {
183186
return walkNodeTree(el, NodeFilter.SHOW_TEXT, {
184187
inspect: (textNode) =>
185-
!PARENT_TAGS_TO_EXCLUDE.includes(textNode.parentElement?.nodeName),
188+
!PARENT_TAGS_TO_EXCLUDE.includes(textNode?.parentElement?.nodeName),
186189
});
187190
}
188191

src/tts/offscreen.js renamed to src/offscreen.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ var stopTtsTimestamp = 0;
88
// Listen for messages from the extension
99
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
1010
(async () => {
11-
if (request.type === "playAudioOffscreen") {
11+
if (request.type == "playAudioOffscreen") {
1212
await playAudio(request.data);
1313
sendResponse({});
14-
} else if (request.type === "playSpeechTTSOffscreen") {
14+
} else if (request.type == "playSpeechTTSOffscreen") {
1515
await playSpeechTTS(request.data);
1616
sendResponse({});
17-
} else if (request.type === "stopTTSOffscreen") {
17+
} else if (request.type == "stopTTSOffscreen") {
1818
await stopAudio(request.data);
1919
sendResponse({});
20+
} else if (request.type == "startSpeechRecognition") {
21+
sendResponse({});
22+
} else if (request.type == "stopSpeechRecognition") {
23+
sendResponse({});
2024
}
2125
})();
2226

src/tts/baseTTS.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,12 @@ export default class BaseTTS {
3535
}
3636

3737
static async playAudioOffscreen(source, rate, volume, timestamp) {
38-
await this.createOffscreen();
39-
await util.sendMessage({
40-
type: "playAudioOffscreen",
41-
data: {
42-
source,
43-
rate,
44-
volume,
45-
timestamp,
46-
},
47-
});
38+
await util.createOffscreen();
39+
await util.requestPlayTtsOffscreen(source, rate, volume, timestamp);
4840
}
4941

5042
static async stopTtsOffscreen(timestamp) {
51-
await this.createOffscreen();
52-
await util.sendMessage({ type: "stopTTSOffscreen", data: { timestamp } });
53-
}
54-
55-
// Create the offscreen document
56-
static async createOffscreen() {
57-
try {
58-
await browser?.offscreen?.createDocument({
59-
url: "offscreen.html",
60-
reasons: ["AUDIO_PLAYBACK"],
61-
justification: "play tts", // details for using the API
62-
});
63-
} catch (error) {
64-
if (!error.message.startsWith("Only a single offscreen")) throw error;
65-
}
66-
}
67-
68-
static async removeOffscreen() {
69-
return new Promise((resolve, reject) => {
70-
browser.offscreen.closeDocument(() => resolve());
71-
});
43+
await util.createOffscreen();
44+
await util.requestStopTtsOffscreen(timestamp);
7245
}
7346
}

src/util/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,61 @@ export async function requestBase64(url) {
799799
url,
800800
});
801801
}
802+
803+
export async function requestCreateOffscreen() {
804+
return await sendMessage({
805+
type: "createOffscreen",
806+
});
807+
}
808+
809+
export async function requestStartSpeechRecognitionOffscreen(lang) {
810+
await requestCreateOffscreen();
811+
return await sendMessage({
812+
type: "startSpeechRecognition",
813+
data: {
814+
lang,
815+
},
816+
});
817+
}
818+
819+
export async function requestStopSpeechRecognitionOffscreen() {
820+
await requestCreateOffscreen();
821+
return await sendMessage({
822+
type: "stopSpeechRecognition",
823+
});
824+
}
825+
826+
export async function requestPlayTtsOffscreen(source, rate, volume, timestamp) {
827+
return await sendMessage({
828+
type: "playAudioOffscreen",
829+
data: {
830+
source,
831+
rate,
832+
volume,
833+
timestamp,
834+
},
835+
});
836+
}
837+
838+
export async function requestStopTtsOffscreen(timestamp) {
839+
return await sendMessage({ type: "stopTTSOffscreen", data: { timestamp } });
840+
}
841+
842+
//offscreen =======================
843+
// Create the offscreen document
844+
export async function createOffscreen() {
845+
try {
846+
await browser?.offscreen?.createDocument({
847+
url: "offscreen.html",
848+
reasons: ["WORKERS"],
849+
justification: "TTS & Speech",
850+
});
851+
} catch (error) {
852+
if (!error.message.startsWith("Only a single offscreen")) throw error;
853+
}
854+
}
855+
export async function removeOffscreen() {
856+
return new Promise((resolve, reject) => {
857+
browser.offscreen.closeDocument(() => resolve());
858+
});
859+
}

0 commit comments

Comments
 (0)