Skip to content

Commit b5d4952

Browse files
feat: implement OCR client, worker, and types for text detection (#232)
* feat: implement OCR client, worker, and types for text detection - Add ocr-client.ts for managing OCR requests and responses. - Create ocr-types.ts to define types for OCR operations. - Implement ocr-worker.ts for processing image data and detecting text. - Introduce vuejsawait.ts for handling Vue.js settled events. - Update tsconfig.json to include VM.d.ts for type definitions. * fix: update type definitions and improve type safety in various files * chore: re-implement `setTimeout` mock func * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Potential fix for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * fix: hide detected ad elements by setting display to none * fix: TS6 migration --------- Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
1 parent 1514fd3 commit b5d4952

17 files changed

Lines changed: 1144 additions & 525 deletions

builder/package.json

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,24 @@
88
"debug": "tsx source/debug.ts",
99
"clean": "rm -rf dist && rm -rf .buildcache"
1010
},
11-
"dependencies": {
12-
"@types/node": "^24.12.0"
13-
},
1411
"devDependencies": {
15-
"@adguard/agtree": "^4.0.1",
12+
"@adguard/agtree": "^4.0.4",
1613
"@npmcli/package-json": "^7.0.5",
14+
"@types/node": "^24.12.0",
1715
"@types/npmcli__package-json": "^4.0.4",
1816
"@types/semver": "^7.7.1",
19-
"@typescript-eslint/eslint-plugin": "^8.57.0",
20-
"@typescript-eslint/parser": "^8.57.0",
17+
"@typescript-eslint/eslint-plugin": "^8.58.0",
18+
"@typescript-eslint/parser": "^8.58.0",
2119
"@typescriptprime/parsing": "^2.0.0",
2220
"@typescriptprime/securereq": "^1.2.0",
2321
"chokidar": "^5.0.0",
24-
"esbuild": "^0.27.3",
25-
"eslint": "^10.0.3",
22+
"esbuild": "^0.27.4",
23+
"eslint": "^10.1.0",
2624
"semver": "^7.7.4",
27-
"tldts": "^7.0.25",
25+
"tldts": "^7.0.27",
2826
"tsx": "^4.21.0",
29-
"typescript": "^5.9.3",
30-
"typescript-eslint": "^8.57.0",
27+
"typescript": "^6.0.2",
28+
"typescript-eslint": "^8.58.0",
3129
"zod": "^4.3.6"
3230
}
3331
}

builder/source/banner/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function CreateBanner(Options: BannerOptions): string {
2828
BannerString += `// @author ${Options.Author}\n`
2929
BannerString += '//\n'
3030
BannerString += '// @grant unsafeWindow\n'
31+
BannerString += '// @grant GM.xmlHttpRequest\n'
3132
BannerString += '// @run-at document-start\n'
3233
BannerString += '//\n'
3334
BannerString += `// @description ${Options.Description['en']}\n`

builder/source/build.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ESBuild from 'esbuild'
22
import * as Zod from 'zod'
33
import * as Process from 'node:process'
4+
import * as Path from 'node:path'
45
import PackageJson from '@npmcli/package-json'
56
import { CreateBanner } from './banner/index.js'
67
import { SafeInitCwd } from './utils/safe-init-cwd.js'
@@ -44,14 +45,25 @@ export async function Build(OptionsParam?: BuildOptions): Promise<void> {
4445
}
4546
})
4647

48+
const WorkerCode = await ESBuild.build({
49+
entryPoints: [Path.resolve(ProjectRoot, 'userscript', 'source', 'ocr-worker.ts')],
50+
bundle: true,
51+
minify: Options.Minify,
52+
write: false,
53+
target: ['es2024', 'chrome119', 'firefox142', 'safari26']
54+
})
55+
4756
await ESBuild.build({
48-
entryPoints: [ProjectRoot + '/userscript/source/index.ts'],
57+
entryPoints: [Path.resolve(ProjectRoot, 'userscript', 'source', 'index.ts')],
4958
bundle: true,
5059
minify: Options.Minify,
5160
outfile: `${ProjectRoot}/dist/NamuLink${Options.BuildType === 'development' ? '.dev' : ''}.user.js`,
5261
banner: {
5362
js: Banner
5463
},
55-
target: ['es2024', 'chrome119', 'firefox142', 'safari26']
64+
target: ['es2024', 'chrome119', 'firefox142', 'safari26'],
65+
define: {
66+
__OCR_WORKER_CODE__: JSON.stringify(WorkerCode.outputFiles[0].text)
67+
}
5668
})
5769
}

builder/source/debug.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Build } from './build.js'
88
import { SafeInitCwd } from './utils/safe-init-cwd.js'
99

1010
let ProjectRoot = SafeInitCwd({ Cwd: Process.cwd(), InitCwd: Process.env.INIT_CWD })
11-
const WatchingGlob = [];
11+
const WatchingGlob: string[] = [];
1212
['builder/', 'userscript/', ''].forEach(Dir => {
1313
WatchingGlob.push(...Fs.globSync(`${ProjectRoot}/${Dir}source/**/*.ts`))
1414
WatchingGlob.push(...Fs.globSync(`${ProjectRoot}/${Dir}source/**/*.json`))
@@ -18,11 +18,13 @@ const Watcher = Chokidar.watch([...WatchingGlob], {
1818
ignored: '**/node_modules/**',
1919
})
2020

21-
let BuildCooldownTimer: NodeJS.Timeout = null
21+
let BuildCooldownTimer: NodeJS.Timeout | null = null
2222
let ShouldPreventHTTPResponse = false
2323
let Version: number = 0
2424
Watcher.on('all', async (WatcherEvent, WatcherPath) => {
25-
clearTimeout(BuildCooldownTimer)
25+
if (BuildCooldownTimer) {
26+
clearTimeout(BuildCooldownTimer)
27+
}
2628
BuildCooldownTimer = setTimeout(async () => {
2729
console.log(`Detected file change (${WatcherEvent}):`, WatcherPath)
2830
ShouldPreventHTTPResponse = true

builder/source/utils/http-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function RunDebugServer(Port: number, FileName: string[], ShouldPreventHT
2626
Res.writeHead(404)
2727
Res.end()
2828
return
29-
} else if (!IsLoopBack(Req.socket.remoteAddress)) {
29+
} else if (!IsLoopBack(Req.socket.remoteAddress ?? '')) {
3030
Res.writeHead(403)
3131
Res.end()
3232
return

builder/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"extends": "../tsconfig.json",
33
"include": [
44
"source/**/*.ts"
5-
]
5+
],
6+
"compilerOptions": {
7+
"types": ["node"]
8+
}
69
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"builder"
3434
],
3535
"devDependencies": {
36-
"@typescript-eslint/eslint-plugin": "^8.57.0",
37-
"@typescript-eslint/parser": "^8.57.0",
38-
"eslint": "^10.0.3",
39-
"typescript-eslint": "^8.57.0"
36+
"@typescript-eslint/eslint-plugin": "^8.58.0",
37+
"@typescript-eslint/parser": "^8.58.0",
38+
"eslint": "^10.1.0",
39+
"typescript-eslint": "^8.58.0"
4040
}
4141
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"target": "ES2024",
55
"moduleResolution": "NodeNext",
66
"removeComments": false,
7-
"alwaysStrict": false,
87
"skipLibCheck": true,
98
"paths": {
109
"@builder/*": ["./builder/source/*"],

userscript/VM.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@violentmonkey/types'

userscript/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"lint": "tsc --noEmit && eslint **/*.ts"
77
},
88
"devDependencies": {
9-
"@types/web": "^0.0.342",
10-
"@typescript-eslint/eslint-plugin": "^8.57.0",
11-
"@typescript-eslint/parser": "^8.57.0",
12-
"eslint": "^10.0.3",
13-
"typescript-eslint": "^8.57.0"
9+
"@types/web": "^0.0.345",
10+
"@typescript-eslint/eslint-plugin": "^8.58.0",
11+
"@typescript-eslint/parser": "^8.58.0",
12+
"@violentmonkey/types": "^0.3.2",
13+
"eslint": "^10.1.0",
14+
"typescript-eslint": "^8.58.0"
1415
}
1516
}

0 commit comments

Comments
 (0)