diff options
| author | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-07-13 23:32:26 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-13 23:32:26 +0530 |
| commit | ce401d03e75bcbfd1cdfa963954e0807219fb80d (patch) | |
| tree | 52a813c1705a952312c20c328b00fa845a3453f9 /src/background/run-ocr.ts | |
| parent | 98e7019c61ea0ffe344ef7ce809d38d62254a9fc (diff) | |
| parent | dfba7ff6fff7bde33b40a343a2318b35c429b40f (diff) | |
| download | banglar-bhumi-utils-ce401d03e75bcbfd1cdfa963954e0807219fb80d.tar.gz banglar-bhumi-utils-ce401d03e75bcbfd1cdfa963954e0807219fb80d.zip | |
Merge pull request #6 from arpitchakladar/adding-documentation-comments
Adding documentation comments
Diffstat (limited to 'src/background/run-ocr.ts')
| -rw-r--r-- | src/background/run-ocr.ts | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/background/run-ocr.ts b/src/background/run-ocr.ts index 7975fc0..683f701 100644 --- a/src/background/run-ocr.ts +++ b/src/background/run-ocr.ts @@ -1,7 +1,13 @@ +import { OCRRequest, OCRResponse } from "@/shared/ocr-request"; + const OFFSCREEN_DOCUMENT_PATH = "offscreen/ocr/index.html"; // Function to ensure the offscreen document is open -async function setupOffscreenDocument() { +/** + * Ensures the offscreen OCR document is open. If it already exists + * this is a no-op. + */ +async function setupOffscreenDocument(): Promise<void> { if (await chrome.offscreen.hasDocument()) { return; // Offscreen document already open } @@ -13,25 +19,26 @@ async function setupOffscreenDocument() { } // Listen for messages from content scripts (and popup if applicable) -chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { +/** + * Listens for `"OCR"` messages from content scripts. Forwards the + * image data URL to an offscreen document for Tesseract.js processing + * and sends the recognised text back to the caller. + */ +chrome.runtime.onMessage.addListener(function(message: OCRRequest, _sender, sendResponse): boolean { if (message.type === "OCR" && message.dataURL) { - (async () => { + void (async function(): Promise<void> { try { // Ensure the offscreen document is open await setupOffscreenDocument(); // Send the OCR request to the offscreen document - const offscreenResponse = await chrome.runtime.sendMessage({ + const offscreenResponse = await chrome.runtime.sendMessage<OCRRequest, OCRResponse>({ type: "OFFSCREEN_OCR_REQUEST", dataURL: message.dataURL }); - if (offscreenResponse && offscreenResponse.success) { - sendResponse(offscreenResponse); - } else { - sendResponse({ success: false, error: offscreenResponse?.error || "Unknown OCR error" }); - } - } catch (error) { + sendResponse(offscreenResponse); + } catch(error) { sendResponse({ success: false, error: (error as Error).message }); } })(); |
