-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
126 lines (105 loc) · 3.53 KB
/
Copy pathmain.js
File metadata and controls
126 lines (105 loc) · 3.53 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { app, BrowserWindow, ipcMain, clipboard, globalShortcut, screen, ipcRenderer } = require('electron')
const path = require('path')
const { execSync } = require('child_process')
const { createWorker, } = require('tesseract.js')
let window;
const worker = createWorker({
cachePath: path.join(__dirname, 'lang-data'),
logger: m => {
// performs status update to the ipc renderer.
// console.log(m)
window.webContents.send('dashboard-update', m)
}
});
function createWindow() {
window = new BrowserWindow({
width: 400,
height: 400,
x: 2800,
y: 64,
frame: true,
hasShadow: true,
alwaysOnTop: true,
icon: path.join(__dirname, 'icon.icns'),
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
window.loadFile('index.html')
if (process.env.NODE_ENV === 'debug') window.webContents.openDevTools()
window.on('close', () => {
window = null;
})
}
function isChineseCharacter(value) {
return /[^\x00-\xff]+$/i.test(value)
}
function isSymbolWithLatterSpace(value) {
return /[.;?!,]+/i.test(value)
}
function filterText(r) {
let filtered = ''
for (let i = 0; i < r.length; i++) {
if (i > 0 && i < r.length - 1) {
if (r[i] === ' ' && isChineseCharacter(r[i - 1]) && isChineseCharacter(r[i + 1])) continue
if (r[i] === ' ' && isSymbolWithLatterSpace(r[i + 1])) continue
if (isSymbolWithLatterSpace(r[i]) && r[i + 1] !== ' ') {
filtered += r[i] + ' '
continue
}
}
filtered += r[i]
}
return filtered
}
ipcMain.handle('cap-n-ocr', async (event) => {
console.log('cap N\' OCR')
try {
execSync('screencapture -i -s -c')
console.log('Image recving.')
let imageObject = clipboard.readImage()
console.log('Image recvd, resizing.')
let scaledImageObject = imageObject.resize({
width: imageObject.width * 2
})
console.log('Image resized; encoding...')
let image = scaledImageObject.toDataURL()
console.log('Encoded; Performing OCR...')
window.webContents.send('dashboard-update', { status: 'Decoding... (this might take a while)', unsure: true})
const { data: { text } } = await worker.recognize(image);
console.log('Done.')
let result = filterText(text)
console.log(result)
clipboard.writeText(result)
return result
} catch (e) {
console.log(e)
// probably because the user has canceled the operation.
// destroy the window and ignore.
window.close()
}
})
app.whenReady().then(() => { // make sure the panel is ready for worker to sync status.
createWindow()
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (window === null) {
createWindow()
}
})
}).then(async () => {
window.webContents.send('dashboard-update', { status: 'initializing.', unsure: true})
await worker.load();
await worker.loadLanguage('eng+tha+chi_sim+chi_sim_vert+equ');
await worker.initialize('eng+tha+chi_sim+chi_sim_vert+equ');
globalShortcut.register('Option+Shift+S', () => {
// activate from sleep
if (window !== null) window.close()
createWindow()
setTimeout(()=>window.webContents.send('shortcut-awake'), 500)
})
})