From 64e38ab240a1ca3427592eb80f2cef57fddbdafe Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Fri, 13 Jun 2025 18:55:27 +0530 Subject: Sending OCR request along with captcha image data to the background script. --- src/background/run-ocr.ts | 18 +++++++++++++----- src/scripts/login/index.ts | 3 +-- 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/background/run-ocr.ts b/src/background/run-ocr.ts index d61e71a..f6d1339 100644 --- a/src/background/run-ocr.ts +++ b/src/background/run-ocr.ts @@ -1,7 +1,15 @@ -chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => { - if (message.type === "ping") { - console.log("Received ping from content script"); - console.log(message.data); - sendResponse({ text: "pong" }); +import Tesseract from "tesseract.js"; + +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.type === "OCR") { + (async () => { + const { data: { text, confidence } } = await Tesseract.recognize(message.dataURL, "eng+osd", { + logger: m => console.log(m), + }); + sendResponse({ text, confidence }); + })(); + return true; } + + return false; }); diff --git a/src/scripts/login/index.ts b/src/scripts/login/index.ts index dacc90a..b7e0a73 100644 --- a/src/scripts/login/index.ts +++ b/src/scripts/login/index.ts @@ -1,4 +1,3 @@ -import Tesseract from "tesseract.js"; import { interceptPost, interceptGet } from "@/shared/intercept-jquery-ajax"; function prepareCaptcha(ctx: CanvasRenderingContext2D, width: number, height: number) { @@ -84,7 +83,7 @@ const observer = new MutationObserver((mutationsList, obs) => { prepareCaptcha(ctx, canvas.width, canvas.height); canvas.style.display = ""; const dataURL = canvas.toDataURL("image/png"); - chrome.runtime.sendMessage({ type: "ping", data: "hello background" }).then(console.log); + chrome.runtime.sendMessage({ type: "OCR", dataURL }, console.log); // TODO: Perform OCR // window.postMessage({ // type: "RUN_OCR", -- cgit v1.2.3