@rickcedwhat/playwright-smart-vision 0.2.0 → 0.3.0-next.76bd6bf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/author/apply.d.ts +83 -0
- package/dist/author/apply.d.ts.map +1 -0
- package/dist/author/apply.js +206 -0
- package/dist/author/apply.js.map +1 -0
- package/dist/author/author.test.d.ts +2 -0
- package/dist/author/author.test.d.ts.map +1 -0
- package/dist/author/author.test.js +157 -0
- package/dist/author/author.test.js.map +1 -0
- package/dist/author/catalog.d.ts +19 -0
- package/dist/author/catalog.d.ts.map +1 -0
- package/dist/author/catalog.js +118 -0
- package/dist/author/catalog.js.map +1 -0
- package/dist/author/detect.d.ts +27 -0
- package/dist/author/detect.d.ts.map +1 -0
- package/dist/author/detect.js +246 -0
- package/dist/author/detect.js.map +1 -0
- package/dist/author/geometry.d.ts +55 -0
- package/dist/author/geometry.d.ts.map +1 -0
- package/dist/author/geometry.js +317 -0
- package/dist/author/geometry.js.map +1 -0
- package/dist/author/geometry.test.d.ts +2 -0
- package/dist/author/geometry.test.d.ts.map +1 -0
- package/dist/author/geometry.test.js +263 -0
- package/dist/author/geometry.test.js.map +1 -0
- package/dist/author/index.d.ts +11 -0
- package/dist/author/index.d.ts.map +1 -0
- package/dist/author/index.js +6 -0
- package/dist/author/index.js.map +1 -0
- package/dist/author/labels.d.ts +33 -0
- package/dist/author/labels.d.ts.map +1 -0
- package/dist/author/labels.js +111 -0
- package/dist/author/labels.js.map +1 -0
- package/dist/author/labels.test.d.ts +2 -0
- package/dist/author/labels.test.d.ts.map +1 -0
- package/dist/author/labels.test.js +64 -0
- package/dist/author/labels.test.js.map +1 -0
- package/dist/author/show.d.ts +8 -0
- package/dist/author/show.d.ts.map +1 -0
- package/dist/author/show.js +20 -0
- package/dist/author/show.js.map +1 -0
- package/dist/author/storage.d.ts +3 -0
- package/dist/author/storage.d.ts.map +1 -0
- package/dist/author/storage.js +18 -0
- package/dist/author/storage.js.map +1 -0
- package/dist/configure.d.ts +14 -1
- package/dist/configure.d.ts.map +1 -1
- package/dist/configure.js +12 -1
- package/dist/configure.js.map +1 -1
- package/dist/devtools.d.ts +16 -0
- package/dist/devtools.d.ts.map +1 -1
- package/dist/devtools.js +581 -74
- package/dist/devtools.js.map +1 -1
- package/dist/field-extractor.d.ts +2 -0
- package/dist/field-extractor.d.ts.map +1 -1
- package/dist/field-extractor.js +33 -15
- package/dist/field-extractor.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/ocr-test.d.ts.map +1 -1
- package/dist/ocr-test.js +7 -9
- package/dist/ocr-test.js.map +1 -1
- package/dist/screen-result.d.ts +3 -0
- package/dist/screen-result.d.ts.map +1 -1
- package/dist/screen-result.js +9 -1
- package/dist/screen-result.js.map +1 -1
- package/dist/screen.d.ts +29 -0
- package/dist/screen.d.ts.map +1 -0
- package/dist/screen.js +63 -0
- package/dist/screen.js.map +1 -0
- package/dist/unhover.d.ts +13 -0
- package/dist/unhover.d.ts.map +1 -0
- package/dist/unhover.js +15 -0
- package/dist/unhover.js.map +1 -0
- package/dist/unhover.test.d.ts +2 -0
- package/dist/unhover.test.d.ts.map +1 -0
- package/dist/unhover.test.js +35 -0
- package/dist/unhover.test.js.map +1 -0
- package/dist/utils/ocr.d.ts +12 -0
- package/dist/utils/ocr.d.ts.map +1 -1
- package/dist/utils/ocr.js +52 -4
- package/dist/utils/ocr.js.map +1 -1
- package/dist/utils/vision.d.ts +1 -0
- package/dist/utils/vision.d.ts.map +1 -1
- package/dist/utils/vision.js +31 -1
- package/dist/utils/vision.js.map +1 -1
- package/package.json +5 -1
package/dist/devtools.js
CHANGED
|
@@ -1,4 +1,70 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { getGlobalConfig, loadScreen, writeScreenBuffer } from './configure.js';
|
|
5
|
+
import { FieldExtractor } from './field-extractor.js';
|
|
6
|
+
import { screenDir } from './author/storage.js';
|
|
7
|
+
import { ensureCvReady } from './utils/vision.js';
|
|
8
|
+
import { getOCRUtil } from './utils/ocr.js';
|
|
9
|
+
/** First document-capture listener: type into the name field before Guacamole can steal keys. */
|
|
10
|
+
const DEVTOOLS_KEYS = `(function () {
|
|
11
|
+
if (window.__ocrNameInputShield) return;
|
|
12
|
+
window.__ocrNameInputShield = true;
|
|
13
|
+
|
|
14
|
+
document.addEventListener('keydown', (e) => {
|
|
15
|
+
const input = document.getElementById('__ocr-name-input');
|
|
16
|
+
if (input && e.target === input) {
|
|
17
|
+
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
18
|
+
const start = input.selectionStart ?? input.value.length;
|
|
19
|
+
const end = input.selectionEnd ?? input.value.length;
|
|
20
|
+
if (e.key.length === 1) {
|
|
21
|
+
input.value = input.value.slice(0, start) + e.key + input.value.slice(end);
|
|
22
|
+
input.setSelectionRange(start + 1, start + 1);
|
|
23
|
+
} else if (e.key === 'Backspace') {
|
|
24
|
+
if (start !== end) {
|
|
25
|
+
input.value = input.value.slice(0, start) + input.value.slice(end);
|
|
26
|
+
input.setSelectionRange(start, start);
|
|
27
|
+
} else if (start > 0) {
|
|
28
|
+
input.value = input.value.slice(0, start - 1) + input.value.slice(end);
|
|
29
|
+
input.setSelectionRange(start - 1, start - 1);
|
|
30
|
+
}
|
|
31
|
+
} else if (e.key === 'Enter') {
|
|
32
|
+
document.getElementById('__ocr-modal-save')?.click();
|
|
33
|
+
} else if (e.key === 'Escape') {
|
|
34
|
+
document.getElementById('__ocr-modal-backdrop')?.remove();
|
|
35
|
+
} else {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
e.preventDefault();
|
|
39
|
+
e.stopImmediatePropagation();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (document.getElementById('__ocr-library-backdrop')) {
|
|
44
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'Enter' || e.key === 'Escape') {
|
|
45
|
+
e.preventDefault();
|
|
46
|
+
e.stopImmediatePropagation();
|
|
47
|
+
window.__ocrLibraryKey && window.__ocrLibraryKey(e.key);
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (e.key === 'Escape') {
|
|
53
|
+
if (document.getElementById('__ocr-inspect-overlay')) {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
e.stopImmediatePropagation();
|
|
56
|
+
window.__ocrHideInspect && window.__ocrHideInspect();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const fab = document.getElementById('__ocr-fab');
|
|
60
|
+
if (fab && fab.classList.contains('open')) {
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
e.stopImmediatePropagation();
|
|
63
|
+
fab.classList.remove('open');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}, true);
|
|
67
|
+
})();`;
|
|
2
68
|
const FAB_SCRIPT = `(function () {
|
|
3
69
|
if (window.__ocrDevtools) return;
|
|
4
70
|
window.__ocrDevtools = true;
|
|
@@ -14,49 +80,61 @@ const FAB_SCRIPT = `(function () {
|
|
|
14
80
|
right: 20px;
|
|
15
81
|
z-index: 2147483647;
|
|
16
82
|
font-family: system-ui, sans-serif;
|
|
83
|
+
display: flex;
|
|
84
|
+
flex-direction: column;
|
|
85
|
+
align-items: center;
|
|
86
|
+
gap: 10px;
|
|
17
87
|
}
|
|
18
|
-
#__ocr-fab-btn {
|
|
88
|
+
#__ocr-fab-btn, #__ocr-fab-actions button {
|
|
19
89
|
width: 48px;
|
|
20
90
|
height: 48px;
|
|
91
|
+
min-width: 48px;
|
|
92
|
+
min-height: 48px;
|
|
93
|
+
padding: 0;
|
|
94
|
+
margin: 0;
|
|
95
|
+
box-sizing: border-box;
|
|
21
96
|
border-radius: 50%;
|
|
22
97
|
background: #1a1a2e;
|
|
23
98
|
border: 2px solid #4f46e5;
|
|
24
99
|
color: #fff;
|
|
25
|
-
font-size: 20px;
|
|
26
100
|
cursor: pointer;
|
|
27
101
|
display: flex;
|
|
28
102
|
align-items: center;
|
|
29
103
|
justify-content: center;
|
|
104
|
+
appearance: none;
|
|
105
|
+
-webkit-appearance: none;
|
|
30
106
|
box-shadow: 0 4px 12px rgba(0,0,0,0.4);
|
|
31
|
-
transition: transform 0.15s;
|
|
32
107
|
}
|
|
33
|
-
#__ocr-fab-btn
|
|
34
|
-
#__ocr-fab-
|
|
35
|
-
|
|
36
|
-
bottom: 56px;
|
|
37
|
-
right: 0;
|
|
38
|
-
background: #1a1a2e;
|
|
39
|
-
border: 1px solid #4f46e5;
|
|
40
|
-
border-radius: 8px;
|
|
41
|
-
overflow: hidden;
|
|
42
|
-
box-shadow: 0 4px 16px rgba(0,0,0,0.5);
|
|
108
|
+
#__ocr-fab-btn { font-size: 26px; line-height: 1; transition: transform 0.2s; }
|
|
109
|
+
#__ocr-fab.open #__ocr-fab-btn { transform: rotate(45deg); }
|
|
110
|
+
#__ocr-fab-actions {
|
|
43
111
|
display: none;
|
|
112
|
+
flex-direction: column;
|
|
113
|
+
gap: 10px;
|
|
44
114
|
}
|
|
45
|
-
#__ocr-fab
|
|
46
|
-
#__ocr-fab-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
115
|
+
#__ocr-fab.open #__ocr-fab-actions { display: flex; }
|
|
116
|
+
#__ocr-fab-actions button { width: 40px; height: 40px; min-width: 40px; min-height: 40px; }
|
|
117
|
+
#__ocr-fab-actions button:hover { filter: brightness(1.15); }
|
|
118
|
+
#__ocr-fab-actions button:disabled { opacity: 0.35; cursor: not-allowed; filter: none; }
|
|
119
|
+
#__ocr-fab-overlay.on { background: #3d6d3d; border-color: #5a9; }
|
|
120
|
+
#__ocr-fab-chip {
|
|
121
|
+
position: absolute;
|
|
122
|
+
right: 56px;
|
|
123
|
+
bottom: 12px;
|
|
124
|
+
max-width: 160px;
|
|
125
|
+
overflow: hidden;
|
|
126
|
+
text-overflow: ellipsis;
|
|
55
127
|
white-space: nowrap;
|
|
56
|
-
|
|
128
|
+
background: #1a1a2e;
|
|
129
|
+
color: #cfc;
|
|
130
|
+
border: 1px solid #4f46e5;
|
|
131
|
+
border-radius: 6px;
|
|
132
|
+
padding: 4px 8px;
|
|
133
|
+
font-size: 11px;
|
|
134
|
+
pointer-events: none;
|
|
57
135
|
}
|
|
58
|
-
#__ocr-fab-
|
|
59
|
-
#__ocr-modal-backdrop {
|
|
136
|
+
#__ocr-fab-chip[hidden] { display: none; }
|
|
137
|
+
#__ocr-modal-backdrop, #__ocr-library-backdrop {
|
|
60
138
|
position: fixed;
|
|
61
139
|
inset: 0;
|
|
62
140
|
background: rgba(0,0,0,0.6);
|
|
@@ -64,18 +142,21 @@ const FAB_SCRIPT = `(function () {
|
|
|
64
142
|
display: flex;
|
|
65
143
|
align-items: center;
|
|
66
144
|
justify-content: center;
|
|
145
|
+
font-family: system-ui, sans-serif;
|
|
67
146
|
}
|
|
68
|
-
#__ocr-modal {
|
|
147
|
+
#__ocr-modal, #__ocr-library {
|
|
69
148
|
background: #1a1a2e;
|
|
70
149
|
border: 1px solid #4f46e5;
|
|
71
150
|
border-radius: 12px;
|
|
72
151
|
padding: 20px;
|
|
73
|
-
width: 480px;
|
|
74
|
-
max-width: 90vw;
|
|
75
152
|
box-shadow: 0 8px 32px rgba(0,0,0,0.6);
|
|
76
153
|
font-family: system-ui, sans-serif;
|
|
154
|
+
line-height: normal;
|
|
155
|
+
color: #e2e8f0;
|
|
77
156
|
}
|
|
78
|
-
#__ocr-modal
|
|
157
|
+
#__ocr-modal { width: 480px; max-width: 90vw; }
|
|
158
|
+
#__ocr-library { width: min(860px, 94vw); max-height: 86vh; display: flex; flex-direction: column; }
|
|
159
|
+
#__ocr-modal h3, #__ocr-library h3 { margin: 0 0 12px; color: #e2e8f0; font-size: 15px; }
|
|
79
160
|
#__ocr-modal img { width: 100%; border-radius: 6px; border: 1px solid #4f46e5; margin-bottom: 12px; }
|
|
80
161
|
#__ocr-modal input {
|
|
81
162
|
width: 100%;
|
|
@@ -88,15 +169,54 @@ const FAB_SCRIPT = `(function () {
|
|
|
88
169
|
font-size: 13px;
|
|
89
170
|
margin-bottom: 12px;
|
|
90
171
|
outline: none;
|
|
172
|
+
pointer-events: auto;
|
|
173
|
+
user-select: text;
|
|
174
|
+
-webkit-user-select: text;
|
|
91
175
|
}
|
|
92
176
|
#__ocr-modal input:focus { border-color: #818cf8; }
|
|
93
|
-
#__ocr-modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
|
|
94
|
-
#__ocr-modal-actions button
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
#__ocr-modal-save
|
|
98
|
-
#__ocr-modal-
|
|
99
|
-
#__ocr-modal-
|
|
177
|
+
#__ocr-modal-actions, #__ocr-library-actions { display: flex; gap: 8px; justify-content: flex-end; }
|
|
178
|
+
#__ocr-modal-actions button, #__ocr-library-actions button {
|
|
179
|
+
padding: 7px 16px; border-radius: 6px; border: none; font-size: 13px; cursor: pointer;
|
|
180
|
+
}
|
|
181
|
+
#__ocr-modal-save, #__ocr-library-choose { background: #4f46e5; color: #fff; }
|
|
182
|
+
#__ocr-modal-save:hover, #__ocr-library-choose:hover { background: #6366f1; }
|
|
183
|
+
#__ocr-modal-save:disabled, #__ocr-library-choose:disabled { opacity: 0.5; cursor: default; }
|
|
184
|
+
#__ocr-modal-cancel, #__ocr-library-cancel { background: #2d2d4e; color: #e2e8f0; }
|
|
185
|
+
#__ocr-library-body { display: grid; grid-template-columns: 220px 1fr; gap: 12px; min-height: 280px; margin-bottom: 12px; }
|
|
186
|
+
#__ocr-library-list {
|
|
187
|
+
overflow: auto;
|
|
188
|
+
max-height: 52vh;
|
|
189
|
+
border: 1px solid #333;
|
|
190
|
+
border-radius: 8px;
|
|
191
|
+
background: #111;
|
|
192
|
+
}
|
|
193
|
+
#__ocr-library-list button {
|
|
194
|
+
display: block;
|
|
195
|
+
width: 100%;
|
|
196
|
+
text-align: left;
|
|
197
|
+
background: none;
|
|
198
|
+
border: none;
|
|
199
|
+
border-bottom: 1px solid #2a2a2a;
|
|
200
|
+
color: #ddd;
|
|
201
|
+
padding: 8px 10px;
|
|
202
|
+
cursor: pointer;
|
|
203
|
+
font: 13px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
204
|
+
}
|
|
205
|
+
#__ocr-library-list button:hover { background: #2a2a2a; }
|
|
206
|
+
#__ocr-library-list button.active { background: #243a24; color: #cfc; }
|
|
207
|
+
#__ocr-library-list .muted { color: #888; font-size: 11px; }
|
|
208
|
+
#__ocr-library-preview {
|
|
209
|
+
background: #000;
|
|
210
|
+
border: 1px solid #333;
|
|
211
|
+
border-radius: 8px;
|
|
212
|
+
min-height: 220px;
|
|
213
|
+
display: flex;
|
|
214
|
+
align-items: center;
|
|
215
|
+
justify-content: center;
|
|
216
|
+
overflow: hidden;
|
|
217
|
+
}
|
|
218
|
+
#__ocr-library-preview img { max-width: 100%; max-height: 52vh; display: block; }
|
|
219
|
+
#__ocr-library-empty { color: #888; padding: 16px; font-size: 13px; }
|
|
100
220
|
#__ocr-toast {
|
|
101
221
|
position: fixed;
|
|
102
222
|
bottom: 80px;
|
|
@@ -111,46 +231,181 @@ const FAB_SCRIPT = `(function () {
|
|
|
111
231
|
box-shadow: 0 4px 12px rgba(0,0,0,0.4);
|
|
112
232
|
transition: opacity 0.3s;
|
|
113
233
|
}
|
|
234
|
+
#__ocr-toast.err { background: #4a2020; color: #fcc; }
|
|
235
|
+
#__ocr-inspect-overlay {
|
|
236
|
+
position: fixed;
|
|
237
|
+
inset: 0;
|
|
238
|
+
z-index: 2147483645;
|
|
239
|
+
pointer-events: auto;
|
|
240
|
+
}
|
|
241
|
+
#__ocr-inspect-overlay .hit {
|
|
242
|
+
position: absolute;
|
|
243
|
+
border: 2px solid #6af;
|
|
244
|
+
background: rgba(80,160,255,.12);
|
|
245
|
+
box-sizing: border-box;
|
|
246
|
+
cursor: pointer;
|
|
247
|
+
}
|
|
248
|
+
#__ocr-inspect-overlay .hit.low { border-color: #f66; background: rgba(255,80,80,.12); }
|
|
249
|
+
#__ocr-inspect-overlay .hit.on { outline: 2px solid #ffe08a; z-index: 2; }
|
|
250
|
+
#__ocr-inspect-overlay .part {
|
|
251
|
+
position: absolute;
|
|
252
|
+
border: 1px dashed #9cf;
|
|
253
|
+
pointer-events: none;
|
|
254
|
+
}
|
|
255
|
+
#__ocr-inspect-pop {
|
|
256
|
+
position: absolute;
|
|
257
|
+
z-index: 3;
|
|
258
|
+
width: min(280px, 90vw);
|
|
259
|
+
background: #1c1c1c;
|
|
260
|
+
border: 1px solid #444;
|
|
261
|
+
border-radius: 8px;
|
|
262
|
+
padding: 10px;
|
|
263
|
+
color: #ddd;
|
|
264
|
+
font: 12px/1.35 system-ui, sans-serif;
|
|
265
|
+
pointer-events: none;
|
|
266
|
+
}
|
|
267
|
+
#__ocr-inspect-pop b { display: block; margin-bottom: 4px; color: #fff; }
|
|
114
268
|
\`;
|
|
115
269
|
(document.head || document.documentElement).appendChild(style);
|
|
116
270
|
|
|
117
271
|
const fab = document.createElement('div');
|
|
118
272
|
fab.id = '__ocr-fab';
|
|
119
273
|
fab.innerHTML = \`
|
|
120
|
-
<div id="__ocr-fab-
|
|
121
|
-
<button id="__ocr-
|
|
274
|
+
<div id="__ocr-fab-actions">
|
|
275
|
+
<button id="__ocr-fab-overlay" type="button" disabled title="Choose a screen first">▦</button>
|
|
276
|
+
<button id="__ocr-fab-library" type="button" title="Choose screen">☰</button>
|
|
277
|
+
<button id="__ocr-fab-capture" type="button" title="Capture screen">👁</button>
|
|
122
278
|
</div>
|
|
123
|
-
<button id="__ocr-fab-btn" title="
|
|
279
|
+
<button id="__ocr-fab-btn" title="smart-vision" aria-expanded="false">+</button>
|
|
280
|
+
<div id="__ocr-fab-chip" hidden></div>
|
|
124
281
|
\`;
|
|
125
282
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
283
|
+
function isolateFromPage(el) {
|
|
284
|
+
const types = ['mousedown', 'mouseup', 'click', 'dblclick', 'keydown', 'keyup', 'keypress', 'input', 'pointerdown', 'pointerup', 'compositionstart', 'compositionupdate', 'compositionend'];
|
|
285
|
+
for (const type of types) {
|
|
286
|
+
el.addEventListener(type, (e) => e.stopPropagation());
|
|
287
|
+
}
|
|
288
|
+
}
|
|
129
289
|
|
|
290
|
+
isolateFromPage(fab);
|
|
130
291
|
(document.body || document.documentElement).appendChild(fab);
|
|
131
292
|
|
|
132
293
|
const fabBtn = fab.querySelector('#__ocr-fab-btn');
|
|
133
|
-
const
|
|
134
|
-
const
|
|
294
|
+
const captureBtn = fab.querySelector('#__ocr-fab-capture');
|
|
295
|
+
const libraryBtn = fab.querySelector('#__ocr-fab-library');
|
|
296
|
+
const overlayBtn = fab.querySelector('#__ocr-fab-overlay');
|
|
297
|
+
const chip = fab.querySelector('#__ocr-fab-chip');
|
|
298
|
+
let currentScreen = '';
|
|
299
|
+
let currentHasIndex = false;
|
|
300
|
+
let overlayOn = false;
|
|
301
|
+
let hoverTimer = 0;
|
|
302
|
+
|
|
303
|
+
let openedByHover = false;
|
|
304
|
+
|
|
305
|
+
function setOpen(open) {
|
|
306
|
+
fab.classList.toggle('open', open);
|
|
307
|
+
fabBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
|
|
308
|
+
if (!open) openedByHover = false;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
fabBtn.addEventListener('click', (e) => {
|
|
312
|
+
e.stopPropagation();
|
|
313
|
+
if (openedByHover && fab.classList.contains('open')) {
|
|
314
|
+
openedByHover = false;
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
setOpen(!fab.classList.contains('open'));
|
|
318
|
+
});
|
|
319
|
+
fab.addEventListener('mouseenter', () => {
|
|
320
|
+
clearTimeout(hoverTimer);
|
|
321
|
+
openedByHover = true;
|
|
322
|
+
setOpen(true);
|
|
323
|
+
});
|
|
135
324
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
325
|
+
function syncOverlayBtn() {
|
|
326
|
+
overlayBtn.disabled = !currentHasIndex;
|
|
327
|
+
overlayBtn.title = currentHasIndex
|
|
328
|
+
? (overlayOn ? 'Hide overlay' : 'Show overlay')
|
|
329
|
+
: (currentScreen ? 'Apply this screen in TM v2 first' : 'Choose a screen first');
|
|
330
|
+
overlayBtn.classList.toggle('on', overlayOn);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function setCurrent(name, hasIndex) {
|
|
334
|
+
currentScreen = name || '';
|
|
335
|
+
currentHasIndex = !!hasIndex;
|
|
336
|
+
chip.hidden = !currentScreen;
|
|
337
|
+
chip.textContent = currentScreen;
|
|
338
|
+
if (overlayOn && !currentHasIndex) hideInspect();
|
|
339
|
+
syncOverlayBtn();
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
fab.addEventListener('mouseleave', () => {
|
|
343
|
+
hoverTimer = setTimeout(() => setOpen(false), 280);
|
|
139
344
|
});
|
|
345
|
+
document.addEventListener('mousedown', (e) => {
|
|
346
|
+
if (!fab.classList.contains('open')) return;
|
|
347
|
+
if (fab.contains(e.target)) return;
|
|
348
|
+
if (document.getElementById('__ocr-modal-backdrop')) return;
|
|
349
|
+
if (document.getElementById('__ocr-library-backdrop')) return;
|
|
350
|
+
setOpen(false);
|
|
351
|
+
}, true);
|
|
140
352
|
|
|
141
|
-
captureBtn.addEventListener('click', async () => {
|
|
142
|
-
|
|
143
|
-
|
|
353
|
+
captureBtn.addEventListener('click', async (e) => {
|
|
354
|
+
e.stopPropagation();
|
|
355
|
+
setOpen(false);
|
|
356
|
+
const overlayEl = document.getElementById('__ocr-inspect-overlay');
|
|
357
|
+
if (overlayEl) overlayEl.style.visibility = 'hidden';
|
|
358
|
+
fab.style.visibility = 'hidden';
|
|
144
359
|
let b64;
|
|
145
360
|
try {
|
|
146
361
|
b64 = await window.__ocrCapture();
|
|
147
362
|
} finally {
|
|
148
|
-
fab.style.
|
|
363
|
+
fab.style.visibility = '';
|
|
364
|
+
if (overlayEl) overlayEl.style.visibility = '';
|
|
149
365
|
}
|
|
150
|
-
|
|
366
|
+
showCaptureModal(b64);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
libraryBtn.addEventListener('click', async (e) => {
|
|
370
|
+
e.stopPropagation();
|
|
371
|
+
setOpen(false);
|
|
372
|
+
await showLibraryModal();
|
|
151
373
|
});
|
|
152
374
|
|
|
153
|
-
|
|
375
|
+
overlayBtn.addEventListener('click', async (e) => {
|
|
376
|
+
e.stopPropagation();
|
|
377
|
+
if (overlayBtn.disabled) return;
|
|
378
|
+
if (overlayOn) {
|
|
379
|
+
hideInspect();
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
setOpen(false);
|
|
383
|
+
overlayBtn.disabled = true;
|
|
384
|
+
overlayBtn.title = 'Matching…';
|
|
385
|
+
fab.style.visibility = 'hidden';
|
|
386
|
+
try {
|
|
387
|
+
const hits = await window.__ocrMatchOverlay(currentScreen);
|
|
388
|
+
showInspect(hits);
|
|
389
|
+
showToast('Overlay: ' + currentScreen);
|
|
390
|
+
} catch (err) {
|
|
391
|
+
showToast('Overlay failed: ' + (err && err.message ? err.message : err), true);
|
|
392
|
+
} finally {
|
|
393
|
+
fab.style.visibility = '';
|
|
394
|
+
syncOverlayBtn();
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
function showToast(msg, err) {
|
|
399
|
+
document.getElementById('__ocr-toast')?.remove();
|
|
400
|
+
const t = document.createElement('div');
|
|
401
|
+
t.id = '__ocr-toast';
|
|
402
|
+
if (err) t.className = 'err';
|
|
403
|
+
t.textContent = msg;
|
|
404
|
+
document.body.appendChild(t);
|
|
405
|
+
setTimeout(() => { t.style.opacity = '0'; setTimeout(() => t.remove(), 300); }, 2500);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function showCaptureModal(b64) {
|
|
154
409
|
const backdrop = document.createElement('div');
|
|
155
410
|
backdrop.id = '__ocr-modal-backdrop';
|
|
156
411
|
backdrop.innerHTML = \`
|
|
@@ -159,22 +414,21 @@ const FAB_SCRIPT = `(function () {
|
|
|
159
414
|
<img src="data:image/png;base64,\${b64}" />
|
|
160
415
|
<input id="__ocr-name-input" type="text" placeholder="Screen name (e.g. desktop, customer-info)" />
|
|
161
416
|
<div id="__ocr-modal-actions">
|
|
162
|
-
<button id="__ocr-modal-cancel">Cancel</button>
|
|
163
|
-
<button id="__ocr-modal-save">Save</button>
|
|
417
|
+
<button id="__ocr-modal-cancel" type="button">Cancel</button>
|
|
418
|
+
<button id="__ocr-modal-save" type="button">Save</button>
|
|
164
419
|
</div>
|
|
165
420
|
</div>
|
|
166
421
|
\`;
|
|
167
422
|
document.body.appendChild(backdrop);
|
|
168
|
-
|
|
423
|
+
const modal = backdrop.querySelector('#__ocr-modal');
|
|
169
424
|
const input = backdrop.querySelector('#__ocr-name-input');
|
|
170
425
|
const saveBtn = backdrop.querySelector('#__ocr-modal-save');
|
|
171
|
-
|
|
172
|
-
|
|
426
|
+
isolateFromPage(backdrop);
|
|
427
|
+
isolateFromPage(modal);
|
|
428
|
+
isolateFromPage(input);
|
|
173
429
|
input.focus();
|
|
174
|
-
|
|
175
|
-
cancelBtn.addEventListener('click', () => backdrop.remove());
|
|
430
|
+
backdrop.querySelector('#__ocr-modal-cancel').addEventListener('click', () => backdrop.remove());
|
|
176
431
|
backdrop.addEventListener('click', (e) => { if (e.target === backdrop) backdrop.remove(); });
|
|
177
|
-
|
|
178
432
|
saveBtn.addEventListener('click', async () => {
|
|
179
433
|
const name = input.value.trim();
|
|
180
434
|
if (!name) { input.focus(); return; }
|
|
@@ -183,6 +437,13 @@ const FAB_SCRIPT = `(function () {
|
|
|
183
437
|
try {
|
|
184
438
|
await window.__ocrSave(name, b64);
|
|
185
439
|
backdrop.remove();
|
|
440
|
+
let hasIndex = false;
|
|
441
|
+
try {
|
|
442
|
+
const listed = await window.__ocrListScreens();
|
|
443
|
+
const row = (listed || []).find((s) => s.name === name);
|
|
444
|
+
hasIndex = !!(row && row.hasIndex);
|
|
445
|
+
} catch (_) {}
|
|
446
|
+
setCurrent(name, hasIndex);
|
|
186
447
|
showToast('Saved: ' + name);
|
|
187
448
|
} catch (err) {
|
|
188
449
|
saveBtn.disabled = false;
|
|
@@ -190,20 +451,189 @@ const FAB_SCRIPT = `(function () {
|
|
|
190
451
|
alert('Save failed: ' + err.message);
|
|
191
452
|
}
|
|
192
453
|
});
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async function showLibraryModal() {
|
|
457
|
+
let screens = [];
|
|
458
|
+
try {
|
|
459
|
+
screens = await window.__ocrListScreens() || [];
|
|
460
|
+
} catch (err) {
|
|
461
|
+
showToast('Could not list screens: ' + (err && err.message ? err.message : err), true);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const backdrop = document.createElement('div');
|
|
465
|
+
backdrop.id = '__ocr-library-backdrop';
|
|
466
|
+
const names = screens.map((s) => s.name);
|
|
467
|
+
let selected = currentScreen && names.includes(currentScreen) ? currentScreen : (names[0] || '');
|
|
468
|
+
backdrop.innerHTML = \`
|
|
469
|
+
<div id="__ocr-library">
|
|
470
|
+
<h3>Choose current screen</h3>
|
|
471
|
+
<div id="__ocr-library-body">
|
|
472
|
+
<div id="__ocr-library-list"></div>
|
|
473
|
+
<div id="__ocr-library-preview"><span class="muted">No preview</span></div>
|
|
474
|
+
</div>
|
|
475
|
+
<div id="__ocr-library-actions">
|
|
476
|
+
<button id="__ocr-library-cancel" type="button">Cancel</button>
|
|
477
|
+
<button id="__ocr-library-choose" type="button">Choose current screen</button>
|
|
478
|
+
</div>
|
|
479
|
+
</div>
|
|
480
|
+
\`;
|
|
481
|
+
document.body.appendChild(backdrop);
|
|
482
|
+
isolateFromPage(backdrop);
|
|
483
|
+
isolateFromPage(backdrop.querySelector('#__ocr-library'));
|
|
484
|
+
const listEl = backdrop.querySelector('#__ocr-library-list');
|
|
485
|
+
const previewEl = backdrop.querySelector('#__ocr-library-preview');
|
|
486
|
+
const chooseBtn = backdrop.querySelector('#__ocr-library-choose');
|
|
487
|
+
chooseBtn.disabled = !screens.length;
|
|
488
|
+
|
|
489
|
+
function renderList() {
|
|
490
|
+
if (!screens.length) {
|
|
491
|
+
listEl.innerHTML = '<div id="__ocr-library-empty">No screens in the local cache yet. Capture one first.</div>';
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
listEl.innerHTML = screens.map((s) => (
|
|
495
|
+
'<button type="button" data-name="' + s.name + '" class="' + (s.name === selected ? 'active' : '') + '">' +
|
|
496
|
+
s.name +
|
|
497
|
+
(s.hasIndex ? '' : '<div class="muted">capture only</div>') +
|
|
498
|
+
'</button>'
|
|
499
|
+
)).join('');
|
|
500
|
+
}
|
|
193
501
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
502
|
+
async function showPreview(name) {
|
|
503
|
+
selected = name;
|
|
504
|
+
renderList();
|
|
505
|
+
previewEl.innerHTML = '<span class="muted">Loading…</span>';
|
|
506
|
+
try {
|
|
507
|
+
const b64 = await window.__ocrScreenPreview(name);
|
|
508
|
+
if (!b64) {
|
|
509
|
+
previewEl.innerHTML = '<span class="muted">No blank.png</span>';
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
previewEl.innerHTML = '<img alt="' + name + '" src="data:image/png;base64,' + b64 + '">';
|
|
513
|
+
} catch (err) {
|
|
514
|
+
previewEl.innerHTML = '<span class="muted">' + (err && err.message ? err.message : 'preview failed') + '</span>';
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
listEl.addEventListener('click', (e) => {
|
|
519
|
+
const btn = e.target.closest('button[data-name]');
|
|
520
|
+
if (btn) showPreview(btn.dataset.name);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
window.__ocrLibraryKey = (key) => {
|
|
524
|
+
if (key === 'Escape') {
|
|
525
|
+
backdrop.remove();
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (!screens.length) return;
|
|
529
|
+
const idx = Math.max(0, names.indexOf(selected));
|
|
530
|
+
if (key === 'ArrowDown') showPreview(names[Math.min(names.length - 1, idx + 1)]);
|
|
531
|
+
if (key === 'ArrowUp') showPreview(names[Math.max(0, idx - 1)]);
|
|
532
|
+
if (key === 'Enter') chooseBtn.click();
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
backdrop.querySelector('#__ocr-library-cancel').addEventListener('click', () => backdrop.remove());
|
|
536
|
+
backdrop.addEventListener('click', (e) => { if (e.target === backdrop) backdrop.remove(); });
|
|
537
|
+
chooseBtn.addEventListener('click', () => {
|
|
538
|
+
if (!selected) return;
|
|
539
|
+
const row = screens.find((s) => s.name === selected);
|
|
540
|
+
setCurrent(selected, !!(row && row.hasIndex));
|
|
541
|
+
backdrop.remove();
|
|
542
|
+
showToast('Current screen: ' + selected);
|
|
197
543
|
});
|
|
544
|
+
|
|
545
|
+
if (selected) showPreview(selected);
|
|
546
|
+
else renderList();
|
|
198
547
|
}
|
|
199
548
|
|
|
200
|
-
function
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
549
|
+
function hideInspect() {
|
|
550
|
+
overlayOn = false;
|
|
551
|
+
document.getElementById('__ocr-inspect-overlay')?.remove();
|
|
552
|
+
syncOverlayBtn();
|
|
553
|
+
}
|
|
554
|
+
window.__ocrHideInspect = hideInspect;
|
|
555
|
+
|
|
556
|
+
function showInspect(hits) {
|
|
557
|
+
hideInspect();
|
|
558
|
+
overlayOn = true;
|
|
559
|
+
const root = document.createElement('div');
|
|
560
|
+
root.id = '__ocr-inspect-overlay';
|
|
561
|
+
const dpr = window.devicePixelRatio || 1;
|
|
562
|
+
const shield = (e) => {
|
|
563
|
+
e.preventDefault();
|
|
564
|
+
e.stopPropagation();
|
|
565
|
+
};
|
|
566
|
+
for (const type of ['mousedown', 'mouseup', 'click', 'dblclick', 'pointerdown', 'pointerup', 'contextmenu', 'wheel']) {
|
|
567
|
+
root.addEventListener(type, shield, true);
|
|
568
|
+
}
|
|
569
|
+
let pinned = '';
|
|
570
|
+
const pop = document.createElement('div');
|
|
571
|
+
pop.id = '__ocr-inspect-pop';
|
|
572
|
+
pop.hidden = true;
|
|
573
|
+
root.appendChild(pop);
|
|
574
|
+
|
|
575
|
+
const sorted = (hits || []).slice().sort((a, b) => (a.width * a.height) - (b.width * b.height));
|
|
576
|
+
for (const hit of sorted) {
|
|
577
|
+
const el = document.createElement('div');
|
|
578
|
+
el.className = 'hit' + ((hit.confidence || 0) < 0.7 ? ' low' : '');
|
|
579
|
+
el.dataset.name = hit.name;
|
|
580
|
+
el.style.left = (hit.x / dpr) + 'px';
|
|
581
|
+
el.style.top = (hit.y / dpr) + 'px';
|
|
582
|
+
el.style.width = (hit.width / dpr) + 'px';
|
|
583
|
+
el.style.height = (hit.height / dpr) + 'px';
|
|
584
|
+
for (const part of hit.parts || []) {
|
|
585
|
+
const p = document.createElement('div');
|
|
586
|
+
p.className = 'part';
|
|
587
|
+
p.style.left = ((part.x - hit.x) / dpr) + 'px';
|
|
588
|
+
p.style.top = ((part.y - hit.y) / dpr) + 'px';
|
|
589
|
+
p.style.width = (part.width / dpr) + 'px';
|
|
590
|
+
p.style.height = (part.height / dpr) + 'px';
|
|
591
|
+
el.appendChild(p);
|
|
592
|
+
}
|
|
593
|
+
el.addEventListener('pointerover', (e) => {
|
|
594
|
+
e.stopPropagation();
|
|
595
|
+
if (!pinned) showPop(hit, el);
|
|
596
|
+
});
|
|
597
|
+
el.addEventListener('pointerout', (e) => {
|
|
598
|
+
if (!pinned && !el.contains(e.relatedTarget)) pop.hidden = true;
|
|
599
|
+
});
|
|
600
|
+
el.addEventListener('click', (e) => {
|
|
601
|
+
e.preventDefault();
|
|
602
|
+
e.stopPropagation();
|
|
603
|
+
pinned = pinned === hit.name ? '' : hit.name;
|
|
604
|
+
root.querySelectorAll('.hit').forEach((node) => node.classList.toggle('on', node.dataset.name === pinned));
|
|
605
|
+
if (pinned) showPop(hit, el);
|
|
606
|
+
else pop.hidden = true;
|
|
607
|
+
});
|
|
608
|
+
root.appendChild(el);
|
|
609
|
+
}
|
|
610
|
+
root.addEventListener('click', (e) => {
|
|
611
|
+
if (e.target === root) {
|
|
612
|
+
pinned = '';
|
|
613
|
+
pop.hidden = true;
|
|
614
|
+
root.querySelectorAll('.hit').forEach((node) => node.classList.remove('on'));
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
function showPop(hit, el) {
|
|
618
|
+
pop.hidden = false;
|
|
619
|
+
const conf = hit.confidence == null ? '' : ' · ' + Math.round(hit.confidence * 100) + '%';
|
|
620
|
+
const parts = (hit.parts || []).map((p) => p.name).join(', ');
|
|
621
|
+
pop.innerHTML = '<b>' + hit.name + '</b>' +
|
|
622
|
+
(hit.type || 'element') + conf +
|
|
623
|
+
(parts ? '<div>' + parts + '</div>' : '');
|
|
624
|
+
const r = el.getBoundingClientRect();
|
|
625
|
+
let left = r.right + 8;
|
|
626
|
+
let top = r.top;
|
|
627
|
+
if (left + 280 > window.innerWidth) left = Math.max(8, r.left - 288);
|
|
628
|
+
if (top + 80 > window.innerHeight) top = Math.max(8, window.innerHeight - 90);
|
|
629
|
+
pop.style.left = left + 'px';
|
|
630
|
+
pop.style.top = top + 'px';
|
|
631
|
+
}
|
|
632
|
+
document.documentElement.appendChild(root);
|
|
633
|
+
syncOverlayBtn();
|
|
206
634
|
}
|
|
635
|
+
|
|
636
|
+
syncOverlayBtn();
|
|
207
637
|
}
|
|
208
638
|
|
|
209
639
|
if (document.readyState === 'loading') {
|
|
@@ -212,14 +642,91 @@ const FAB_SCRIPT = `(function () {
|
|
|
212
642
|
init();
|
|
213
643
|
}
|
|
214
644
|
})();`;
|
|
645
|
+
function listDevtoolsScreens() {
|
|
646
|
+
const root = getGlobalConfig().storage?.root;
|
|
647
|
+
if (!root || !fs.existsSync(root))
|
|
648
|
+
return [];
|
|
649
|
+
const out = [];
|
|
650
|
+
for (const ent of fs.readdirSync(root, { withFileTypes: true })) {
|
|
651
|
+
if (!ent.isDirectory() || ent.name.startsWith('.'))
|
|
652
|
+
continue;
|
|
653
|
+
const dir = path.join(root, ent.name);
|
|
654
|
+
if (!fs.existsSync(path.join(dir, 'blank.png')))
|
|
655
|
+
continue;
|
|
656
|
+
out.push({
|
|
657
|
+
name: ent.name,
|
|
658
|
+
hasIndex: fs.existsSync(path.join(dir, 'index.json')),
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
return out.sort((a, b) => a.name.localeCompare(b.name));
|
|
662
|
+
}
|
|
663
|
+
async function matchOverlay(page, name) {
|
|
664
|
+
const screen = loadScreen(name);
|
|
665
|
+
const shot = await page.screenshot({ timeout: 8_000 });
|
|
666
|
+
const tmp = path.join(os.tmpdir(), `ocr-fab-overlay-${Date.now()}.png`);
|
|
667
|
+
fs.writeFileSync(tmp, shot);
|
|
668
|
+
await ensureCvReady();
|
|
669
|
+
const ocr = await getOCRUtil();
|
|
670
|
+
const extractor = new FieldExtractor(ocr, false);
|
|
671
|
+
try {
|
|
672
|
+
await extractor.loadForms(screen.blankScreenPath, tmp);
|
|
673
|
+
const comparison = await extractor.extractElements(screen.elementConfigs, { locateOnly: true });
|
|
674
|
+
return comparison.elements.map((el) => {
|
|
675
|
+
const hit = {
|
|
676
|
+
name: el.name,
|
|
677
|
+
x: el.location.x,
|
|
678
|
+
y: el.location.y,
|
|
679
|
+
width: el.location.width,
|
|
680
|
+
height: el.location.height,
|
|
681
|
+
confidence: el.confidence ?? 0,
|
|
682
|
+
};
|
|
683
|
+
if (el.type)
|
|
684
|
+
hit.type = String(el.type);
|
|
685
|
+
if (el.parts?.length) {
|
|
686
|
+
hit.parts = el.parts.map((part) => ({
|
|
687
|
+
name: part.name,
|
|
688
|
+
x: part.location.x,
|
|
689
|
+
y: part.location.y,
|
|
690
|
+
width: part.location.width,
|
|
691
|
+
height: part.location.height,
|
|
692
|
+
}));
|
|
693
|
+
}
|
|
694
|
+
return hit;
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
finally {
|
|
698
|
+
extractor.cleanup();
|
|
699
|
+
fs.rmSync(tmp, { force: true });
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
async function expose(page, name, fn) {
|
|
703
|
+
try {
|
|
704
|
+
await page.exposeFunction(name, fn);
|
|
705
|
+
}
|
|
706
|
+
catch (err) {
|
|
707
|
+
if (!String(err).includes('already'))
|
|
708
|
+
throw err;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
215
711
|
export async function injectDevtools(page) {
|
|
216
|
-
await page
|
|
712
|
+
await expose(page, '__ocrCapture', async () => {
|
|
217
713
|
const buffer = await page.screenshot({ timeout: 5_000 });
|
|
218
714
|
return buffer.toString('base64');
|
|
219
715
|
});
|
|
220
|
-
await page
|
|
716
|
+
await expose(page, '__ocrSave', async (name, b64) => {
|
|
221
717
|
writeScreenBuffer(name, Buffer.from(b64, 'base64'));
|
|
222
718
|
});
|
|
719
|
+
await expose(page, '__ocrListScreens', async () => listDevtoolsScreens());
|
|
720
|
+
await expose(page, '__ocrScreenPreview', async (name) => {
|
|
721
|
+
const blankPath = path.join(screenDir(name), 'blank.png');
|
|
722
|
+
if (!fs.existsSync(blankPath))
|
|
723
|
+
return null;
|
|
724
|
+
return fs.readFileSync(blankPath).toString('base64');
|
|
725
|
+
});
|
|
726
|
+
await expose(page, '__ocrMatchOverlay', async (name) => matchOverlay(page, name));
|
|
727
|
+
await page.addInitScript(DEVTOOLS_KEYS);
|
|
223
728
|
await page.addInitScript(FAB_SCRIPT);
|
|
729
|
+
await page.evaluate(DEVTOOLS_KEYS);
|
|
730
|
+
await page.evaluate(FAB_SCRIPT);
|
|
224
731
|
}
|
|
225
732
|
//# sourceMappingURL=devtools.js.map
|