-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { createWorker } from 'tesseract.js';
import "@logseq/libs"
async function pocr(worker) {
try {
// necessary to have the window focused in order to access the clipboard
window.focus();
const clipboardItem = await navigator.clipboard.read()
if (!clipboardItem) {
console.error('No clipboard item found')
return
}
console.log('Clipboard item: ', clipboardItem)
const data = await clipboardItem[0].getType('image/png').catch(err => {
throw new Error('Clipboard item is not an image')
})
console.log('Clipboard data: ', data)
const ocrResult = await worker.recognize(
data,
{},
{ text: true }
)
console.log('OCR result: ', ocrResult)
return ocrResult.data.text
} catch (err) {
logseq.UI.showMsg('Failed to read image from clipboard: ' + err, 'error')
}
}
const SETTINGS_SCHEMA = [
{
key: 'lang',
type: 'string',
title: 'lang',
default: 'eng',
description: 'The language for OCR. e.g., `chi_sim+eng` means Simplified Chinese & English. Please check [HERE](https://tesseract-ocr.github.io/tessdoc/Data-Files#data-files-for-version-400-november-29-2016) for supported languages'
}
]
async function main() {
logseq.useSettingsSchema(SETTINGS_SCHEMA);
const lang = logseq.settings.lang || 'eng'
const worker = await createWorker();
await worker.loadLanguage(lang);
await worker.initialize(lang);
await worker.setParameters({
preserve_interword_spaces: "1",
});
logseq.Editor.registerSlashCommand(
'OCR image from clipboard',
async () => {
const text = await pocr(worker, lang)
await logseq.Editor.insertAtEditingCursor(text)
},
)
}
// bootstrap
logseq.ready(main).catch(console.error)