aboutsummaryrefslogtreecommitdiffstats
path: root/src/background/run-ocr.ts
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2025-06-14 01:40:18 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2025-06-14 01:40:18 +0530
commit54467df36805eabf9113d1bb84d7434b331cd167 (patch)
treea13b48386e20bf69e7ede0bfff4715c17b90549d /src/background/run-ocr.ts
parent7164ef595147d997068b953d0e8bd19add0db5f9 (diff)
downloadbanglar-bhumi-utils-54467df36805eabf9113d1bb84d7434b331cd167.tar.gz
banglar-bhumi-utils-54467df36805eabf9113d1bb84d7434b331cd167.zip
Successfully performing OCR on the captcha image and autofilling it for login.
Diffstat (limited to 'src/background/run-ocr.ts')
-rw-r--r--src/background/run-ocr.ts65
1 files changed, 32 insertions, 33 deletions
diff --git a/src/background/run-ocr.ts b/src/background/run-ocr.ts
index 1bf605b..4476f54 100644
--- a/src/background/run-ocr.ts
+++ b/src/background/run-ocr.ts
@@ -1,42 +1,41 @@
-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;
-// });
+const OFFSCREEN_DOCUMENT_PATH = "offscreen/ocr/index.html";
+// Function to ensure the offscreen document is open
+async function setupOffscreenDocument() {
+ if (await chrome.offscreen.hasDocument()) {
+ return; // Offscreen document already open
+ }
+ await chrome.offscreen.createDocument({
+ url: OFFSCREEN_DOCUMENT_PATH,
+ reasons: [chrome.offscreen.Reason.CLIPBOARD],
+ justification: "Performing OCR with Tesseract.js in a long-running context."
+ });
+}
+
+// Listen for messages from content scripts (and popup if applicable)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
- if (message.type === "OCR") {
+ if (message.type === "OCR" && message.dataURL) {
(async () => {
- console.log(message);
- const worker = await Tesseract.createWorker("eng", 1, {
- workerBlobURL: false,
- logger: console.log,
- });
-
- await worker.setParameters({
- tessedit_char_whitelist: "ABCDEFGHJKLMNPQRSTUVWXYZ23456789",
- });
+ try {
+ // Ensure the offscreen document is open
+ await setupOffscreenDocument();
- const {
- data: { text, confidence }
- } = await worker.recognize(message.dataURL);
+ // Send the OCR request to the offscreen document
+ const offscreenResponse = await chrome.runtime.sendMessage({
+ type: "OFFSCREEN_OCR_REQUEST",
+ dataURL: message.dataURL
+ });
- await worker.terminate();
-
- sendResponse({ type: "OCR_RESULT", text, confidence });
+ if (offscreenResponse && offscreenResponse.success) {
+ sendResponse({ success: true, text: offscreenResponse.text });
+ } else {
+ sendResponse({ success: false, error: offscreenResponse?.error || "Unknown OCR error" });
+ }
+ } catch (error) {
+ sendResponse({ success: false, error: (error as Error).message });
+ }
})();
-
- return true;
+ return true; // Indicate async response
}
return false;