diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2025-06-14 01:40:18 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2025-06-14 01:40:18 +0530 |
| commit | 54467df36805eabf9113d1bb84d7434b331cd167 (patch) | |
| tree | a13b48386e20bf69e7ede0bfff4715c17b90549d /src/offscreen/ocr/index.js | |
| parent | 7164ef595147d997068b953d0e8bd19add0db5f9 (diff) | |
| download | banglar-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/offscreen/ocr/index.js')
| -rw-r--r-- | src/offscreen/ocr/index.js | 43 |
1 files changed, 43 insertions, 0 deletions
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 }); + } +}); |
