diff --git a/config/webpack.common.js b/config/webpack.common.js index e776d651..ccc788f3 100644 --- a/config/webpack.common.js +++ b/config/webpack.common.js @@ -98,6 +98,9 @@ const common = { { from: "**/*", context: "public", + filter: (resourcePath) => { + return !resourcePath.includes(".map"); + }, }, ], }), diff --git a/doc/description.md b/doc/description.md index c28154ab..e91e29c9 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.104 + - fix shadow dom performance - 0.1.103 - update tesseract js v5.0.3 - fix pdf shortcut key (request by woong park) diff --git a/public/_locales/el/messages.json b/public/_locales/el/messages.json index f9837c0c..51ffa2c9 100644 --- a/public/_locales/el/messages.json +++ b/public/_locales/el/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Μεταφραστής εργαλείων ποντικιού : Translate Image" + "message": "Μεταφραστής εργαλείων ποντικιού: Translate Image" }, "appDesc": { "message": "Μεταφραστής εργαλείων ποντικιού Μεταφράστε κείμενο ποντικιού χρησιμοποιώντας το Google Translate" diff --git a/public/_locales/es/messages.json b/public/_locales/es/messages.json index bae7df1b..788e8794 100644 --- a/public/_locales/es/messages.json +++ b/public/_locales/es/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Traductor de información sobre herramientas del mouse" + "message": "Traductor de información sobre herramientas del mouse for Education" }, "appDesc": { "message": "El traductor de información sobre herramientas de mouse traducir el texto de mouseover usando Google Translate" diff --git a/public/_locales/es_419/messages.json b/public/_locales/es_419/messages.json index 788e8794..131727aa 100644 --- a/public/_locales/es_419/messages.json +++ b/public/_locales/es_419/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Traductor de información sobre herramientas del mouse for Education" + "message": "Traductor de información sobre herramientas del mouse : deepl google translate" }, "appDesc": { "message": "El traductor de información sobre herramientas de mouse traducir el texto de mouseover usando Google Translate" diff --git a/public/_locales/et/messages.json b/public/_locales/et/messages.json index 13ee2370..15e1fd7b 100644 --- a/public/_locales/et/messages.json +++ b/public/_locales/et/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Hiire tööriistavihje tõlkija for Studying Language" + "message": "Hiire tööriistavihje tõlkija : TTS" }, "appDesc": { "message": "Hiire tööriistapeal tõlkija tõlkida hiireoveri teksti Google'i abil" diff --git a/public/_locales/fil/messages.json b/public/_locales/fil/messages.json index 04439da6..4eefa27c 100644 --- a/public/_locales/fil/messages.json +++ b/public/_locales/fil/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Mouse Tooltip Translator For Chrome and Edge" + "message": "Mouse Tooltip Translator For Chrome Edge" }, "appDesc": { "message": "Mouse Tooltip Translator translate mouseover text using google translate" diff --git a/public/_locales/gu/messages.json b/public/_locales/gu/messages.json index 28490e20..9d8de7af 100644 --- a/public/_locales/gu/messages.json +++ b/public/_locales/gu/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "માઉસ ટૂલટિપ અનુવાદક : TTS" + "message": "માઉસ ટૂલટિપ અનુવાદક: TTS" }, "appDesc": { "message": "ગૂગલ ટ્રાન્સલેશનનો ઉપયોગ કરીને માઉસ ટૂલટિપ અનુવાદક માઉસઓવર ટેક્સ્ટનું ભાષાંતર કરે છે" diff --git a/public/_locales/hi/messages.json b/public/_locales/hi/messages.json index ac54246c..4ee93eff 100644 --- a/public/_locales/hi/messages.json +++ b/public/_locales/hi/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "माउस टूलटिप अनुवादक With Voice Speech translator" + "message": "माउस टूलटिप अनुवादक : PDF Translator" }, "appDesc": { "message": "माउस टूलटिप ट्रांसलेटर अनुवाद करें माउसओवर पाठ Google अनुवाद का उपयोग करके" diff --git a/public/_locales/hr/messages.json b/public/_locales/hr/messages.json index 37547908..06063c4c 100644 --- a/public/_locales/hr/messages.json +++ b/public/_locales/hr/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Translator Alat -a za miša : Dictionary & Papago" + "message": "Translator Alat -a za miša: Dictionary & Papago" }, "appDesc": { "message": "Translator Mouse Tooltip Prevodite tekst MouseOver pomoću Google Translatea" diff --git a/public/_locales/hu/messages.json b/public/_locales/hu/messages.json index edc653d1..b5f1991d 100644 --- a/public/_locales/hu/messages.json +++ b/public/_locales/hu/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Egér ToolTip fordító : Subtitle Epub Reader Translator" + "message": "Egér ToolTip fordító : Subtitle Translator" }, "appDesc": { "message": "Egér ToolTip Translator fordítása MouseOver szöveget a Google Translate segítségével" diff --git a/public/_locales/id/messages.json b/public/_locales/id/messages.json index 5d28c4c8..f9b4cd66 100644 --- a/public/_locales/id/messages.json +++ b/public/_locales/id/messages.json @@ -162,7 +162,7 @@ "message": "Judul " }, "appName": { - "message": "Penerjemah Tooltip Mouse : Penerjemah AI" + "message": "Penerjemah Tooltip Mouse: Penerjemah AI" }, "appDesc": { "message": "Penerjemah Tooltip Mouse yang Menerjemahkan Teks Saat Diarahkan Mouse Menggunakan Google Translate" diff --git a/public/_locales/it/messages.json b/public/_locales/it/messages.json index cbc117f1..fca22852 100644 --- a/public/_locales/it/messages.json +++ b/public/_locales/it/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Traduttore di tooltip del mouse With TTS OCR" + "message": "Traduttore di tooltip del mouse: TTS OCR" }, "appDesc": { "message": "Traduttore di tooltip del mouse Translate Testo del mouseover usando Google Translate" diff --git a/public/_locales/lt/messages.json b/public/_locales/lt/messages.json index 1ec40012..71d8e766 100644 --- a/public/_locales/lt/messages.json +++ b/public/_locales/lt/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Pelės patarimų vertėjas : Translate Youtube" + "message": "Pelės patarimų vertėjas: Translate Youtube" }, "appDesc": { "message": "Pelės patarimo vertėjas vertėjas versti pelės viršų" diff --git a/public/_locales/lv/messages.json b/public/_locales/lv/messages.json index 5cc1880e..a9d526d7 100644 --- a/public/_locales/lv/messages.json +++ b/public/_locales/lv/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Peles rīku padomu tulkotājs : Translate Manga, Epub Viewer" + "message": "Peles rīku padomu tulkotājs: Translate Manga" }, "appDesc": { "message": "Peles rīka padomu tulkotājs tulkot peles pārsniegšanas tekstu, izmantojot Google tulkojumu" diff --git a/public/_locales/ml/messages.json b/public/_locales/ml/messages.json index 796cedb3..abc6b769 100644 --- a/public/_locales/ml/messages.json +++ b/public/_locales/ml/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "മൗസ് ടൂൾടിപ്പ് പരിഭാഷകൻ : translate pdf subtitle" + "message": "മൗസ് ടൂൾടിപ്പ് പരിഭാഷകൻ: translate pdf subtitle" }, "appDesc": { "message": "മ mouse സ് ടൂൾടിപ്പ് ട്രാൻസ്ലേറ്റർ വിവർത്തനം ചെയ്യുക മസോവർ ടെക്സ്റ്റ് Google വിവർത്തനം ഉപയോഗിച്ച്" diff --git a/public/_locales/mr/messages.json b/public/_locales/mr/messages.json index 399a696a..c2012872 100644 --- a/public/_locales/mr/messages.json +++ b/public/_locales/mr/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "माउस टूलटिप ट्रान्सलेटर With Dual Subtitles & translate ebook viewer" + "message": "माउस टूलटिप ट्रान्सलेटर: Dual Subtitles" }, "appDesc": { "message": "माउस टूलटिप ट्रान्सलेटर Google भाषांतर वापरून माउसओव्हर मजकूराचे भाषांतर करा" diff --git a/public/_locales/ms/messages.json b/public/_locales/ms/messages.json index 00d9a4f1..43714b48 100644 --- a/public/_locales/ms/messages.json +++ b/public/_locales/ms/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Penterjemah Tooltip Mouse for PDF GOOGLE DOCS" + "message": "Penterjemah Tooltip Mouse: PDF GOOGLE DOCS" }, "appDesc": { "message": "Penterjemah Tooltip Mouse Terjemahkan Teks Mouseover Menggunakan Google Translate" diff --git a/public/_locales/nl/messages.json b/public/_locales/nl/messages.json index 096747c3..be9edf34 100644 --- a/public/_locales/nl/messages.json +++ b/public/_locales/nl/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Muise -gereedschap vertaler with translate chatgpt, ebook viewer" + "message": "Muise -gereedschap vertaler: translate chatgpt ebook viewer" }, "appDesc": { "message": "Muis Toltip Translator vertaalt mouseover -tekst met behulp van Google Translate" diff --git a/public/_locales/no/messages.json b/public/_locales/no/messages.json index c3fe3692..ed46ff23 100644 --- a/public/_locales/no/messages.json +++ b/public/_locales/no/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Mus ToolTip Translator : PDF, Manga" + "message": "Mus ToolTip Translator : PDF Manga" }, "appDesc": { "message": "Mus ToolTip Translator Translate Mouseover -tekst ved hjelp av Google Translate" diff --git a/public/_locales/pl/messages.json b/public/_locales/pl/messages.json index 1af169bd..2a62716e 100644 --- a/public/_locales/pl/messages.json +++ b/public/_locales/pl/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Tłumacz myszy : Translate Writing" + "message": "Tłumacz myszy: Translate Writing" }, "appDesc": { "message": "Tłumacz myszy tłumacz tłumacza tekst myszy za pomocą tłumacza Google" diff --git a/public/_locales/pt_BR/messages.json b/public/_locales/pt_BR/messages.json index ca095fa9..e0f2c328 100644 --- a/public/_locales/pt_BR/messages.json +++ b/public/_locales/pt_BR/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Tradutor da dica de ferramenta do mouse With PDF Viewer" + "message": "Tradutor da dica de ferramenta do mouse With Epub PDF Viewer" }, "appDesc": { "message": "Mouse Tooltip Tradutor Traduzir Texto do MouseOver usando o Google Translate" diff --git a/public/_locales/sk/messages.json b/public/_locales/sk/messages.json index fed2ee6f..eccb2022 100644 --- a/public/_locales/sk/messages.json +++ b/public/_locales/sk/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Prekladateľ myši : Voice Speech Reader Translator" + "message": "Prekladateľ myši: Voice Speech Reader" }, "appDesc": { "message": "Prekladač prekladateľ myši prekladač Mouseover text pomocou prekladu Google" diff --git a/public/_locales/sl/messages.json b/public/_locales/sl/messages.json index e461bce6..f9b69ec2 100644 --- a/public/_locales/sl/messages.json +++ b/public/_locales/sl/messages.json @@ -162,7 +162,7 @@ "message": "Title " }, "appName": { - "message": "Prevajalec orodij za miško For Productivity" + "message": "Prevajalec orodij za miško: Translator Extension" }, "appDesc": { "message": "Prevajalec za mišjo orodje Translate MuseOver besedilo z uporabo Google Translate" diff --git a/src/background.js b/src/background.js index 59db2c87..d94e7b74 100644 --- a/src/background.js +++ b/src/background.js @@ -4,7 +4,6 @@ import browser from "webextension-polyfill"; import { waitUntil } from "async-wait-until"; -import delay from "delay"; import translator from "./translator/index.js"; import * as util from "/src/util"; @@ -310,7 +309,7 @@ function getBase64Url(blob) { async function playSound(source, rate, volume) { await createOffscreen(); - await chrome.runtime.sendMessage({ + await util.sendMessage({ play: { source, rate, @@ -321,14 +320,13 @@ async function playSound(source, rate, volume) { async function stopTtsOffscreen() { await createOffscreen(); - await chrome.runtime.sendMessage({ stop: {} }); - // removeOffscreen(); + util.sendMessage({ stop: {} }); } // Create the offscreen document async function createOffscreen() { try { - await chrome.offscreen.createDocument({ + await browser.offscreen.createDocument({ url: "offscreen.html", reasons: ["AUDIO_PLAYBACK"], justification: "play tts", // details for using the API @@ -340,7 +338,7 @@ async function createOffscreen() { function removeOffscreen() { return new Promise((resolve, reject) => { - chrome.offscreen.closeDocument(() => resolve()); + browser.offscreen.closeDocument(() => resolve()); }); } diff --git a/src/ocr/ocrIframe.js b/src/ocr/ocrIframe.js index a04476ce..bdbb32a1 100644 --- a/src/ocr/ocrIframe.js +++ b/src/ocr/ocrIframe.js @@ -15,8 +15,6 @@ window.addEventListener( async function ({ data }) { if (data.type === "ocr") { doOcr(data); - } else if (data.type === "initTesseract") { - initTesseract(data); } }, false @@ -31,7 +29,6 @@ async function doOcr(request) { try { var canvas = await loadImage(request.base64Url); // document.body.appendChild(canvas); - ocrData = await useTesseract( canvas, request.lang, @@ -75,7 +72,7 @@ function useTesseract(image, lang, rectangles, mode) { var data = []; var scheduler = await getScheduler(lang, mode); // //ocr on plain image - if (mode == "auto") { + if (mode.includes("auto")) { var d = await scheduler.addJob("recognize", image); data = [d]; @@ -99,32 +96,29 @@ function useTesseract(image, lang, rectangles, mode) { } async function getScheduler(lang, mode) { var id = lang + "_" + mode; - await waitUntil(() => schedulerList[id], { - timeout: WAIT_FOREVER, - }); - return schedulerList[id]; -} -async function loadScheduler(lang, mode) { - var id = lang + "_" + mode; - if (loadingList[id]) { - return; + if (schedulerList[id]) { + return schedulerList[id]; + } else if (loadingList[id]) { + await waitUntil(() => schedulerList[id], { + timeout: WAIT_FOREVER, + }); + return schedulerList[id]; } loadingList[id] = true; var scheduler = Tesseract.createScheduler(); - var workerIndexList = mode == "auto" ? [0] : [0, 1, 2, 3]; + var workerIndexList = mode.includes("auto") ? [0] : [0, 1, 2, 3]; var workerPath = chrome.runtime.getURL("/tesseract/worker.min.js"); var corePath = chrome.runtime.getURL( "/tesseract/tesseract-core-lstm.wasm.js" ); - var tessedit_pageseg_mode = - mode == "auto" - ? Tesseract.PSM.AUTO_ONLY - : lang.includes("vert") - ? Tesseract.PSM.SINGLE_BLOCK_VERT_TEXT - : Tesseract.PSM.SINGLE_BLOCK; + var tessedit_pageseg_mode = mode.includes("auto") + ? Tesseract.PSM.AUTO_ONLY + : lang.includes("vert") + ? Tesseract.PSM.SINGLE_BLOCK_VERT_TEXT + : Tesseract.PSM.SINGLE_BLOCK; await Promise.all( workerIndexList.map(async (i) => { @@ -145,14 +139,7 @@ async function loadScheduler(lang, mode) { ); schedulerList[id] = scheduler; -} - -function initTesseract(request) { - loadScheduler(request.lang, request.mode); - - response({ - windowPostMessageProxy: request.windowPostMessageProxy, - }); + return schedulerList[id]; } function response(data) { diff --git a/src/ocr/ocrView.js b/src/ocr/ocrView.js index e500bf24..d26d44b1 100644 --- a/src/ocr/ocrView.js +++ b/src/ocr/ocrView.js @@ -4,6 +4,7 @@ import * as util from "/src/util"; import { waitUntil, WAIT_FOREVER } from "async-wait-until"; import { WindowPostMessageProxy } from "window-post-message-proxy"; import textfit from "textfit"; +import delay from "delay"; const windowPostMessageProxy = new WindowPostMessageProxy({ suppressWarnings: true, @@ -31,6 +32,8 @@ export async function checkImage(img, setting, keyDownList) { makeLoadingMouseStyle(img); //run both, ocr with opencv rect, ocr without opencv + // auto= no opencv only tesseract + // bbox use opencv and tesseract var base64Url = await getBase64Image(img.src); // var start = new Date().getTime(); await Promise.all([ @@ -58,14 +61,15 @@ async function processOcr(mainUrl, lang, base64Url, img, color, mode = "auto") { } var ratio = 1; var bboxList = [[]]; - await initOCR(lang, mode); + await initOCR(); //ocr process with opencv , then display - if (mode != "auto") { + if (!mode.includes("auto")) { var { bboxList, base64Url, ratio } = await requestSegmentBox( mainUrl, lang, - base64Url + base64Url, + mode ); } @@ -91,10 +95,9 @@ function checkIsImage(ele) { } // create ocr================== -async function initOCR(lang, mode) { +async function initOCR() { await createIframe("opencvFrame", "/opencvHandler.html"); await createIframe("ocrFrame", "/ocr.html"); - requestTesseractInit(lang, mode); } async function createIframe(name, htmlPath) { @@ -128,9 +131,9 @@ async function createIframe(name, htmlPath) { } // request ocr iframe ========== -async function requestSegmentBox(mainUrl, lang, base64Url) { +async function requestSegmentBox(mainUrl, lang, base64Url, mode) { return await postMessage( - { type: "segmentBox", mainUrl, lang, base64Url }, + { type: "segmentBox", mainUrl, lang, base64Url, mode }, iFrames["opencvFrame"] ); } @@ -201,7 +204,7 @@ function getTextBoxList(ocrData) { return textBoxList; } -function createOcrTextBlock(img, textBox, ratio, color) { +async function createOcrTextBlock(img, textBox, ratio, color) { //init bbox var $div = $("
", { class: "ocr_text_div notranslate", @@ -216,13 +219,16 @@ function createOcrTextBlock(img, textBox, ratio, color) { Math.max(0, 100000 - getBboxSize(textBox["bbox"])) + textBox["confidence"]; $div.css("z-index", zIndex); - // position and size + //set box position and szie setLeftTopWH(img, textBox["bbox"], $div, ratio); - textfit($div); $(window).on("resize", (e) => { setLeftTopWH(img, textBox["bbox"], $div, ratio); - textfit($div); + // textfit($div); }); + + //set text font size + await delay(100); + textfit($div, { alignHoriz: true, multiLine: true, reProcess: false }); } function getBboxSize(bbox) { diff --git a/src/ocr/opencvHandler.js b/src/ocr/opencvHandler.js index 8ca9cfb1..669419b3 100644 --- a/src/ocr/opencvHandler.js +++ b/src/ocr/opencvHandler.js @@ -19,6 +19,7 @@ async function segmentBox(request, isResize = true) { var bboxList = []; var base64 = request.base64Url; var ratio = 1; + var mode = request.mode; try { await checkOpencvLoad(); @@ -29,7 +30,7 @@ async function segmentBox(request, isResize = true) { base64 = canvas1.toDataURL(); // get bbox from image - bboxList = detectText(canvas1); + bboxList = detectText(canvas1, mode); } catch (err) { console.log(err); type = "segmentFail"; @@ -91,7 +92,7 @@ function response(data) { } // opencv========================================= -function detectText(canvasIn) { +function detectText(canvasIn, mode) { // https://github.com/qzane/text-detection let src = cv.imread(canvasIn); @@ -99,27 +100,23 @@ function detectText(canvasIn) { var bboxList = []; var w = src.cols; var h = src.rows; + var paddingSize = 10; let contours = new cv.MatVector(); let hierarchy = new cv.Mat(); - let ksize = new cv.Size(10, 10); + var ksize = new cv.Size(10, 10); var element = cv.getStructuringElement(cv.MORPH_RECT, ksize); - var paddingSize = 10; + + // var ksize = new cv.Size(20, 20); + // var element = cv.getStructuringElement(cv.MORPH_ELLIPSE, ksize); + // cv.erode(dst, dst, delement); + // cv.dilate(dst, dst, delement); + // cv.medianBlur(dst, dst, 5); cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY, 0); cv.threshold(dst, dst, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY); cv.Sobel(dst, dst, cv.CV_8U, 1, 0, 1, 1, 0, cv.BORDER_DEFAULT); //x1,y0,ksize3, cv.Sobel(dst, dst, cv.CV_8U, 0, 1, 1, 1, 0, cv.BORDER_DEFAULT); //x1,y0,ksize3, remove straight line - // var blurSize = new cv.Size(3, 3); - // // cv.blur(dst, dst, blurSize); - // cv.GaussianBlur(dst, dst, blurSize, 1.0); - - // var dsize = new cv.Size(3, 3); - // var delement = cv.getStructuringElement(cv.MORPH_RECT, dsize); - // cv.dilate(dst, dst, delement); - // cv.erode(dst, dst, delement); - // cv.medianBlur(dst, dst, 1); - cv.threshold(dst, dst, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY); //remove smooth color diff cv.morphologyEx(dst, dst, cv.MORPH_CLOSE, element); //make bigger for char grouping cv.findContours( @@ -134,7 +131,7 @@ function detectText(canvasIn) { let cnt = contours.get(i); let area = cv.contourArea(cnt); let angle = Math.abs(cv.minAreaRect(cnt).angle); - let isRightAngle = [0, 90, 180, 270].some( + let isRightAngle = [0, 90, 180, 270, 360].some( (x) => Math.abs(x - angle) <= 10.0 ); let rect = cv.boundingRect(cnt); @@ -143,12 +140,13 @@ function detectText(canvasIn) { var width = parseInt(Math.min(rect.width + paddingSize * 2, w - left)); var height = parseInt(Math.min(rect.height + paddingSize * 2, h - top)); var whRatio = Math.max(width / height, height / width); + var r = area / (rect.width * rect.height); // if not sharp, small size, wrong angle, too side pos if ( + r < 0.2 || cnt.rows < 100 || area < 150 || - area > (h / 8) * (w / 8) || !isRightAngle || left == 0 || top == 0 || @@ -174,19 +172,21 @@ function detectText(canvasIn) { } } if (showResult) { - var canvasOut1 = document.createElement("canvas"); - var canvasOut2 = document.createElement("canvas"); - cv.imshow(canvasOut1, src); - document.body.appendChild(canvasOut1); - cv.imshow(canvasOut2, dst); - document.body.appendChild(canvasOut2); + console.log(bboxList.length); + showImage(src); + showImage(dst); } - console.log(bboxList.length); bboxList = sortBbox(bboxList); return bboxList; } +function showImage(cvImage) { + var canvas = document.createElement("canvas"); + cv.imshow(canvas, cvImage); + document.body.appendChild(canvas); +} + function sortBbox(bboxList) { return bboxList.sort((a, b) => { if (a.top < b.top) { @@ -195,25 +195,20 @@ function sortBbox(bboxList) { }); } -function image_resize(src, width, height) { +function image_resize(src, minSize) { var dim; var r; var w = src.cols; var h = src.rows; - // # if both the width and height are None, then return the - // # original image - if (!width && !height) { + if (!minSize) { return src; - } - - // # check to see if the width is None - if (!width) { - r = height / h; - dim = [parseInt(w * r), height]; + } else if (h < w) { + r = minSize / h; + dim = [parseInt(w * r), minSize]; } else { - r = width / w; - dim = [width, parseInt(h * r)]; + r = minSize / w; + dim = [minSize, parseInt(h * r)]; } let dsize = new cv.Size(...dim); diff --git a/src/subtitle/youtube.js b/src/subtitle/youtube.js index fec2e909..0c2c54db 100644 --- a/src/subtitle/youtube.js +++ b/src/subtitle/youtube.js @@ -95,7 +95,9 @@ async function addCaptionButtonListener() { } captionButtonListenerLoaded = true; - await waitUntil(() => $(".ytp-subtitles-button").get(0)); + await waitUntil(() => $(".ytp-subtitles-button").get(0), { + timeout: WAIT_FOREVER, + }); $(".ytp-subtitles-button").on("click", (e) => { handleCaptionOnOff(); @@ -124,11 +126,15 @@ function postFrame(data) { //intercept sub request =================================================================== // check any subtitle request and concat dual sub + +//interceptor api has bug that does not pass skipped one +//need to kill this interceptor when done for above reason function loadInterceptor() { if (interceptorLoaded == true) { return; } interceptorLoaded = true; + killIntercept(); interceptor.apply(); interceptor.on("request", async ({ request, requestId }) => { @@ -242,7 +248,6 @@ const activateCaption = debounce( loadCaption(); // turn on caption for embed video setPlayerCaption(lang, tlang); //turn on caption on specified lang reloadCaption(); //reset previous caption immediately - killIntercept(); } ); diff --git a/src/tts/offscreen.js b/src/tts/offscreen.js index 030f980d..1577bd8a 100644 --- a/src/tts/offscreen.js +++ b/src/tts/offscreen.js @@ -24,11 +24,14 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { function play(url, rate = 1.0, volume = 1.0) { return new Promise((resolve, reject) => { - audio = new Audio(url); + audio = new Audio(); + + audio.onloadeddata = () => audio.play(); + audio.onended = () => resolve(); + audio.preload = "auto"; audio.volume = volume; audio.playbackRate = rate; - audio.onended = () => resolve(); - audio.play(); + audio.src = url; }); } diff --git a/src/util/index.js b/src/util/index.js index abc5f5cf..8953f1b8 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -43,7 +43,7 @@ var defaultData = { voiceRepeat: "1", // exclude langExcludeList: [], - websiteExcludeList: ["*.test.com"], + websiteExcludeList: ["*.example.com"], // remains captionOnStatusByUser: "true", historyList: [], @@ -564,7 +564,7 @@ export function getBrowserTtsVoiceList() { try { // get voice list and sort by remote first // get matched lang voice - chrome.tts.getVoices((voices) => { + browser.tts.getVoices((voices) => { let filtered = voices.filter((e) => { return e.remote != null && e.lang != null && e.voiceName != null; }); //get one that include remote, lang, voiceName @@ -642,6 +642,11 @@ export function caretRangeFromPointOnDocument(x, y) { export function caretRangeFromPointOnShadowDom(x, y) { // get all text from shadows var shadows = getAllShadows(); + + //filter shadow dom by parent position overlap + shadows = shadows.filter((shadow) => checkXYInElement(shadow?.host, x, y)); + + //get all text node var textNodes = shadows .map((shadow) => Array.from(textNodesUnder(shadow))) .flat(); @@ -741,11 +746,15 @@ export function getCharRanges(textNode) { } export function checkXYInElement(ele, x, y) { - var rect = ele.getBoundingClientRect(); //mouse in word rect - if (rect.left > x || rect.right < x || rect.top > y || rect.bottom < y) { + try { + var rect = ele.getBoundingClientRect(); //mouse in word rect + if (rect.left > x || rect.right < x || rect.top > y || rect.bottom < y) { + return false; + } + return true; + } catch (error) { return false; } - return true; } // text util================================== @@ -830,7 +839,7 @@ export function injectScript(scriptUrl) { $("