aboutsummaryrefslogtreecommitdiffstats
path: root/src/background/run-ocr.ts
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2025-06-13 20:11:35 +0000
committerGitHub <noreply@github.com>2025-06-13 20:11:35 +0000
commitca20de409d08ec5abe2d3275bb97cfc5e8c164b3 (patch)
treea13b48386e20bf69e7ede0bfff4715c17b90549d /src/background/run-ocr.ts
parentcaa9c3f705730886cab911cb241703a10494581c (diff)
parent54467df36805eabf9113d1bb84d7434b331cd167 (diff)
downloadbanglar-bhumi-utils-ca20de409d08ec5abe2d3275bb97cfc5e8c164b3.tar.gz
banglar-bhumi-utils-ca20de409d08ec5abe2d3275bb97cfc5e8c164b3.zip
Merge pull request #2 from arpitchakladar/dev/porting-to-firefox
Dev/implementing OCR for captcha autofill
Diffstat (limited to 'src/background/run-ocr.ts')
-rw-r--r--src/background/run-ocr.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/background/run-ocr.ts b/src/background/run-ocr.ts
new file mode 100644
index 0000000..4476f54
--- /dev/null
+++ b/src/background/run-ocr.ts
@@ -0,0 +1,42 @@
+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" && message.dataURL) {
+ (async () => {
+ try {
+ // Ensure the offscreen document is open
+ await setupOffscreenDocument();
+
+ // Send the OCR request to the offscreen document
+ const offscreenResponse = await chrome.runtime.sendMessage({
+ type: "OFFSCREEN_OCR_REQUEST",
+ dataURL: message.dataURL
+ });
+
+ 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; // Indicate async response
+ }
+
+ return false;
+});