From bce4cff7f7144bc91d780e2dd2f8e1114109f407 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Fri, 13 Jun 2025 18:30:50 +0530 Subject: Added background scripts. --- config/webpack/plugins/create-manifest-webpack-plugin/index.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'config/webpack/plugins/create-manifest-webpack-plugin/index.js') diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 8dc0ae4..330c22f 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -97,6 +97,8 @@ class CreateManifestPlugin { ]; } + manifest.background = { service_worker: "background.js" }; + compilation.emitAsset( "manifest.json", new sources.RawSource( -- cgit v1.2.3 From 7164ef595147d997068b953d0e8bd19add0db5f9 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Fri, 13 Jun 2025 23:28:57 +0530 Subject: Using es-modules for webpack configuration. --- config/webpack.config.js | 36 ++++++++++++---------- .../webpack/loader/arrange-shared-module-loader.js | 9 +++--- config/webpack/loader/count-imports-loader.js | 7 ++--- config/webpack/loader/to-string-loader.js | 2 +- ...reate-injected-shared-modules-webpack-plugin.js | 12 ++++---- .../create-manifest-webpack-plugin/index.js | 25 +++++++++------ .../webpack/plugins/create-rules-webpack-plugin.js | 12 ++++---- .../plugins/inject-script-webpack-plugin.js | 14 ++++----- config/webpack/utils/build-file.js | 4 +-- config/webpack/utils/injected-code.js | 4 +-- config/webpack/utils/inline-javascript.js | 2 +- config/webpack/utils/script-runtime.js | 2 +- config/webpack/utils/scripts.js | 12 ++++++-- config/webpack/utils/shared-modules.js | 6 ++-- package.json | 2 ++ src/background/run-ocr.ts | 34 ++++++++++++++++++-- src/offscreen.json | 3 ++ src/offscreen/ocr.ts | 1 + 18 files changed, 116 insertions(+), 71 deletions(-) create mode 100644 src/offscreen.json create mode 100644 src/offscreen/ocr.ts (limited to 'config/webpack/plugins/create-manifest-webpack-plugin/index.js') diff --git a/config/webpack.config.js b/config/webpack.config.js index 1d3b4c0..4b21245 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -1,27 +1,29 @@ -const path = require("path"); -const fs = require("fs"); -const webpack = require("webpack"); -const { merge } = require("webpack-merge"); -const CopyPlugin = require("copy-webpack-plugin"); +import path from "path"; +import fs from "fs"; +import { fileURLToPath } from "url"; +import webpack from "webpack"; +import { merge } from "webpack-merge"; +import CopyPlugin from "copy-webpack-plugin"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); global.ROOT_DIR = path.resolve(__dirname, ".."); global.CONFIG_DIR = path.resolve(ROOT_DIR, "config"); global.SOURCE_DIR = path.resolve(ROOT_DIR, "src"); global.production = process.env.NODE_ENV === "production"; -global.webpackRequire = modulePath => require(path.resolve(CONFIG_DIR, "webpack", modulePath)); - -const CreateManifestPlugin = webpackRequire("plugins/create-manifest-webpack-plugin"); -const CreateRulesPlugin = webpackRequire("plugins/create-rules-webpack-plugin"); -const InjectScriptPlugin = webpackRequire("plugins/inject-script-webpack-plugin"); -const CreateInjectedSharedModulesPlugin = webpackRequire("plugins/create-injected-shared-modules-webpack-plugin"); +import CreateManifestPlugin from "./webpack/plugins/create-manifest-webpack-plugin/index.js"; +import CreateRulesPlugin from "./webpack/plugins/create-rules-webpack-plugin.js"; +import InjectScriptPlugin from "./webpack/plugins/inject-script-webpack-plugin.js"; +import CreateInjectedSharedModulesPlugin from "./webpack/plugins/create-injected-shared-modules-webpack-plugin.js"; -const { inlineJavascript } = webpackRequire("utils/inline-javascript"); -const { getScriptRuntimeFromType } = webpackRequire("utils/script-runtime"); -const { getFileName } = webpackRequire("utils/build-file"); +import { inlineJavascript } from "./webpack/utils/inline-javascript.js"; +import { getScriptRuntimeFromType } from "./webpack/utils/script-runtime.js"; +import { getFileName } from "./webpack/utils/build-file.js"; -const scripts = webpackRequire("utils/scripts"); -const sharedModules = webpackRequire("utils/shared-modules"); +import scripts from "./webpack/utils/scripts.js"; +import sharedModules from "./webpack/utils/shared-modules.js"; const sharedModulesImportedCount = {}; @@ -222,7 +224,7 @@ const sharedModulesConfiguration = merge( } ); -module.exports = [ +export default [ backgroundScriptConfiguration, uninjectedScriptConfiguration, injectedScriptConfiguration, diff --git a/config/webpack/loader/arrange-shared-module-loader.js b/config/webpack/loader/arrange-shared-module-loader.js index 1669a64..2265185 100644 --- a/config/webpack/loader/arrange-shared-module-loader.js +++ b/config/webpack/loader/arrange-shared-module-loader.js @@ -1,10 +1,9 @@ -const path = require("path"); +import path from "path"; +import { ImportManager } from "import-manager"; -const { ImportManager } = require("import-manager"); +import sharedModules from "../utils/shared-modules.js"; -const sharedModules = webpackRequire("utils/shared-modules.js"); - -module.exports = function(source) { +export default function(source) { const currentSharedModuleName = path.basename(this.resourcePath.substring(0, this.resourcePath.length - 3)); if (sharedModules.includes(currentSharedModuleName)) { diff --git a/config/webpack/loader/count-imports-loader.js b/config/webpack/loader/count-imports-loader.js index 4c3faa4..2586332 100644 --- a/config/webpack/loader/count-imports-loader.js +++ b/config/webpack/loader/count-imports-loader.js @@ -1,8 +1,7 @@ -const { ImportManager } = require("import-manager"); +import { ImportManager } from "import-manager"; +import sharedModules from "../utils/shared-modules.js"; -const sharedModules = webpackRequire("utils/shared-modules.js"); - -module.exports = function(source) { +export default function(source) { const { sharedModulesImportedCount } = this.getOptions(); const manager = new ImportManager(source); diff --git a/config/webpack/loader/to-string-loader.js b/config/webpack/loader/to-string-loader.js index cf26b28..248574a 100644 --- a/config/webpack/loader/to-string-loader.js +++ b/config/webpack/loader/to-string-loader.js @@ -1,4 +1,4 @@ -module.exports = function(source) { +export default function(source) { source = source .replaceAll("\\", "\\\\") .replaceAll("`","\\`") diff --git a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js index aa076b8..695d4ef 100644 --- a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js +++ b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js @@ -1,8 +1,8 @@ -const path = require("path"); -const fs = require("fs"); -const { sources } = require("webpack"); +import path from "path"; +import fs from "fs"; +import webpack from "webpack"; -const { getFileName } = webpackRequire("utils/build-file"); +import { getFileName } from "../utils/build-file.js"; class CreateInjectedSharedModulesPlugin { constructor({ injectedSharedModulesImportedCount, sortedSharedModules }) { @@ -29,7 +29,7 @@ class CreateInjectedSharedModulesPlugin { .join(""); compilation.emitAsset( `shared/${getFileName("injected-shared-modules", "shared")}.js`, - new sources.RawSource(injectionCode) + new webpack.sources.RawSource(injectionCode) ); } } @@ -38,4 +38,4 @@ class CreateInjectedSharedModulesPlugin { } } -module.exports = CreateInjectedSharedModulesPlugin; +export default CreateInjectedSharedModulesPlugin; diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 330c22f..2c9ab8e 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -1,10 +1,15 @@ -const { sources } = require("webpack"); -const path = require("path"); - -const { getScriptRuntimeFromType } = webpackRequire("utils/script-runtime"); -const { getFileName } = webpackRequire("utils/build-file"); -const scripts = webpackRequire("utils/scripts"); -const manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json"); +import webpack from "webpack"; +import path from "path"; +import fs from "fs"; + +import { getScriptRuntimeFromType } from "../../utils/script-runtime.js"; +import { getFileName } from "../../utils/build-file.js"; +import scripts from "../../utils/scripts.js"; +const manifest = JSON.parse( + fs.readFileSync( + path.resolve("config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json"), + ), +); class CreateManifestPlugin { constructor({ sharedModulesImportedCount, injectedSharedModulesImportedCount, sortedSharedModules }) { @@ -21,7 +26,7 @@ class CreateManifestPlugin { stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, assets => { - manifest.version = require(path.resolve(ROOT_DIR, "package.json")).version; + manifest.version = JSON.parse(fs.readFileSync((path.resolve(ROOT_DIR, "package.json")))).version; manifest.content_scripts = []; let resources = []; @@ -101,7 +106,7 @@ class CreateManifestPlugin { compilation.emitAsset( "manifest.json", - new sources.RawSource( + new webpack.sources.RawSource( JSON.stringify( manifest, undefined, @@ -117,4 +122,4 @@ class CreateManifestPlugin { } } -module.exports = CreateManifestPlugin; +export default CreateManifestPlugin; diff --git a/config/webpack/plugins/create-rules-webpack-plugin.js b/config/webpack/plugins/create-rules-webpack-plugin.js index 6e31196..5573ddf 100644 --- a/config/webpack/plugins/create-rules-webpack-plugin.js +++ b/config/webpack/plugins/create-rules-webpack-plugin.js @@ -1,6 +1,6 @@ -const path = require("path"); -const fs = require("fs"); -const { sources } = require("webpack"); +import path from "path"; +import fs from "fs"; +import webpack from "webpack"; class CreateRulesPlugin { apply(compiler) { @@ -16,7 +16,7 @@ class CreateRulesPlugin { let ruleId = 1; for (const ruleFileName of ruleFileNames) { - const rule = require(path.resolve(SOURCE_DIR, "rules", ruleFileName)); + const rule = JSON.parse(fs.readFileSync(path.resolve("src/rules", ruleFileName))); if (rule instanceof Array) { for (const currentRule of rule) { @@ -31,7 +31,7 @@ class CreateRulesPlugin { compilation.emitAsset( "rules.json", - new sources.RawSource( + new webpack.sources.RawSource( JSON.stringify( rules, undefined, @@ -47,4 +47,4 @@ class CreateRulesPlugin { } } -module.exports = CreateRulesPlugin; +export default CreateRulesPlugin; diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index 25b1dc6..2b60022 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -1,8 +1,8 @@ -const { sources } = require("webpack"); -const path = require("path"); +import webpack from "webpack"; +import path from "path"; -const { getFileName } = webpackRequire("utils/build-file"); -const { getInjectedCode } = webpackRequire("utils/injected-code"); +import { getFileName } from "../utils/build-file.js"; +import { getInjectedCode } from "../utils/injected-code.js"; class InjectScriptPlugin { apply(compiler) { @@ -20,9 +20,9 @@ class InjectScriptPlugin { const scriptInjectorModuleName = getFileName("script-injector", "shared", true); compilation.updateAsset( assetName, - new sources.RawSource(injectedCodeResponse[0]) + new webpack.sources.RawSource(injectedCodeResponse[0]) ); - assets["scripts/" + path.basename(assetName)] = new sources.RawSource( + assets["scripts/" + path.basename(assetName)] = new webpack.sources.RawSource( `$${scriptInjectorModuleName}.injectScriptHead("scripts/injected/${path.basename(assetName)}", ${injectedCodeResponse[1]});` ); } @@ -33,4 +33,4 @@ class InjectScriptPlugin { } } -module.exports = InjectScriptPlugin; +export default InjectScriptPlugin; diff --git a/config/webpack/utils/build-file.js b/config/webpack/utils/build-file.js index 2ab8d86..6945911 100644 --- a/config/webpack/utils/build-file.js +++ b/config/webpack/utils/build-file.js @@ -1,6 +1,6 @@ -const crypto = require("crypto"); +import crypto from "crypto"; -module.exports.getFileName = (fileName, prefix, justHash = false) => { +export const getFileName = (fileName, prefix, justHash = false) => { let hash = crypto .createHash("md5") .update(`${prefix}-${fileName}`) diff --git a/config/webpack/utils/injected-code.js b/config/webpack/utils/injected-code.js index a4f88ac..a2884f6 100644 --- a/config/webpack/utils/injected-code.js +++ b/config/webpack/utils/injected-code.js @@ -1,4 +1,4 @@ -const crypto = require("crypto"); +import crypto from "crypto"; const getHash = url => crypto @@ -7,7 +7,7 @@ const getHash = url => .digest("hex") .substring(16); -module.exports.getInjectedCode = code => { +export const getInjectedCode = code => { const extensionAssets = {}; let i = 0; diff --git a/config/webpack/utils/inline-javascript.js b/config/webpack/utils/inline-javascript.js index ea344a4..456dc11 100644 --- a/config/webpack/utils/inline-javascript.js +++ b/config/webpack/utils/inline-javascript.js @@ -1 +1 @@ -module.exports.inlineJavascript = code => `data:text/javascript;base64,${Buffer.from(code).toString("base64")}`; +export const inlineJavascript = code => `data:text/javascript;base64,${Buffer.from(code).toString("base64")}`; diff --git a/config/webpack/utils/script-runtime.js b/config/webpack/utils/script-runtime.js index a89d4b8..a7c02f7 100644 --- a/config/webpack/utils/script-runtime.js +++ b/config/webpack/utils/script-runtime.js @@ -1,4 +1,4 @@ -module.exports.getScriptRuntimeFromType = scriptType => { +export const getScriptRuntimeFromType = scriptType => { switch (scriptType) { case "before": case "injected-after": diff --git a/config/webpack/utils/scripts.js b/config/webpack/utils/scripts.js index 427aaab..af58e0a 100644 --- a/config/webpack/utils/scripts.js +++ b/config/webpack/utils/scripts.js @@ -1,5 +1,11 @@ -const path = require("path"); -const scripts = require(path.resolve(SOURCE_DIR, "scripts.json")); +import path from "path"; +import fs from "fs"; + +const scripts = JSON.parse( + fs.readFileSync( + path.resolve("./src/scripts.json"), + ), +); const formattedScripts = {}; @@ -14,4 +20,4 @@ for (const scriptType in scripts) { } } -module.exports = formattedScripts; +export default formattedScripts; diff --git a/config/webpack/utils/shared-modules.js b/config/webpack/utils/shared-modules.js index 1584f0f..2b16fa5 100644 --- a/config/webpack/utils/shared-modules.js +++ b/config/webpack/utils/shared-modules.js @@ -1,6 +1,6 @@ -const path = require("path"); -const fs = require("fs"); +import path from "path"; +import fs from "fs"; -module.exports = fs.readdirSync(path.resolve(SOURCE_DIR, "shared")) +export default fs.readdirSync(path.resolve("src/shared")) .filter(sharedModule => !sharedModule.endsWith("import-shared.js")) .map(sharedModule => sharedModule.substring(0, sharedModule.length - 3)); diff --git a/package.json b/package.json index c45b910..60cb50e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,8 @@ { "name": "banglar-bhumi-utils", "version": "1.1.0", + "mode": "browser", + "type": "module", "description": "A chrome extension to improve functionality of the banglarbhumi.gov.in website.", "main": "dist/index.js", "repository": "https://github.com/arpitchakladar/banglar-bhumi-utils", diff --git a/src/background/run-ocr.ts b/src/background/run-ocr.ts index f6d1339..1bf605b 100644 --- a/src/background/run-ocr.ts +++ b/src/background/run-ocr.ts @@ -1,13 +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; +// }); 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), + console.log(message); + const worker = await Tesseract.createWorker("eng", 1, { + workerBlobURL: false, + logger: console.log, }); - sendResponse({ text, confidence }); + + await worker.setParameters({ + tessedit_char_whitelist: "ABCDEFGHJKLMNPQRSTUVWXYZ23456789", + }); + + const { + data: { text, confidence } + } = await worker.recognize(message.dataURL); + + await worker.terminate(); + + sendResponse({ type: "OCR_RESULT", text, confidence }); })(); + return true; } diff --git a/src/offscreen.json b/src/offscreen.json new file mode 100644 index 0000000..3159170 --- /dev/null +++ b/src/offscreen.json @@ -0,0 +1,3 @@ +[ + "ocr.ts" +] diff --git a/src/offscreen/ocr.ts b/src/offscreen/ocr.ts new file mode 100644 index 0000000..c645b9f --- /dev/null +++ b/src/offscreen/ocr.ts @@ -0,0 +1 @@ +console.log("THIS IS OFFSCREEN DOCUMENT!"); -- cgit v1.2.3 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. --- config/webpack.config.js | 14 +- .../create-manifest-webpack-plugin/index.js | 1 + .../manifest-template.json | 5 +- package.json | 3 - src/background/run-ocr.ts | 65 +++-- src/offscreen.json | 3 - src/offscreen/ocr.ts | 1 - src/offscreen/ocr/index.html | 10 + src/offscreen/ocr/index.js | 43 ++++ src/offscreen/ocr/tesseract-core-lstm.wasm.js | 283 +++++++++++++++++++++ src/offscreen/ocr/tesseract-core-simd-lstm.wasm.js | 283 +++++++++++++++++++++ src/offscreen/ocr/tesseract-core.wasm.js | 282 ++++++++++++++++++++ src/offscreen/ocr/tesseract.esm.min.js | 19 ++ src/offscreen/ocr/worker.min.js | 3 + src/scripts/login/index.ts | 37 ++- static/icon512.png | Bin 16 files changed, 986 insertions(+), 66 deletions(-) delete mode 100644 src/offscreen.json delete mode 100644 src/offscreen/ocr.ts create mode 100644 src/offscreen/ocr/index.html create mode 100644 src/offscreen/ocr/index.js create mode 100644 src/offscreen/ocr/tesseract-core-lstm.wasm.js create mode 100644 src/offscreen/ocr/tesseract-core-simd-lstm.wasm.js create mode 100644 src/offscreen/ocr/tesseract-core.wasm.js create mode 100644 src/offscreen/ocr/tesseract.esm.min.js create mode 100644 src/offscreen/ocr/worker.min.js mode change 100644 => 100755 static/icon512.png (limited to 'config/webpack/plugins/create-manifest-webpack-plugin/index.js') diff --git a/config/webpack.config.js b/config/webpack.config.js index 4b21245..8fd3f15 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -193,10 +193,16 @@ const sharedModulesConfiguration = merge( entry: sharedModuleEntries, plugins: [ new CopyPlugin({ - patterns: [{ - from: path.resolve(ROOT_DIR, "static"), - to: "./" - }] + patterns: [ + { + from: path.resolve("static"), + to: "./", + }, + { + from: path.resolve("src/offscreen"), + to: "./offscreen", + }, + ], }), new CreateInjectedSharedModulesPlugin({ injectedSharedModulesImportedCount, diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 2c9ab8e..6f1e2be 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -123,3 +123,4 @@ class CreateManifestPlugin { } export default CreateManifestPlugin; + diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json b/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json index d1d29f4..52496b6 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json +++ b/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Banglar Bhumi Utils", - "permissions": ["scripting", "declarativeNetRequest"], + "permissions": ["offscreen", "scripting", "declarativeNetRequest"], "description": "A chrome extension to improve functionality of the banglarbhumi.gov.in website.", "icons": { "512": "icon512.png" @@ -12,5 +12,8 @@ "enabled": true, "path": "rules.json" }] + }, + "content_security_policy": { + "extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';" } } diff --git a/package.json b/package.json index 60cb50e..fd60090 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,5 @@ "webpack": "^5.75.0", "webpack-cli": "^5.0.1", "webpack-merge": "^5.8.0" - }, - "dependencies": { - "tesseract.js": "^6.0.1" } } 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; diff --git a/src/offscreen.json b/src/offscreen.json deleted file mode 100644 index 3159170..0000000 --- a/src/offscreen.json +++ /dev/null @@ -1,3 +0,0 @@ -[ - "ocr.ts" -] diff --git a/src/offscreen/ocr.ts b/src/offscreen/ocr.ts deleted file mode 100644 index c645b9f..0000000 --- a/src/offscreen/ocr.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("THIS IS OFFSCREEN DOCUMENT!"); diff --git a/src/offscreen/ocr/index.html b/src/offscreen/ocr/index.html new file mode 100644 index 0000000..ffb3c87 --- /dev/null +++ b/src/offscreen/ocr/index.html @@ -0,0 +1,10 @@ + + +
+ +=e:return[265,e-11,1];case 14>=e:return[266,e-13,1];case 16>=e:return[267,e-15,1];case 18>=e:return[268,e-17,1];case 22>=e:return[269,e-19,2];case 26>=e:return[270,e-23,2];case 30>=e:return[271,e-27,2];case 34>=e:return[272,e-31,2];case 42>=e:return[273,e-35,3];case 50>=e:return[274,e-43,3];case 58>=e:return[275,e-51,3];case 66>=e:return[276,e-59,3];case 82>=e:return[277,e-67,4];case 98>=e:return[278,e-83,4];case 114>=e:return[279,e-99,4];case 130>=e:return[280,e-115,4];case 162>=e:return[281,e-131,5];case 194>=e:return[282,e-163,5];case 226>=e:return[283,e-195,5];case 257>=e:return[284,e-227,5];case 258===e:return[285,e-258,0];default:t("invalid length: "+e)}}var r,n,o=[];for(r=3;258>=r;r++)n=e(r),o[r]=n[2]<<24|n[1]<<16|n[0];return o}(),S=o?new Uint32Array(O):O;function I(e,n){function a(e,r){var n,o,a,s,u=e.N,f=[],c=0;switch(n=S[e.length],f[c++]=65535&n,f[c++]=n>>16&255,f[c++]=n>>24,i){case 1===u:o=[0,u-1,0];break;case 2===u:o=[1,u-2,0];break;case 3===u:o=[2,u-3,0];break;case 4===u:o=[3,u-4,0];break;case 6>=u:o=[4,u-5,1];break;case 8>=u:o=[5,u-7,1];break;case 12>=u:o=[6,u-9,2];break;case 16>=u:o=[7,u-13,2];break;case 24>=u:o=[8,u-17,3];break;case 32>=u:o=[9,u-25,3];break;case 48>=u:o=[10,u-33,4];break;case 64>=u:o=[11,u-49,4];break;case 96>=u:o=[12,u-65,5];break;case 128>=u:o=[13,u-97,5];break;case 192>=u:o=[14,u-129,6];break;case 256>=u:o=[15,u-193,6];break;case 384>=u:o=[16,u-257,7];break;case 512>=u:o=[17,u-385,7];break;case 768>=u:o=[18,u-513,8];break;case 1024>=u:o=[19,u-769,8];break;case 1536>=u:o=[20,u-1025,9];break;case 2048>=u:o=[21,u-1537,9];break;case 3072>=u:o=[22,u-2049,10];break;case 4096>=u:o=[23,u-3073,10];break;case 6144>=u:o=[24,u-4097,11];break;case 8192>=u:o=[25,u-6145,11];break;case 12288>=u:o=[26,u-8193,12];break;case 16384>=u:o=[27,u-12289,12];break;case 24576>=u:o=[28,u-16385,13];break;case 32768>=u:o=[29,u-24577,13];break;default:t("invalid distance")}for(n=o,f[c++]=n[0],f[c++]=n[1],f[c++]=n[2],a=0,s=f.length;a=f;)m[f++]=0;for(f=0;29>=f;)E[f++]=0}for(m[256]=1,s=0,u=n.length;s=u){for(y&&a(y,-1),f=0,c=u-s;f>>=1;return a}function j(t,e){this.input=t,this.b=this.c=0,this.g={},e&&(e.flags&&(this.g=e.flags),"string"==typeof e.filename&&(this.filename=e.filename),"string"==typeof e.comment&&(this.w=e.comment),e.deflateOptions&&(this.l=e.deflateOptions)),this.l||(this.l={})}j.prototype.h=function(){var t,e,n,i,a,s,u,f,c=new(o?Uint8Array:Array)(32768),h=0,l=this.input,y=this.c,d=this.filename,g=this.w;if(c[h++]=31,c[h++]=139,c[h++]=8,t=0,this.g.fname&&(t|=T),this.g.fcomment&&(t|=R),this.g.fhcrc&&(t|=P),c[h++]=t,e=(Date.now?Date.now():+new Date)/1e3|0,c[h++]=255&e,c[h++]=e>>>8&255,c[h++]=e>>>16&255,c[h++]=e>>>24&255,c[h++]=0,c[h++]=B,this.g.fname!==r){for(u=0,f=d.length;u>>0}function st(e,r){var n,i;this.input=e,this.c=0,!r&&(r={})||(r.index&&(this.c=r.index),r.verify&&(this.V=r.verify)),n=e[this.c++],i=e[this.c++],(15&n)===ut?this.method=ut:t(Error("unsupported compression method")),0!=((n<<8)+i)%31&&t(Error("invalid fcheck flag:"+((n<<8)+i)%31)),32&i&&t(Error("fdict flag is not supported")),this.J=new N(e,{index:this.c,bufferSize:r.bufferSize,bufferType:r.bufferType,resize:r.resize})}N.prototype.r=function(t,e){var r=this.a,n=this.b;this.A=t;for(var i,o,a,s,u=r.length-258;256!==(i=it(this,t));)if(256>i)n>=u&&(this.b=n,r=this.f(),n=this.b),r[n++]=i;else for(s=Y[o=i-257],0=u&&(this.b=n,r=this.f(),n=this.b);s--;)r[n]=r[n++-a];for(;8<=this.e;)this.e-=8,this.c--;this.b=n},N.prototype.Q=function(t,e){var r=this.a,n=this.b;this.A=t;for(var i,o,a,s,u=r.length;256!==(i=it(this,t));)if(256>i)n>=u&&(u=(r=this.f()).length),r[n++]=i;else for(s=Y[o=i-257],0
u&&(u=(r=this.f()).length);s--;)r[n]=r[n++-a];for(;8<=this.e;)this.e-=8,this.c--;this.b=n},N.prototype.f=function(){var t,e,r=new(o?Uint8Array:Array)(this.b-32768),n=this.b-32768,i=this.a;if(o)r.set(i.subarray(32768,r.length));else for(t=0,e=r.length;t