From 54467df36805eabf9113d1bb84d7434b331cd167 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Sat, 14 Jun 2025 01:40:18 +0530 Subject: Successfully performing OCR on the captcha image and autofilling it for login. --- src/offscreen/ocr/index.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/offscreen/ocr/index.js (limited to 'src/offscreen/ocr/index.js') diff --git a/src/offscreen/ocr/index.js b/src/offscreen/ocr/index.js new file mode 100644 index 0000000..7390bbf --- /dev/null +++ b/src/offscreen/ocr/index.js @@ -0,0 +1,43 @@ +import Tesseract from "./tesseract.esm.min.js"; + +let ocrWorker = null; + +async function performOcrInOffscreen(dataURL) { + if (!ocrWorker) { + ocrWorker = await Tesseract.createWorker("eng", 1, { + workerPath: chrome.runtime.getURL("/offscreen/ocr/worker.min.js"), + corePath: chrome.runtime.getURL("/offscreen/ocr"), + workerBlobURL: false, + }); + + await ocrWorker.setParameters({ + // The only characters that appears + tessedit_char_whitelist: "ABCDEFGHJKLMNPQRSTUVWXYZ23456789", + }); + } + + const { data } = await ocrWorker.recognize(dataURL); + return data; +} + +// Listen for messages from the Service Worker +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.type === "OFFSCREEN_OCR_REQUEST" && message.dataURL) { + performOcrInOffscreen(message.dataURL) + .then(({ text, confidence }) => { + sendResponse({ success: true, text, confidence }); + }) + .catch(error => { + sendResponse({ success: false, error: error.message }); + }); + return true; + } + + if (message.type === "OFFSCREEN_TERMINATE_OCR_WORKER") { + if (ocrWorker) { + ocrWorker.terminate(); + ocrWorker = null; + } + sendResponse({ success: true }); + } +}); -- cgit v1.2.3