@myrialabs/clopen 0.4.20 → 0.4.22
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/README.md +2 -1
- package/backend/artifacts/generate.ts +11 -5
- package/backend/auth/permissions.ts +8 -8
- package/backend/bootstrap.ts +39 -0
- package/backend/chat/stream-manager.ts +15 -0
- package/backend/database/migrations/065_add_session_reasoning_effort.ts +28 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/session-queries.ts +10 -0
- package/backend/engine/README.md +11 -8
- package/backend/engine/adapters/claude/models.ts +46 -23
- package/backend/engine/adapters/claude/stream.ts +18 -4
- package/backend/engine/adapters/codex/error-handler.ts +1 -1
- package/backend/engine/adapters/codex/models.ts +19 -2
- package/backend/engine/adapters/codex/stream.ts +10 -3
- package/backend/engine/adapters/copilot/models.ts +20 -1
- package/backend/engine/adapters/copilot/stream.ts +11 -1
- package/backend/engine/adapters/cursor/models.ts +27 -3
- package/backend/engine/adapters/cursor/stream.ts +23 -3
- package/backend/engine/adapters/pi/models.ts +20 -2
- package/backend/engine/adapters/pi/stream.ts +6 -3
- package/backend/engine/adapters/qwen/error-handler.ts +1 -1
- package/backend/engine/bootstrap-default-engine.ts +18 -5
- package/backend/engine/docs/adapter-contract.md +66 -2
- package/backend/engine/docs/adding-an-engine.md +46 -7
- package/backend/engine/docs/frontend-and-chat.md +80 -17
- package/backend/engine/docs/lessons-learned.md +126 -7
- package/backend/engine/docs/quick-reference.md +12 -3
- package/backend/engine/docs/{system-tools.md → stack.md} +35 -6
- package/backend/engine/docs/websocket-routes.md +54 -15
- package/backend/engine/install-runner.ts +4 -4
- package/backend/engine/resolve-model.ts +87 -0
- package/backend/engine/structured-helpers.test.ts +64 -0
- package/backend/engine/structured-helpers.ts +105 -13
- package/backend/engine/types.ts +6 -0
- package/backend/index.ts +6 -9
- package/backend/mcp/README.md +32 -11
- package/backend/mcp/internal/config.ts +3 -20
- package/backend/mcp/internal/servers/browser-automation/actions/console.ts +65 -0
- package/backend/mcp/internal/servers/browser-automation/actions/dom-analyze.ts +202 -0
- package/backend/mcp/internal/servers/browser-automation/actions/index.ts +31 -0
- package/backend/mcp/internal/servers/browser-automation/actions/input.ts +332 -0
- package/backend/mcp/internal/servers/browser-automation/actions/inspect.ts +158 -0
- package/backend/mcp/internal/servers/browser-automation/actions/navigation.ts +157 -0
- package/backend/mcp/internal/servers/browser-automation/actions/shared.ts +60 -0
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +146 -0
- package/backend/mcp/internal/servers/browser-automation/actions/types.ts +63 -0
- package/backend/mcp/internal/servers/browser-automation/context.ts +82 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +59 -0
- package/backend/mcp/internal/servers/browser-automation/format.ts +56 -0
- package/backend/mcp/internal/servers/browser-automation/index.ts +36 -784
- package/backend/mcp/internal/servers/browser-automation/paths.ts +71 -0
- package/backend/mcp/internal/servers/browser-automation/runner.ts +260 -0
- package/backend/mcp/internal/servers/browser-automation/schema.ts +26 -0
- package/backend/preview/browser/browser-console-manager.ts +310 -156
- package/backend/preview/browser/browser-dialog-handler.ts +45 -16
- package/backend/preview/browser/browser-host-bridge.ts +471 -0
- package/backend/preview/browser/browser-interaction-handler.ts +868 -297
- package/backend/preview/browser/browser-mcp-control.ts +7 -2
- package/backend/preview/browser/browser-native-ui-handler.ts +455 -81
- package/backend/preview/browser/browser-navigation-tracker.ts +56 -0
- package/backend/preview/browser/browser-pool.ts +83 -9
- package/backend/preview/browser/browser-preview-service.ts +432 -53
- package/backend/preview/browser/browser-tab-manager.ts +16 -16
- package/backend/preview/browser/browser-video-capture.ts +1334 -386
- package/backend/preview/browser/capture-profile.ts +213 -0
- package/backend/preview/browser/element-locator.ts +357 -0
- package/backend/preview/browser/frame-targeting.ts +92 -0
- package/backend/preview/browser/scripts/audio-stream.ts +258 -78
- package/backend/preview/browser/scripts/host-bridge.ts +1277 -0
- package/backend/preview/browser/scripts/video-stream.ts +803 -177
- package/backend/preview/browser/types.ts +397 -37
- package/backend/utils/cli.ts +1 -1
- package/backend/utils/path-enrich.ts +2 -2
- package/backend/ws/README.md +1 -1
- package/backend/ws/artifacts/generate.ts +2 -1
- package/backend/ws/chat/stream.ts +38 -2
- package/backend/ws/engine/codex/accounts.ts +1 -1
- package/backend/ws/engine/codex/status.ts +1 -1
- package/backend/ws/git/branch-name.ts +11 -5
- package/backend/ws/git/commit-message.ts +11 -5
- package/backend/ws/index.ts +3 -3
- package/backend/ws/preview/browser/host.ts +95 -0
- package/backend/ws/preview/browser/interact.ts +204 -10
- package/backend/ws/preview/browser/native-ui.ts +31 -1
- package/backend/ws/preview/browser/tab-info.ts +10 -1
- package/backend/ws/preview/browser/tab.ts +43 -0
- package/backend/ws/preview/browser/webcodecs.ts +180 -53
- package/backend/ws/preview/index.ts +58 -1
- package/backend/ws/{system-tools → stack}/access.ts +1 -1
- package/backend/ws/stack/index.ts +16 -0
- package/backend/ws/{system-tools → stack}/install.ts +7 -7
- package/backend/ws/{system-tools → stack}/status.ts +7 -7
- package/backend/ws/system/device-info.ts +290 -0
- package/backend/ws/system/index.ts +2 -0
- package/backend/ws/system/operations.ts +7 -0
- package/backend/ws/user/crud.ts +6 -3
- package/dist/assets/{index-BqBxKeAv.js → index-DH_GO0Lf.js} +1 -1
- package/dist/assets/{index-DdrcY5TE.js → index-igSQ3JaM.js} +1 -1
- package/dist/assets/main-BD3-qPfT.css +1 -0
- package/dist/assets/main-D-d8SFfW.js +616 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js +1 -0
- package/dist/index.html +2 -2
- package/package.json +2 -1
- package/shared/constants/engines.ts +23 -0
- package/shared/types/database/schema.ts +1 -0
- package/shared/types/stores/settings.ts +12 -0
- package/shared/types/unified/engine.ts +33 -0
- package/shared/types/unified/index.ts +2 -0
- package/shared/types/unified/message.ts +6 -0
- package/shared/types/unified/stream.ts +6 -0
- package/backend/mcp/internal/servers/browser-automation/actions.ts +0 -159
- package/backend/mcp/internal/servers/browser-automation/browser.ts +0 -375
- package/backend/mcp/internal/servers/browser-automation/inspection.ts +0 -495
- package/backend/ws/system-tools/index.ts +0 -16
- package/dist/assets/main-BBxAj17T.js +0 -582
- package/dist/assets/main-Cry3pLcl.css +0 -1
|
@@ -1,16 +1,41 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import type { Page } from 'puppeteer';
|
|
2
|
+
import type { Frame, Page } from 'puppeteer';
|
|
3
3
|
import type {
|
|
4
4
|
BrowserTab,
|
|
5
5
|
BrowserSelectInfo,
|
|
6
6
|
BrowserSelectResponse,
|
|
7
7
|
BrowserContextMenuInfo,
|
|
8
8
|
BrowserContextMenuResponse,
|
|
9
|
-
BrowserContextMenuItem
|
|
9
|
+
BrowserContextMenuItem,
|
|
10
|
+
BrowserNativePickerInfo
|
|
10
11
|
} from './types';
|
|
12
|
+
import { resolveFrameTarget } from './frame-targeting';
|
|
11
13
|
import { debug } from '$shared/utils/logger';
|
|
12
14
|
import { nanoid } from 'nanoid';
|
|
13
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Read the current text selection from wherever it actually lives.
|
|
18
|
+
*
|
|
19
|
+
* A selection inside an iframe is invisible to the top document, so asking only
|
|
20
|
+
* the main frame returns an empty string for exactly the pages most likely to
|
|
21
|
+
* be embedding content. `cut` additionally deletes it in the frame that owns it.
|
|
22
|
+
*/
|
|
23
|
+
async function readSelection(page: Page, deleteAfter = false): Promise<string> {
|
|
24
|
+
for (const frame of page.frames()) {
|
|
25
|
+
try {
|
|
26
|
+
const text = await frame.evaluate((remove: boolean) => {
|
|
27
|
+
const selected = window.getSelection()?.toString() ?? '';
|
|
28
|
+
if (selected && remove) document.execCommand('delete');
|
|
29
|
+
return selected;
|
|
30
|
+
}, deleteAfter);
|
|
31
|
+
if (text) return text;
|
|
32
|
+
} catch {
|
|
33
|
+
// Detached or cross-origin mid-navigation; keep looking.
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
|
|
14
39
|
/**
|
|
15
40
|
* Browser Native UI Handler
|
|
16
41
|
*
|
|
@@ -34,7 +59,12 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
34
59
|
// Generate unique select ID
|
|
35
60
|
const selectId = nanoid(10);
|
|
36
61
|
|
|
37
|
-
|
|
62
|
+
// Resolve which frame owns the point first. Evaluating against the main
|
|
63
|
+
// frame would find the `<iframe>` element rather than the select inside
|
|
64
|
+
// it, which is why selects in embedded pages never opened.
|
|
65
|
+
const target = await resolveFrameTarget(page, x, y);
|
|
66
|
+
|
|
67
|
+
const selectData = await target.frame.evaluate((params) => {
|
|
38
68
|
const { x, y, selectId } = params;
|
|
39
69
|
const element = document.elementFromPoint(x, y);
|
|
40
70
|
|
|
@@ -50,17 +80,51 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
50
80
|
|
|
51
81
|
if (!selectElement) return null;
|
|
52
82
|
|
|
83
|
+
// A `size > 1` or `multiple` select renders as an inline list box that
|
|
84
|
+
// the page already paints — it has no popup to stand in for, and
|
|
85
|
+
// intercepting its clicks would break selection outright.
|
|
86
|
+
if (selectElement.multiple || selectElement.size > 1) return null;
|
|
87
|
+
if (selectElement.disabled) return null;
|
|
88
|
+
|
|
53
89
|
// IMPORTANT: Mark this select element with unique ID for later reference
|
|
54
90
|
selectElement.setAttribute('data-puppeteer-select-id', selectId);
|
|
55
91
|
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
92
|
+
// Walk the children rather than `.options`, so `<optgroup>` labels and
|
|
93
|
+
// grouping survive instead of being flattened into a single list.
|
|
94
|
+
const options: Array<{
|
|
95
|
+
index: number;
|
|
96
|
+
value: string;
|
|
97
|
+
text: string;
|
|
98
|
+
selected: boolean;
|
|
99
|
+
disabled?: boolean;
|
|
100
|
+
group?: string;
|
|
101
|
+
groupDisabled?: boolean;
|
|
102
|
+
}> = [];
|
|
103
|
+
|
|
104
|
+
const pushOption = (option: HTMLOptionElement, group?: HTMLOptGroupElement) => {
|
|
105
|
+
options.push({
|
|
106
|
+
index: option.index,
|
|
107
|
+
value: option.value || '',
|
|
108
|
+
text: option.label || option.textContent || '',
|
|
109
|
+
selected: option.selected,
|
|
110
|
+
// An option inside a disabled group is unusable even when the
|
|
111
|
+
// option itself carries no disabled attribute.
|
|
112
|
+
disabled: option.disabled || !!group?.disabled,
|
|
113
|
+
group: group?.label || undefined,
|
|
114
|
+
groupDisabled: group?.disabled || undefined
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
for (const child of Array.from(selectElement.children)) {
|
|
119
|
+
if (child.tagName === 'OPTGROUP') {
|
|
120
|
+
const group = child as HTMLOptGroupElement;
|
|
121
|
+
for (const option of Array.from(group.children)) {
|
|
122
|
+
if (option.tagName === 'OPTION') pushOption(option as HTMLOptionElement, group);
|
|
123
|
+
}
|
|
124
|
+
} else if (child.tagName === 'OPTION') {
|
|
125
|
+
pushOption(child as HTMLOptionElement);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
64
128
|
|
|
65
129
|
// Get bounding box
|
|
66
130
|
const rect = selectElement.getBoundingClientRect();
|
|
@@ -76,15 +140,26 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
76
140
|
selectedIndex: selectElement.selectedIndex,
|
|
77
141
|
boundingBox
|
|
78
142
|
};
|
|
79
|
-
}, { x, y, selectId });
|
|
143
|
+
}, { x: target.x, y: target.y, selectId });
|
|
80
144
|
|
|
81
145
|
if (!selectData) return null;
|
|
146
|
+
|
|
147
|
+
// The box came back in the owning frame's coordinates; shift it back
|
|
148
|
+
// into page space so the overlay lands over the real element.
|
|
149
|
+
const offsetX = x - target.x;
|
|
150
|
+
const offsetY = y - target.y;
|
|
151
|
+
|
|
82
152
|
const selectInfo: any = {
|
|
83
153
|
sessionId, // Internal use only, converted to tabId at previewService layer
|
|
84
154
|
selectId,
|
|
85
155
|
x,
|
|
86
156
|
y,
|
|
87
|
-
boundingBox:
|
|
157
|
+
boundingBox: {
|
|
158
|
+
x: selectData.boundingBox.x + offsetX,
|
|
159
|
+
y: selectData.boundingBox.y + offsetY,
|
|
160
|
+
width: selectData.boundingBox.width,
|
|
161
|
+
height: selectData.boundingBox.height
|
|
162
|
+
},
|
|
88
163
|
options: selectData.options,
|
|
89
164
|
selectedIndex: selectData.selectedIndex,
|
|
90
165
|
timestamp: Date.now()
|
|
@@ -98,24 +173,140 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
98
173
|
}
|
|
99
174
|
}
|
|
100
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Detect a native input whose picker Chrome renders outside the page — a
|
|
178
|
+
* colour swatch or a date field. Those popups are drawn by the browser
|
|
179
|
+
* process and never reach the screencast, so the viewer has to supply one.
|
|
180
|
+
*/
|
|
181
|
+
async checkForNativePicker(
|
|
182
|
+
sessionId: string,
|
|
183
|
+
page: Page,
|
|
184
|
+
x: number,
|
|
185
|
+
y: number
|
|
186
|
+
): Promise<BrowserNativePickerInfo | null> {
|
|
187
|
+
try {
|
|
188
|
+
const pickerId = nanoid(10);
|
|
189
|
+
const target = await resolveFrameTarget(page, x, y);
|
|
190
|
+
|
|
191
|
+
const data = await target.frame.evaluate((params) => {
|
|
192
|
+
const { x, y, pickerId } = params;
|
|
193
|
+
const element = document.elementFromPoint(x, y);
|
|
194
|
+
if (!element || element.tagName !== 'INPUT') return null;
|
|
195
|
+
|
|
196
|
+
const input = element as HTMLInputElement;
|
|
197
|
+
const type = (input.type || '').toLowerCase();
|
|
198
|
+
const PICKER_TYPES = ['color', 'date', 'datetime-local', 'month', 'time', 'week'];
|
|
199
|
+
if (!PICKER_TYPES.includes(type)) return null;
|
|
200
|
+
if (input.disabled || input.readOnly) return null;
|
|
201
|
+
|
|
202
|
+
input.setAttribute('data-clopen-picker-id', pickerId);
|
|
203
|
+
|
|
204
|
+
const rect = input.getBoundingClientRect();
|
|
205
|
+
return {
|
|
206
|
+
inputType: type,
|
|
207
|
+
value: input.value,
|
|
208
|
+
min: input.min || undefined,
|
|
209
|
+
max: input.max || undefined,
|
|
210
|
+
step: input.step || undefined,
|
|
211
|
+
boundingBox: { x: rect.left, y: rect.top, width: rect.width, height: rect.height }
|
|
212
|
+
};
|
|
213
|
+
}, { x: target.x, y: target.y, pickerId });
|
|
214
|
+
|
|
215
|
+
if (!data) return null;
|
|
216
|
+
|
|
217
|
+
const offsetX = x - target.x;
|
|
218
|
+
const offsetY = y - target.y;
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
sessionId,
|
|
222
|
+
pickerId,
|
|
223
|
+
inputType: data.inputType as BrowserNativePickerInfo['inputType'],
|
|
224
|
+
value: data.value,
|
|
225
|
+
min: data.min,
|
|
226
|
+
max: data.max,
|
|
227
|
+
step: data.step,
|
|
228
|
+
boundingBox: {
|
|
229
|
+
x: data.boundingBox.x + offsetX,
|
|
230
|
+
y: data.boundingBox.y + offsetY,
|
|
231
|
+
width: data.boundingBox.width,
|
|
232
|
+
height: data.boundingBox.height
|
|
233
|
+
},
|
|
234
|
+
timestamp: Date.now()
|
|
235
|
+
// sessionId is converted to tabId at the previewService layer, the
|
|
236
|
+
// same handoff the select and context-menu payloads use.
|
|
237
|
+
} as unknown as BrowserNativePickerInfo;
|
|
238
|
+
} catch (error) {
|
|
239
|
+
debug.error('preview', 'Error checking for native picker:', error);
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Write a value chosen in the viewer's own picker back into the page.
|
|
246
|
+
*/
|
|
247
|
+
async handleNativePickerResponse(page: Page, pickerId: string, value: string): Promise<boolean> {
|
|
248
|
+
for (const frame of page.frames()) {
|
|
249
|
+
try {
|
|
250
|
+
const applied = await frame.evaluate(
|
|
251
|
+
(params) => {
|
|
252
|
+
const input = document.querySelector(
|
|
253
|
+
`input[data-clopen-picker-id="${params.pickerId}"]`
|
|
254
|
+
) as HTMLInputElement | null;
|
|
255
|
+
if (!input) return false;
|
|
256
|
+
|
|
257
|
+
input.value = params.value;
|
|
258
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
259
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
260
|
+
input.removeAttribute('data-clopen-picker-id');
|
|
261
|
+
return true;
|
|
262
|
+
},
|
|
263
|
+
{ pickerId, value }
|
|
264
|
+
);
|
|
265
|
+
if (applied) return true;
|
|
266
|
+
} catch {
|
|
267
|
+
// Frame went away or is cross-origin mid-navigation; keep looking.
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
|
|
101
273
|
/**
|
|
102
274
|
* Handle select option selection from frontend
|
|
103
275
|
*/
|
|
104
276
|
async handleSelectResponse(page: Page, response: BrowserSelectResponse): Promise<boolean> {
|
|
105
277
|
const { selectId, selectedIndex } = response;
|
|
106
278
|
|
|
279
|
+
// The marked select may live in any frame, and only the frame that owns it
|
|
280
|
+
// can see the marker — so every frame is asked until one claims it.
|
|
281
|
+
for (const frame of page.frames()) {
|
|
282
|
+
try {
|
|
283
|
+
const applied = await this.applySelectResponse(frame, selectId, selectedIndex);
|
|
284
|
+
if (applied) return true;
|
|
285
|
+
} catch {
|
|
286
|
+
// Detached or cross-origin mid-navigation; try the next frame.
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
debug.warn('preview', `⚠️ Select element ${selectId} not found in any frame`);
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private async applySelectResponse(
|
|
295
|
+
frame: Frame,
|
|
296
|
+
selectId: string,
|
|
297
|
+
selectedIndex: number
|
|
298
|
+
): Promise<boolean> {
|
|
107
299
|
try {
|
|
108
300
|
// Update the select value in the page
|
|
109
|
-
const result = await
|
|
301
|
+
const result = await frame.evaluate((params) => {
|
|
110
302
|
const { selectId, index } = params;
|
|
111
303
|
|
|
112
304
|
// Find the select element by the unique ID we set earlier
|
|
113
305
|
const selectElement = document.querySelector(`select[data-puppeteer-select-id="${selectId}"]`) as HTMLSelectElement;
|
|
114
306
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
307
|
+
// Absent here just means the select lives in a different frame —
|
|
308
|
+
// the caller keeps asking, so this must stay quiet.
|
|
309
|
+
if (!selectElement) return false;
|
|
119
310
|
|
|
120
311
|
if (index < 0 || index >= selectElement.options.length) {
|
|
121
312
|
console.error(`Invalid option index: ${index}`);
|
|
@@ -141,8 +332,6 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
141
332
|
|
|
142
333
|
if (result) {
|
|
143
334
|
debug.log('preview', `✅ Select option updated to index: ${selectedIndex}`);
|
|
144
|
-
} else {
|
|
145
|
-
debug.warn('preview', `⚠️ Failed to update select option to index: ${selectedIndex}`);
|
|
146
335
|
}
|
|
147
336
|
|
|
148
337
|
return result;
|
|
@@ -155,9 +344,20 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
155
344
|
/**
|
|
156
345
|
* Check element at coordinates and build context menu
|
|
157
346
|
*/
|
|
158
|
-
async checkForContextMenu(
|
|
347
|
+
async checkForContextMenu(
|
|
348
|
+
sessionId: string,
|
|
349
|
+
page: Page,
|
|
350
|
+
x: number,
|
|
351
|
+
y: number,
|
|
352
|
+
navigation?: { canGoBack: boolean; canGoForward: boolean }
|
|
353
|
+
): Promise<BrowserContextMenuInfo | null> {
|
|
159
354
|
try {
|
|
160
|
-
|
|
355
|
+
// Same reason as the select path: inside an iframe the main frame only
|
|
356
|
+
// ever reports the `<iframe>` element, so the menu came back describing
|
|
357
|
+
// the frame rather than whatever was actually right-clicked.
|
|
358
|
+
const target = await resolveFrameTarget(page, x, y);
|
|
359
|
+
|
|
360
|
+
const contextData = await target.frame.evaluate((coordinates) => {
|
|
161
361
|
const { x, y } = coordinates;
|
|
162
362
|
const element = document.elementFromPoint(x, y);
|
|
163
363
|
|
|
@@ -165,28 +365,23 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
165
365
|
|
|
166
366
|
// Get element information
|
|
167
367
|
const tagName = element.tagName;
|
|
168
|
-
const
|
|
368
|
+
const anchor = (element.tagName === 'A' ? element : element.closest('a')) as HTMLAnchorElement | null;
|
|
369
|
+
const isLink = !!anchor;
|
|
169
370
|
const isImage = element.tagName === 'IMG';
|
|
170
371
|
const isInput = element.tagName === 'INPUT' || element.tagName === 'TEXTAREA';
|
|
372
|
+
// contenteditable regions accept the same edit commands as a real
|
|
373
|
+
// input, and every rich-text editor on the web is built on them.
|
|
374
|
+
const isEditable = isInput || (element as HTMLElement).isContentEditable;
|
|
375
|
+
|
|
376
|
+
const mediaElement = (
|
|
377
|
+
element.tagName === 'VIDEO' || element.tagName === 'AUDIO' ? element : null
|
|
378
|
+
) as HTMLMediaElement | null;
|
|
171
379
|
|
|
172
380
|
// Check for text selection
|
|
173
381
|
const selection = window.getSelection();
|
|
174
|
-
const
|
|
382
|
+
const selectedText = selection ? selection.toString() : '';
|
|
383
|
+
const isTextSelected = selectedText.length > 0;
|
|
175
384
|
|
|
176
|
-
// Get link URL if it's a link
|
|
177
|
-
let linkUrl: string | undefined;
|
|
178
|
-
if (isLink) {
|
|
179
|
-
const linkElement = element.tagName === 'A' ? element as HTMLAnchorElement : element.closest('a') as HTMLAnchorElement;
|
|
180
|
-
linkUrl = linkElement?.href;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// Get image URL if it's an image
|
|
184
|
-
let imageUrl: string | undefined;
|
|
185
|
-
if (isImage) {
|
|
186
|
-
imageUrl = (element as HTMLImageElement).src;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Get input type
|
|
190
385
|
let inputType: string | undefined;
|
|
191
386
|
if (isInput && element.tagName === 'INPUT') {
|
|
192
387
|
inputType = (element as HTMLInputElement).type;
|
|
@@ -197,17 +392,25 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
197
392
|
isLink,
|
|
198
393
|
isImage,
|
|
199
394
|
isInput,
|
|
395
|
+
isEditable,
|
|
200
396
|
isTextSelected,
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
397
|
+
// Long selections are only ever used for a "search for…" label,
|
|
398
|
+
// so there is no reason to ship the whole thing over the wire.
|
|
399
|
+
selectedText: selectedText.slice(0, 200),
|
|
400
|
+
linkUrl: anchor?.href,
|
|
401
|
+
linkText: anchor?.textContent?.trim().slice(0, 120),
|
|
402
|
+
imageUrl: isImage ? (element as HTMLImageElement).src : undefined,
|
|
403
|
+
mediaUrl: mediaElement?.currentSrc || mediaElement?.src,
|
|
404
|
+
mediaType: mediaElement ? mediaElement.tagName.toLowerCase() : undefined,
|
|
405
|
+
inputType,
|
|
406
|
+
pageUrl: location.href
|
|
204
407
|
};
|
|
205
|
-
}, { x, y });
|
|
408
|
+
}, { x: target.x, y: target.y });
|
|
206
409
|
|
|
207
410
|
if (!contextData) return null;
|
|
208
411
|
|
|
209
412
|
// Build context menu items based on element type
|
|
210
|
-
const items = this.buildContextMenuItems(contextData);
|
|
413
|
+
const items = this.buildContextMenuItems(contextData, navigation);
|
|
211
414
|
|
|
212
415
|
const menuId = nanoid(10);
|
|
213
416
|
const menuInfo: any = {
|
|
@@ -215,6 +418,11 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
215
418
|
menuId,
|
|
216
419
|
x,
|
|
217
420
|
y,
|
|
421
|
+
// Kept so actions that re-hit-test (paste, media controls) can go
|
|
422
|
+
// straight back to the same element instead of re-resolving.
|
|
423
|
+
frameX: target.x,
|
|
424
|
+
frameY: target.y,
|
|
425
|
+
frameUrl: target.frame.url(),
|
|
218
426
|
items,
|
|
219
427
|
elementInfo: contextData,
|
|
220
428
|
timestamp: Date.now()
|
|
@@ -231,55 +439,117 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
231
439
|
/**
|
|
232
440
|
* Build context menu items based on element type
|
|
233
441
|
*/
|
|
234
|
-
private buildContextMenuItems(
|
|
442
|
+
private buildContextMenuItems(
|
|
443
|
+
elementInfo: {
|
|
444
|
+
isLink?: boolean;
|
|
445
|
+
isImage?: boolean;
|
|
446
|
+
isEditable?: boolean;
|
|
447
|
+
isTextSelected?: boolean;
|
|
448
|
+
selectedText?: string;
|
|
449
|
+
linkUrl?: string;
|
|
450
|
+
imageUrl?: string;
|
|
451
|
+
mediaUrl?: string;
|
|
452
|
+
mediaType?: string;
|
|
453
|
+
},
|
|
454
|
+
navigation?: { canGoBack: boolean; canGoForward: boolean }
|
|
455
|
+
): BrowserContextMenuItem[] {
|
|
235
456
|
const items: BrowserContextMenuItem[] = [];
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
457
|
+
let separatorSeq = 0;
|
|
458
|
+
|
|
459
|
+
/** Append a divider, but never two in a row and never a leading one. */
|
|
460
|
+
const divider = () => {
|
|
461
|
+
const last = items[items.length - 1];
|
|
462
|
+
if (!last || last.type === 'separator') return;
|
|
463
|
+
separatorSeq += 1;
|
|
464
|
+
items.push({ id: `separator-${separatorSeq}`, label: '', enabled: false, type: 'separator' });
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// Context-specific actions come first, exactly as a browser orders them:
|
|
468
|
+
// what you right-clicked on matters more than what the page can do.
|
|
246
469
|
if (elementInfo.isLink && elementInfo.linkUrl) {
|
|
247
470
|
items.push(
|
|
248
|
-
{ id: 'open-link-new-tab', label: 'Open Link in New Tab', enabled: true },
|
|
249
|
-
{ id: '
|
|
250
|
-
{ id: '
|
|
471
|
+
{ id: 'open-link-new-tab', label: 'Open Link in New Tab', enabled: true, icon: 'lucide:external-link' },
|
|
472
|
+
{ id: 'open-link-host', label: 'Open Link in Your Browser', enabled: true, icon: 'lucide:app-window' },
|
|
473
|
+
{ id: 'copy-link', label: 'Copy Link Address', enabled: true, icon: 'lucide:link' }
|
|
251
474
|
);
|
|
475
|
+
divider();
|
|
252
476
|
}
|
|
253
477
|
|
|
254
|
-
// Image-specific actions
|
|
255
478
|
if (elementInfo.isImage && elementInfo.imageUrl) {
|
|
256
479
|
items.push(
|
|
257
|
-
{ id: 'open-image-new-tab', label: 'Open Image in New Tab', enabled: true },
|
|
258
|
-
{ id: 'save-image', label: 'Save Image As
|
|
259
|
-
{ id: 'copy-image', label: 'Copy Image', enabled: true },
|
|
260
|
-
{ id: 'copy-image-address', label: 'Copy Image Address', enabled: true }
|
|
261
|
-
{ id: 'separator-3', label: '', enabled: false, type: 'separator' }
|
|
480
|
+
{ id: 'open-image-new-tab', label: 'Open Image in New Tab', enabled: true, icon: 'lucide:image' },
|
|
481
|
+
{ id: 'save-image', label: 'Save Image As…', enabled: true, icon: 'lucide:download' },
|
|
482
|
+
{ id: 'copy-image', label: 'Copy Image', enabled: true, icon: 'lucide:copy' },
|
|
483
|
+
{ id: 'copy-image-address', label: 'Copy Image Address', enabled: true, icon: 'lucide:link' }
|
|
262
484
|
);
|
|
485
|
+
divider();
|
|
263
486
|
}
|
|
264
487
|
|
|
265
|
-
|
|
266
|
-
if (elementInfo.isTextSelected) {
|
|
488
|
+
if (elementInfo.mediaUrl) {
|
|
267
489
|
items.push(
|
|
268
|
-
{ id: '
|
|
269
|
-
{ id: '
|
|
490
|
+
{ id: 'media-play-pause', label: 'Play / Pause', enabled: true, icon: 'lucide:play' },
|
|
491
|
+
{ id: 'media-mute', label: 'Mute / Unmute', enabled: true, icon: 'lucide:volume-2' },
|
|
492
|
+
{ id: 'media-loop', label: 'Loop', enabled: true, icon: 'lucide:repeat' },
|
|
493
|
+
{
|
|
494
|
+
id: 'open-media-new-tab',
|
|
495
|
+
label: `Open ${elementInfo.mediaType === 'audio' ? 'Audio' : 'Video'} in New Tab`,
|
|
496
|
+
enabled: true,
|
|
497
|
+
icon: 'lucide:external-link'
|
|
498
|
+
}
|
|
270
499
|
);
|
|
500
|
+
divider();
|
|
271
501
|
}
|
|
272
502
|
|
|
273
|
-
|
|
274
|
-
|
|
503
|
+
if (elementInfo.isEditable) {
|
|
504
|
+
items.push(
|
|
505
|
+
{ id: 'undo', label: 'Undo', enabled: true, icon: 'lucide:undo-2' },
|
|
506
|
+
{ id: 'redo', label: 'Redo', enabled: true, icon: 'lucide:redo-2' }
|
|
507
|
+
);
|
|
508
|
+
divider();
|
|
509
|
+
items.push(
|
|
510
|
+
{ id: 'cut', label: 'Cut', enabled: !!elementInfo.isTextSelected, icon: 'lucide:scissors' },
|
|
511
|
+
{ id: 'copy', label: 'Copy', enabled: !!elementInfo.isTextSelected, icon: 'lucide:copy' },
|
|
512
|
+
{ id: 'paste', label: 'Paste', enabled: true, icon: 'lucide:clipboard' },
|
|
513
|
+
{ id: 'paste-plain', label: 'Paste as Plain Text', enabled: true, icon: 'lucide:clipboard-type' },
|
|
514
|
+
{ id: 'delete', label: 'Delete', enabled: !!elementInfo.isTextSelected, icon: 'lucide:eraser' },
|
|
515
|
+
{ id: 'select-all', label: 'Select All', enabled: true, icon: 'lucide:text-select' }
|
|
516
|
+
);
|
|
517
|
+
divider();
|
|
518
|
+
} else if (elementInfo.isTextSelected) {
|
|
519
|
+
const snippet = (elementInfo.selectedText || '').replace(/\s+/g, ' ').trim();
|
|
520
|
+
const label = snippet.length > 24 ? `${snippet.slice(0, 24)}…` : snippet;
|
|
275
521
|
items.push(
|
|
276
|
-
{ id: '
|
|
277
|
-
{
|
|
278
|
-
|
|
279
|
-
|
|
522
|
+
{ id: 'copy', label: 'Copy', enabled: true, icon: 'lucide:copy' },
|
|
523
|
+
{
|
|
524
|
+
id: 'search-selection',
|
|
525
|
+
label: label ? `Search for “${label}”` : 'Search for selection',
|
|
526
|
+
enabled: true,
|
|
527
|
+
icon: 'lucide:search'
|
|
528
|
+
}
|
|
280
529
|
);
|
|
530
|
+
divider();
|
|
281
531
|
}
|
|
282
532
|
|
|
533
|
+
// Page-level navigation last, and only enabled where it can actually go
|
|
534
|
+
// somewhere — an always-enabled Back that does nothing is worse than none.
|
|
535
|
+
items.push(
|
|
536
|
+
{ id: 'back', label: 'Back', enabled: navigation?.canGoBack ?? false, icon: 'lucide:arrow-left' },
|
|
537
|
+
{ id: 'forward', label: 'Forward', enabled: navigation?.canGoForward ?? false, icon: 'lucide:arrow-right' },
|
|
538
|
+
{ id: 'reload', label: 'Reload', enabled: true, icon: 'lucide:refresh-cw' }
|
|
539
|
+
);
|
|
540
|
+
divider();
|
|
541
|
+
|
|
542
|
+
if (!elementInfo.isEditable) {
|
|
543
|
+
items.push({ id: 'select-all', label: 'Select All', enabled: true, icon: 'lucide:text-select' });
|
|
544
|
+
}
|
|
545
|
+
items.push(
|
|
546
|
+
{ id: 'copy-page-url', label: 'Copy Page Address', enabled: true, icon: 'lucide:link-2' },
|
|
547
|
+
{ id: 'open-page-host', label: 'Open Page in Your Browser', enabled: true, icon: 'lucide:app-window' },
|
|
548
|
+
{ id: 'print', label: 'Print…', enabled: true, icon: 'lucide:printer' }
|
|
549
|
+
);
|
|
550
|
+
divider();
|
|
551
|
+
items.push({ id: 'inspect', label: 'Inspect', enabled: true, icon: 'lucide:code' });
|
|
552
|
+
|
|
283
553
|
return items;
|
|
284
554
|
}
|
|
285
555
|
|
|
@@ -339,7 +609,9 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
339
609
|
try {
|
|
340
610
|
debug.log('preview', `📋 Pasting text to element at (${x}, ${y})`);
|
|
341
611
|
|
|
342
|
-
await page
|
|
612
|
+
const target = await resolveFrameTarget(page, x, y);
|
|
613
|
+
|
|
614
|
+
await target.frame.evaluate((params) => {
|
|
343
615
|
const { x, y, text } = params;
|
|
344
616
|
const element = document.elementFromPoint(x, y) as HTMLElement;
|
|
345
617
|
|
|
@@ -370,7 +642,7 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
370
642
|
document.execCommand('insertText', false, text);
|
|
371
643
|
}
|
|
372
644
|
}
|
|
373
|
-
}, { x, y, text });
|
|
645
|
+
}, { x: target.x, y: target.y, text });
|
|
374
646
|
|
|
375
647
|
debug.log('preview', `✅ Pasted ${text.length} characters successfully`);
|
|
376
648
|
} catch (error) {
|
|
@@ -439,18 +711,28 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
439
711
|
break;
|
|
440
712
|
|
|
441
713
|
case 'copy':
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
714
|
+
// Read the selection out of the page and hand it to the viewer's
|
|
715
|
+
// clipboard: `execCommand('copy')` would only fill the headless
|
|
716
|
+
// browser's, which nothing the user can paste into ever reads.
|
|
717
|
+
{
|
|
718
|
+
const selected = await readSelection(page);
|
|
719
|
+
if (selected) {
|
|
720
|
+
this.emit('copy-to-clipboard', { text: selected });
|
|
721
|
+
}
|
|
722
|
+
}
|
|
445
723
|
break;
|
|
446
724
|
|
|
447
725
|
case 'cut':
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
726
|
+
{
|
|
727
|
+
const selected = await readSelection(page, true);
|
|
728
|
+
if (selected) {
|
|
729
|
+
this.emit('copy-to-clipboard', { text: selected });
|
|
730
|
+
}
|
|
731
|
+
}
|
|
451
732
|
break;
|
|
452
733
|
|
|
453
734
|
case 'paste':
|
|
735
|
+
case 'paste-plain':
|
|
454
736
|
// Paste clipboard content to the page
|
|
455
737
|
if (clipboardText !== undefined) {
|
|
456
738
|
await this.pasteTextToPage(page, menuInfo.x, menuInfo.y, clipboardText);
|
|
@@ -459,6 +741,98 @@ export class BrowserNativeUIHandler extends EventEmitter {
|
|
|
459
741
|
}
|
|
460
742
|
break;
|
|
461
743
|
|
|
744
|
+
case 'undo':
|
|
745
|
+
await page.evaluate(() => document.execCommand('undo'));
|
|
746
|
+
break;
|
|
747
|
+
|
|
748
|
+
case 'redo':
|
|
749
|
+
await page.evaluate(() => document.execCommand('redo'));
|
|
750
|
+
break;
|
|
751
|
+
|
|
752
|
+
case 'delete':
|
|
753
|
+
await page.evaluate(() => document.execCommand('delete'));
|
|
754
|
+
break;
|
|
755
|
+
|
|
756
|
+
case 'select-all': {
|
|
757
|
+
// Inside a field select just that field, otherwise the document —
|
|
758
|
+
// the same split Chrome makes.
|
|
759
|
+
const selectAllTarget = await resolveFrameTarget(page, menuInfo.x, menuInfo.y);
|
|
760
|
+
await selectAllTarget.frame.evaluate(() => {
|
|
761
|
+
const active = document.activeElement as HTMLInputElement | HTMLTextAreaElement | null;
|
|
762
|
+
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA')) {
|
|
763
|
+
active.select();
|
|
764
|
+
} else {
|
|
765
|
+
document.execCommand('selectAll');
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
case 'search-selection':
|
|
772
|
+
{
|
|
773
|
+
const selected = await readSelection(page);
|
|
774
|
+
if (selected.trim()) {
|
|
775
|
+
this.emit('open-url-new-tab', {
|
|
776
|
+
url: `https://www.google.com/search?q=${encodeURIComponent(selected.trim())}`
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
break;
|
|
781
|
+
|
|
782
|
+
case 'copy-page-url':
|
|
783
|
+
this.emit('copy-to-clipboard', { text: page.url() });
|
|
784
|
+
break;
|
|
785
|
+
|
|
786
|
+
case 'open-page-host':
|
|
787
|
+
this.emit('open-url-host-browser', { url: page.url() });
|
|
788
|
+
break;
|
|
789
|
+
|
|
790
|
+
case 'open-link-host':
|
|
791
|
+
if (menuInfo.elementInfo.linkUrl) {
|
|
792
|
+
this.emit('open-url-host-browser', { url: menuInfo.elementInfo.linkUrl });
|
|
793
|
+
}
|
|
794
|
+
break;
|
|
795
|
+
|
|
796
|
+
case 'open-media-new-tab':
|
|
797
|
+
if (menuInfo.elementInfo.mediaUrl) {
|
|
798
|
+
this.emit('open-url-new-tab', { url: menuInfo.elementInfo.mediaUrl });
|
|
799
|
+
}
|
|
800
|
+
break;
|
|
801
|
+
|
|
802
|
+
case 'media-play-pause':
|
|
803
|
+
case 'media-mute':
|
|
804
|
+
case 'media-loop': {
|
|
805
|
+
const mediaTarget = await resolveFrameTarget(page, menuInfo.x, menuInfo.y);
|
|
806
|
+
await mediaTarget.frame.evaluate(
|
|
807
|
+
(params: { x: number; y: number; action: string }) => {
|
|
808
|
+
const element = document.elementFromPoint(params.x, params.y);
|
|
809
|
+
const media = (element?.tagName === 'VIDEO' || element?.tagName === 'AUDIO'
|
|
810
|
+
? element
|
|
811
|
+
: element?.closest('video, audio')) as HTMLMediaElement | null;
|
|
812
|
+
if (!media) return;
|
|
813
|
+
|
|
814
|
+
if (params.action === 'media-play-pause') {
|
|
815
|
+
if (media.paused) void media.play();
|
|
816
|
+
else media.pause();
|
|
817
|
+
} else if (params.action === 'media-mute') {
|
|
818
|
+
media.muted = !media.muted;
|
|
819
|
+
} else {
|
|
820
|
+
media.loop = !media.loop;
|
|
821
|
+
}
|
|
822
|
+
},
|
|
823
|
+
{ x: mediaTarget.x, y: mediaTarget.y, action: itemId }
|
|
824
|
+
);
|
|
825
|
+
break;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
case 'print':
|
|
829
|
+
this.emit('print-page', { timestamp: Date.now() });
|
|
830
|
+
break;
|
|
831
|
+
|
|
832
|
+
case 'inspect':
|
|
833
|
+
this.emit('open-inspector', { timestamp: Date.now() });
|
|
834
|
+
break;
|
|
835
|
+
|
|
462
836
|
case 'copy-link':
|
|
463
837
|
if (menuInfo.elementInfo.linkUrl) {
|
|
464
838
|
// Emit event to frontend to copy to clipboard (can't access clipboard from backend)
|