@rickcedwhat/playwright-smart-vision 0.3.0-next.9cee38e → 0.3.0-next.ac49cf7

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.
Files changed (58) hide show
  1. package/dist/author/apply.d.ts +15 -0
  2. package/dist/author/apply.d.ts.map +1 -1
  3. package/dist/author/apply.js +52 -5
  4. package/dist/author/apply.js.map +1 -1
  5. package/dist/author/author.test.js +75 -0
  6. package/dist/author/author.test.js.map +1 -1
  7. package/dist/author/catalog.d.ts +10 -4
  8. package/dist/author/catalog.d.ts.map +1 -1
  9. package/dist/author/catalog.js +74 -27
  10. package/dist/author/catalog.js.map +1 -1
  11. package/dist/author/index.d.ts +3 -1
  12. package/dist/author/index.d.ts.map +1 -1
  13. package/dist/author/index.js +2 -1
  14. package/dist/author/index.js.map +1 -1
  15. package/dist/author/options.d.ts +23 -0
  16. package/dist/author/options.d.ts.map +1 -0
  17. package/dist/author/options.js +123 -0
  18. package/dist/author/options.js.map +1 -0
  19. package/dist/configure.d.ts +14 -1
  20. package/dist/configure.d.ts.map +1 -1
  21. package/dist/configure.js +32 -9
  22. package/dist/configure.js.map +1 -1
  23. package/dist/devtools.d.ts +16 -0
  24. package/dist/devtools.d.ts.map +1 -1
  25. package/dist/devtools.js +554 -84
  26. package/dist/devtools.js.map +1 -1
  27. package/dist/element.d.ts +15 -0
  28. package/dist/element.d.ts.map +1 -1
  29. package/dist/element.js +56 -0
  30. package/dist/element.js.map +1 -1
  31. package/dist/field-extractor.d.ts +2 -0
  32. package/dist/field-extractor.d.ts.map +1 -1
  33. package/dist/field-extractor.js +28 -0
  34. package/dist/field-extractor.js.map +1 -1
  35. package/dist/index.d.ts +3 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +2 -0
  38. package/dist/index.js.map +1 -1
  39. package/dist/ocr-test.d.ts.map +1 -1
  40. package/dist/ocr-test.js +7 -9
  41. package/dist/ocr-test.js.map +1 -1
  42. package/dist/screen-result.d.ts +3 -0
  43. package/dist/screen-result.d.ts.map +1 -1
  44. package/dist/screen-result.js +9 -1
  45. package/dist/screen-result.js.map +1 -1
  46. package/dist/screen.d.ts +29 -0
  47. package/dist/screen.d.ts.map +1 -0
  48. package/dist/screen.js +63 -0
  49. package/dist/screen.js.map +1 -0
  50. package/dist/unhover.d.ts +13 -0
  51. package/dist/unhover.d.ts.map +1 -0
  52. package/dist/unhover.js +15 -0
  53. package/dist/unhover.js.map +1 -0
  54. package/dist/unhover.test.d.ts +2 -0
  55. package/dist/unhover.test.d.ts.map +1 -0
  56. package/dist/unhover.test.js +35 -0
  57. package/dist/unhover.test.js.map +1 -0
  58. package/package.json +1 -1
package/dist/devtools.js CHANGED
@@ -1,49 +1,76 @@
1
- import { writeScreenBuffer } from './configure.js';
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';
2
9
  /** First document-capture listener: type into the name field before Guacamole can steal keys. */
3
- const NAME_INPUT_SHIELD = `(function () {
10
+ const DEVTOOLS_KEYS = `(function () {
4
11
  if (window.__ocrNameInputShield) return;
5
12
  window.__ocrNameInputShield = true;
6
13
 
7
14
  document.addEventListener('keydown', (e) => {
8
15
  const input = document.getElementById('__ocr-name-input');
9
- if (!input || e.target !== input) return;
10
- if (e.ctrlKey || e.metaKey || e.altKey) return;
11
-
12
- const start = input.selectionStart ?? input.value.length;
13
- const end = input.selectionEnd ?? input.value.length;
14
-
15
- if (e.key.length === 1) {
16
- input.value = input.value.slice(0, start) + e.key + input.value.slice(end);
17
- input.setSelectionRange(start + 1, start + 1);
18
- } else if (e.key === 'Backspace') {
19
- if (start !== end) {
20
- input.value = input.value.slice(0, start) + input.value.slice(end);
21
- input.setSelectionRange(start, start);
22
- } else if (start > 0) {
23
- input.value = input.value.slice(0, start - 1) + input.value.slice(end);
24
- input.setSelectionRange(start - 1, start - 1);
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);
25
48
  }
26
- } else if (e.key === 'Enter') {
27
- document.getElementById('__ocr-modal-save')?.click();
28
- } else if (e.key === 'Escape') {
29
- document.getElementById('__ocr-modal-backdrop')?.remove();
30
- } else {
31
49
  return;
32
50
  }
33
51
 
34
- e.preventDefault();
35
- e.stopImmediatePropagation();
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
+ }
36
66
  }, true);
37
67
  })();`;
38
68
  const FAB_SCRIPT = `(function () {
39
69
  if (window.__ocrDevtools) return;
40
70
  window.__ocrDevtools = true;
41
71
 
42
- console.log('[OCR devtools] script loaded, readyState:', document.readyState);
43
-
44
72
  function init() {
45
73
  if (document.getElementById('__ocr-fab')) return;
46
- console.log('[OCR devtools] mounting FAB');
47
74
 
48
75
  const style = document.createElement('style');
49
76
  style.textContent = \`
@@ -53,37 +80,61 @@ const FAB_SCRIPT = `(function () {
53
80
  right: 20px;
54
81
  z-index: 2147483647;
55
82
  font-family: system-ui, sans-serif;
83
+ display: flex;
84
+ flex-direction: column;
85
+ align-items: center;
86
+ gap: 10px;
56
87
  }
57
- #__ocr-fab-btn {
88
+ #__ocr-fab-btn, #__ocr-fab-actions button {
58
89
  width: 48px;
59
90
  height: 48px;
60
91
  min-width: 48px;
61
92
  min-height: 48px;
62
- max-width: 48px;
63
- max-height: 48px;
64
93
  padding: 0;
65
94
  margin: 0;
66
95
  box-sizing: border-box;
67
- overflow: hidden;
68
96
  border-radius: 50%;
69
97
  background: #1a1a2e;
70
98
  border: 2px solid #4f46e5;
71
99
  color: #fff;
72
- font-size: 20px;
73
- line-height: 1;
74
100
  cursor: pointer;
75
101
  display: flex;
76
102
  align-items: center;
77
103
  justify-content: center;
78
- flex-shrink: 0;
79
- aspect-ratio: 1;
80
104
  appearance: none;
81
105
  -webkit-appearance: none;
82
106
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
83
- transition: transform 0.15s;
84
107
  }
85
- #__ocr-fab-btn:hover { transform: scale(1.1); }
86
- #__ocr-modal-backdrop {
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 {
111
+ display: none;
112
+ flex-direction: column;
113
+ gap: 10px;
114
+ }
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;
127
+ white-space: nowrap;
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;
135
+ }
136
+ #__ocr-fab-chip[hidden] { display: none; }
137
+ #__ocr-modal-backdrop, #__ocr-library-backdrop {
87
138
  position: fixed;
88
139
  inset: 0;
89
140
  background: rgba(0,0,0,0.6);
@@ -93,18 +144,19 @@ const FAB_SCRIPT = `(function () {
93
144
  justify-content: center;
94
145
  font-family: system-ui, sans-serif;
95
146
  }
96
- #__ocr-modal {
147
+ #__ocr-modal, #__ocr-library {
97
148
  background: #1a1a2e;
98
149
  border: 1px solid #4f46e5;
99
150
  border-radius: 12px;
100
151
  padding: 20px;
101
- width: 480px;
102
- max-width: 90vw;
103
152
  box-shadow: 0 8px 32px rgba(0,0,0,0.6);
104
153
  font-family: system-ui, sans-serif;
105
154
  line-height: normal;
155
+ color: #e2e8f0;
106
156
  }
107
- #__ocr-modal h3 { margin: 0 0 12px; color: #e2e8f0; font-size: 15px; }
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; }
108
160
  #__ocr-modal img { width: 100%; border-radius: 6px; border: 1px solid #4f46e5; margin-bottom: 12px; }
109
161
  #__ocr-modal input {
110
162
  width: 100%;
@@ -122,13 +174,49 @@ const FAB_SCRIPT = `(function () {
122
174
  -webkit-user-select: text;
123
175
  }
124
176
  #__ocr-modal input:focus { border-color: #818cf8; }
125
- #__ocr-modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
126
- #__ocr-modal-actions button { padding: 7px 16px; border-radius: 6px; border: none; font-size: 13px; cursor: pointer; }
127
- #__ocr-modal-save { background: #4f46e5; color: #fff; }
128
- #__ocr-modal-save:hover { background: #6366f1; }
129
- #__ocr-modal-save:disabled { opacity: 0.5; cursor: default; }
130
- #__ocr-modal-cancel { background: #2d2d4e; color: #e2e8f0; }
131
- #__ocr-modal-cancel:hover { background: #3d3d5e; }
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; }
132
220
  #__ocr-toast {
133
221
  position: fixed;
134
222
  bottom: 80px;
@@ -143,42 +231,181 @@ const FAB_SCRIPT = `(function () {
143
231
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
144
232
  transition: opacity 0.3s;
145
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; }
146
268
  \`;
147
269
  (document.head || document.documentElement).appendChild(style);
148
270
 
149
271
  const fab = document.createElement('div');
150
272
  fab.id = '__ocr-fab';
151
273
  fab.innerHTML = \`
152
- <button id="__ocr-fab-btn" title="Capture screen">\u{1F441}</button>
274
+ <div id="__ocr-fab-actions">
275
+ <button id="__ocr-fab-overlay" type="button" disabled title="Choose a screen first">&#9638;</button>
276
+ <button id="__ocr-fab-library" type="button" title="Choose screen">&#9776;</button>
277
+ <button id="__ocr-fab-capture" type="button" title="Capture screen">&#128065;</button>
278
+ </div>
279
+ <button id="__ocr-fab-btn" title="smart-vision" aria-expanded="false">+</button>
280
+ <div id="__ocr-fab-chip" hidden></div>
153
281
  \`;
154
282
 
155
283
  function isolateFromPage(el) {
156
- const types = ['mousedown', 'mouseup', 'click', 'dblclick', 'keydown', 'keyup', 'keypress', 'input', 'compositionstart', 'compositionupdate', 'compositionend'];
284
+ const types = ['mousedown', 'mouseup', 'click', 'dblclick', 'keydown', 'keyup', 'keypress', 'input', 'pointerdown', 'pointerup', 'compositionstart', 'compositionupdate', 'compositionend'];
157
285
  for (const type of types) {
158
286
  el.addEventListener(type, (e) => e.stopPropagation());
159
287
  }
160
288
  }
161
289
 
162
- fab.addEventListener('mousedown', (e) => e.stopPropagation());
163
- fab.addEventListener('mouseup', (e) => e.stopPropagation());
164
- fab.addEventListener('click', (e) => e.stopPropagation());
165
-
290
+ isolateFromPage(fab);
166
291
  (document.body || document.documentElement).appendChild(fab);
167
292
 
168
293
  const fabBtn = fab.querySelector('#__ocr-fab-btn');
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
+ });
169
324
 
170
- fabBtn.addEventListener('click', async () => {
171
- fab.style.display = 'none';
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);
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);
352
+
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';
172
359
  let b64;
173
360
  try {
174
361
  b64 = await window.__ocrCapture();
175
362
  } finally {
176
- fab.style.display = '';
363
+ fab.style.visibility = '';
364
+ if (overlayEl) overlayEl.style.visibility = '';
177
365
  }
178
- showModal(b64);
366
+ showCaptureModal(b64);
367
+ });
368
+
369
+ libraryBtn.addEventListener('click', async (e) => {
370
+ e.stopPropagation();
371
+ setOpen(false);
372
+ await showLibraryModal();
179
373
  });
180
374
 
181
- function showModal(b64) {
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) {
182
409
  const backdrop = document.createElement('div');
183
410
  backdrop.id = '__ocr-modal-backdrop';
184
411
  backdrop.innerHTML = \`
@@ -187,27 +414,21 @@ const FAB_SCRIPT = `(function () {
187
414
  <img src="data:image/png;base64,\${b64}" />
188
415
  <input id="__ocr-name-input" type="text" placeholder="Screen name (e.g. desktop, customer-info)" />
189
416
  <div id="__ocr-modal-actions">
190
- <button id="__ocr-modal-cancel">Cancel</button>
191
- <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>
192
419
  </div>
193
420
  </div>
194
421
  \`;
195
422
  document.body.appendChild(backdrop);
196
-
197
423
  const modal = backdrop.querySelector('#__ocr-modal');
198
424
  const input = backdrop.querySelector('#__ocr-name-input');
199
425
  const saveBtn = backdrop.querySelector('#__ocr-modal-save');
200
- const cancelBtn = backdrop.querySelector('#__ocr-modal-cancel');
201
-
202
426
  isolateFromPage(backdrop);
203
427
  isolateFromPage(modal);
204
428
  isolateFromPage(input);
205
-
206
429
  input.focus();
207
-
208
- cancelBtn.addEventListener('click', () => backdrop.remove());
430
+ backdrop.querySelector('#__ocr-modal-cancel').addEventListener('click', () => backdrop.remove());
209
431
  backdrop.addEventListener('click', (e) => { if (e.target === backdrop) backdrop.remove(); });
210
-
211
432
  saveBtn.addEventListener('click', async () => {
212
433
  const name = input.value.trim();
213
434
  if (!name) { input.focus(); return; }
@@ -216,6 +437,13 @@ const FAB_SCRIPT = `(function () {
216
437
  try {
217
438
  await window.__ocrSave(name, b64);
218
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);
219
447
  showToast('Saved: ' + name);
220
448
  } catch (err) {
221
449
  saveBtn.disabled = false;
@@ -223,20 +451,189 @@ const FAB_SCRIPT = `(function () {
223
451
  alert('Save failed: ' + err.message);
224
452
  }
225
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
+ }
501
+
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
+ };
226
534
 
227
- input.addEventListener('keydown', (e) => {
228
- if (e.key === 'Enter') saveBtn.click();
229
- if (e.key === 'Escape') backdrop.remove();
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);
230
543
  });
544
+
545
+ if (selected) showPreview(selected);
546
+ else renderList();
231
547
  }
232
548
 
233
- function showToast(msg) {
234
- const t = document.createElement('div');
235
- t.id = '__ocr-toast';
236
- t.textContent = msg;
237
- document.body.appendChild(t);
238
- setTimeout(() => { t.style.opacity = '0'; setTimeout(() => t.remove(), 300); }, 2500);
549
+ function hideInspect() {
550
+ overlayOn = false;
551
+ document.getElementById('__ocr-inspect-overlay')?.remove();
552
+ syncOverlayBtn();
239
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();
634
+ }
635
+
636
+ syncOverlayBtn();
240
637
  }
241
638
 
242
639
  if (document.readyState === 'loading') {
@@ -245,18 +642,91 @@ const FAB_SCRIPT = `(function () {
245
642
  init();
246
643
  }
247
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
+ }
248
711
  export async function injectDevtools(page) {
249
- await page.exposeFunction('__ocrCapture', async () => {
712
+ await expose(page, '__ocrCapture', async () => {
250
713
  const buffer = await page.screenshot({ timeout: 5_000 });
251
714
  return buffer.toString('base64');
252
715
  });
253
- await page.exposeFunction('__ocrSave', async (name, b64) => {
716
+ await expose(page, '__ocrSave', async (name, b64) => {
254
717
  writeScreenBuffer(name, Buffer.from(b64, 'base64'));
255
718
  });
256
- // addInitScript covers future navigations; evaluate covers the already-loaded page.
257
- await page.addInitScript(NAME_INPUT_SHIELD);
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);
258
728
  await page.addInitScript(FAB_SCRIPT);
259
- await page.evaluate(NAME_INPUT_SHIELD);
729
+ await page.evaluate(DEVTOOLS_KEYS);
260
730
  await page.evaluate(FAB_SCRIPT);
261
731
  }
262
732
  //# sourceMappingURL=devtools.js.map