browserpaw 5.0.0 → 5.0.1
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/v5/cli.cjs +1207 -335
- package/dist/v5/extension/background.js +8649 -6972
- package/dist/v5/extension/browserpaw-build.json +52 -45
- package/dist/v5/extension/chunks/{popup-DjxqduQH.js → popup-BSqTH70w.js} +39 -6
- package/dist/v5/extension/content-scripts/agent-cursor.js +30 -14
- package/dist/v5/extension/inject-scripts/accessibility-tree-helper.js +377 -229
- package/dist/v5/extension/inject-scripts/keyboard-helper.js +12 -2
- package/dist/v5/extension/inject-scripts/network-helper.js +166 -1
- package/dist/v5/extension/inject-scripts/screenshot-helper.js +397 -356
- package/dist/v5/extension/inpage-engine.js +7070 -6933
- package/dist/v5/extension/manifest.json +1 -1
- package/dist/v5/extension/popup.html +1 -1
- package/dist/v5/host.cjs +445 -73
- package/dist/v5/recipes/bilibili-popular-videos.json +364 -0
- package/dist/v5/recipes/bilibili-video-detail.json +395 -0
- package/dist/v5/recipes/github-issue-thread.json +313 -0
- package/dist/v5/recipes/github-releases.json +86 -45
- package/dist/v5/recipes/github-repo-readme.json +306 -0
- package/dist/v5/recipes/github-repo-search.json +313 -0
- package/dist/v5/recipes/mdn-search.json +93 -48
- package/dist/v5/recipes/npm-compare.json +6 -4
- package/dist/v5/recipes/reddit-post-read.json +289 -0
- package/dist/v5/recipes/reddit-subreddit-posts.json +331 -0
- package/dist/v5/recipes/v2ex-node-topics.json +334 -0
- package/dist/v5/recipes/v2ex-topic-read.json +325 -0
- package/dist/v5/recipes/x-profile-posts.json +332 -0
- package/dist/v5/recipes/x-search-thread.json +13 -41
- package/dist/v5/recipes/x-visible-thread.json +40 -83
- package/dist/v5/recipes/youtube-search-videos.json +279 -0
- package/dist/v5/recipes/youtube-video-read.json +349 -0
- package/dist/v5/skill/browserpaw/SKILL.md +4 -4
- package/dist/v5/skill/browserpaw/assets/recipes/bilibili-popular-videos.json +364 -0
- package/dist/v5/skill/browserpaw/assets/recipes/bilibili-video-detail.json +395 -0
- package/dist/v5/skill/browserpaw/assets/recipes/github-issue-thread.json +313 -0
- package/dist/v5/skill/browserpaw/assets/recipes/github-releases.json +86 -45
- package/dist/v5/skill/browserpaw/assets/recipes/github-repo-readme.json +306 -0
- package/dist/v5/skill/browserpaw/assets/recipes/github-repo-search.json +313 -0
- package/dist/v5/skill/browserpaw/assets/recipes/mdn-search.json +93 -48
- package/dist/v5/skill/browserpaw/assets/recipes/npm-compare.json +6 -4
- package/dist/v5/skill/browserpaw/assets/recipes/reddit-post-read.json +289 -0
- package/dist/v5/skill/browserpaw/assets/recipes/reddit-subreddit-posts.json +331 -0
- package/dist/v5/skill/browserpaw/assets/recipes/v2ex-node-topics.json +334 -0
- package/dist/v5/skill/browserpaw/assets/recipes/v2ex-topic-read.json +325 -0
- package/dist/v5/skill/browserpaw/assets/recipes/x-profile-posts.json +332 -0
- package/dist/v5/skill/browserpaw/assets/recipes/x-search-thread.json +13 -41
- package/dist/v5/skill/browserpaw/assets/recipes/x-visible-thread.json +40 -83
- package/dist/v5/skill/browserpaw/assets/recipes/youtube-search-videos.json +279 -0
- package/dist/v5/skill/browserpaw/assets/recipes/youtube-video-read.json +349 -0
- package/dist/v5/skill/browserpaw/references/recipe-contract.md +4 -3
- package/package.json +1 -1
|
@@ -13,11 +13,151 @@
|
|
|
13
13
|
const MAX_LINE_LABEL = 100; // max characters for a single label in output
|
|
14
14
|
const REF_MAP_LIMIT = 1000; // limit size of the ref map to keep payload small
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
const TAG_ROLE_MAP = new Map([
|
|
17
|
+
['a', 'link'],
|
|
18
|
+
['button', 'button'],
|
|
19
|
+
['select', 'combobox'],
|
|
20
|
+
['textarea', 'textbox'],
|
|
21
|
+
['h1', 'heading'],
|
|
22
|
+
['h2', 'heading'],
|
|
23
|
+
['h3', 'heading'],
|
|
24
|
+
['h4', 'heading'],
|
|
25
|
+
['h5', 'heading'],
|
|
26
|
+
['h6', 'heading'],
|
|
27
|
+
['img', 'image'],
|
|
28
|
+
['nav', 'navigation'],
|
|
29
|
+
['main', 'main'],
|
|
30
|
+
['header', 'banner'],
|
|
31
|
+
['footer', 'contentinfo'],
|
|
32
|
+
['section', 'region'],
|
|
33
|
+
['article', 'article'],
|
|
34
|
+
['aside', 'complementary'],
|
|
35
|
+
['form', 'form'],
|
|
36
|
+
['table', 'table'],
|
|
37
|
+
['ul', 'list'],
|
|
38
|
+
['ol', 'list'],
|
|
39
|
+
['li', 'listitem'],
|
|
40
|
+
['label', 'label'],
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
const INPUT_TYPE_ROLE_MAP = new Map([
|
|
44
|
+
['submit', 'button'],
|
|
45
|
+
['button', 'button'],
|
|
46
|
+
['checkbox', 'checkbox'],
|
|
47
|
+
['radio', 'radio'],
|
|
48
|
+
['file', 'button'],
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const TEXT_LABEL_TAGS = new Set(['button', 'a', 'summary']);
|
|
52
|
+
const HEADING_TAG_RE = /^h[1-6]$/;
|
|
53
|
+
const INTERACTIVE_TAGS = new Set([
|
|
54
|
+
'a',
|
|
55
|
+
'button',
|
|
56
|
+
'input',
|
|
57
|
+
'select',
|
|
58
|
+
'textarea',
|
|
59
|
+
'details',
|
|
60
|
+
'summary',
|
|
61
|
+
]);
|
|
62
|
+
const INTERACTIVE_ROLES = new Set([
|
|
63
|
+
'button',
|
|
64
|
+
'link',
|
|
65
|
+
'checkbox',
|
|
66
|
+
'radio',
|
|
67
|
+
'switch',
|
|
68
|
+
'slider',
|
|
69
|
+
'option',
|
|
70
|
+
'menuitem',
|
|
71
|
+
'textbox',
|
|
72
|
+
'searchbox',
|
|
73
|
+
'combobox',
|
|
74
|
+
'spinbutton',
|
|
75
|
+
'tab',
|
|
76
|
+
'treeitem',
|
|
77
|
+
]);
|
|
78
|
+
const SHADOW_INTERACTIVE_SELECTOR =
|
|
79
|
+
'input, textarea, select, button, a[href], [contenteditable="true"], [role="button"], [role="link"], [role="textbox"], [role="combobox"], [role="searchbox"], [role="menuitem"], [role="option"], [role="switch"], [role="radio"], [role="checkbox"], [role="tab"], [role="slider"]';
|
|
80
|
+
const STRUCTURAL_TAGS = new Set([
|
|
81
|
+
'h1',
|
|
82
|
+
'h2',
|
|
83
|
+
'h3',
|
|
84
|
+
'h4',
|
|
85
|
+
'h5',
|
|
86
|
+
'h6',
|
|
87
|
+
'nav',
|
|
88
|
+
'main',
|
|
89
|
+
'header',
|
|
90
|
+
'footer',
|
|
91
|
+
'section',
|
|
92
|
+
'article',
|
|
93
|
+
'aside',
|
|
94
|
+
]);
|
|
95
|
+
const FORMISH_ROLES = new Set(['search', 'form', 'group', 'toolbar', 'navigation']);
|
|
96
|
+
const FORMISH_TAGS = new Set(['form', 'fieldset', 'nav', 'legend']);
|
|
97
|
+
const EXCLUDED_TAGS = new Set(['script', 'style', 'meta', 'link', 'title', 'noscript']);
|
|
98
|
+
const SELECTOR_IDENTITY_ATTRS = ['data-testid', 'data-cy', 'name'];
|
|
99
|
+
const EMPTY_GENERIC_LINE_RE = /^\s*- generic \[ref=ref_\d+\]$/;
|
|
100
|
+
|
|
101
|
+
function syncReverseMapAndPrune(map, reverseMap) {
|
|
102
|
+
for (const key in map) {
|
|
103
|
+
if (!Object.prototype.hasOwnProperty.call(map, key)) continue;
|
|
104
|
+
const weak = map[key];
|
|
105
|
+
const derefed = weak && typeof weak.deref === 'function' ? weak.deref() : null;
|
|
106
|
+
if (!derefed) {
|
|
107
|
+
delete map[key];
|
|
108
|
+
} else if (typeof derefed === 'object' || typeof derefed === 'function') {
|
|
109
|
+
reverseMap.set(derefed, key);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function ensureElementRefRegistry() {
|
|
115
|
+
if (!window.__mcpElementMap) window.__mcpElementMap = window.__claudeElementMap || {};
|
|
116
|
+
if (!window.__mcpRefCounter) window.__mcpRefCounter = window.__claudeRefCounter || 0;
|
|
117
|
+
window.__claudeElementMap = window.__mcpElementMap;
|
|
118
|
+
window.__claudeRefCounter = window.__mcpRefCounter;
|
|
119
|
+
|
|
120
|
+
const map = window.__mcpElementMap;
|
|
121
|
+
let reverseMap = window.__mcpElementReverseMap;
|
|
122
|
+
let rebuilt = false;
|
|
123
|
+
if (!(reverseMap instanceof WeakMap) || window.__mcpElementReverseMapSource !== map) {
|
|
124
|
+
reverseMap = new WeakMap();
|
|
125
|
+
window.__mcpElementReverseMap = reverseMap;
|
|
126
|
+
window.__mcpElementReverseMapSource = map;
|
|
127
|
+
syncReverseMapAndPrune(map, reverseMap);
|
|
128
|
+
rebuilt = true;
|
|
129
|
+
}
|
|
130
|
+
return { map, reverseMap, rebuilt };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function syncAndPruneElementRefs() {
|
|
134
|
+
const { map, reverseMap, rebuilt } = ensureElementRefRegistry();
|
|
135
|
+
if (!rebuilt) {
|
|
136
|
+
syncReverseMapAndPrune(map, reverseMap);
|
|
137
|
+
}
|
|
138
|
+
return { map, reverseMap };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function getOrAssignElementRef(el) {
|
|
142
|
+
const { map, reverseMap } = ensureElementRefRegistry();
|
|
143
|
+
const existing = reverseMap.get(el);
|
|
144
|
+
if (existing) {
|
|
145
|
+
const existingWeak = Object.prototype.hasOwnProperty.call(map, existing)
|
|
146
|
+
? map[existing]
|
|
147
|
+
: null;
|
|
148
|
+
if (existingWeak && typeof existingWeak.deref === 'function' && existingWeak.deref() === el) {
|
|
149
|
+
return existing;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const refId = `ref_${++window.__mcpRefCounter}`;
|
|
153
|
+
window.__claudeRefCounter = window.__mcpRefCounter;
|
|
154
|
+
map[refId] = new WeakRef(el);
|
|
155
|
+
reverseMap.set(el, refId);
|
|
156
|
+
return refId;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Keep a weak map from ref id to elements and an O(1) reverse WeakMap
|
|
160
|
+
ensureElementRefRegistry();
|
|
21
161
|
|
|
22
162
|
/**
|
|
23
163
|
* Infer ARIA-like role from element
|
|
@@ -28,44 +168,11 @@
|
|
|
28
168
|
const role = el.getAttribute('role');
|
|
29
169
|
if (role) return role;
|
|
30
170
|
const tag = el.tagName.toLowerCase();
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
type === 'submit' || type === 'button'
|
|
37
|
-
? 'button'
|
|
38
|
-
: type === 'checkbox'
|
|
39
|
-
? 'checkbox'
|
|
40
|
-
: type === 'radio'
|
|
41
|
-
? 'radio'
|
|
42
|
-
: type === 'file'
|
|
43
|
-
? 'button'
|
|
44
|
-
: 'textbox',
|
|
45
|
-
select: 'combobox',
|
|
46
|
-
textarea: 'textbox',
|
|
47
|
-
h1: 'heading',
|
|
48
|
-
h2: 'heading',
|
|
49
|
-
h3: 'heading',
|
|
50
|
-
h4: 'heading',
|
|
51
|
-
h5: 'heading',
|
|
52
|
-
h6: 'heading',
|
|
53
|
-
img: 'image',
|
|
54
|
-
nav: 'navigation',
|
|
55
|
-
main: 'main',
|
|
56
|
-
header: 'banner',
|
|
57
|
-
footer: 'contentinfo',
|
|
58
|
-
section: 'region',
|
|
59
|
-
article: 'article',
|
|
60
|
-
aside: 'complementary',
|
|
61
|
-
form: 'form',
|
|
62
|
-
table: 'table',
|
|
63
|
-
ul: 'list',
|
|
64
|
-
ol: 'list',
|
|
65
|
-
li: 'listitem',
|
|
66
|
-
label: 'label',
|
|
67
|
-
};
|
|
68
|
-
return map[tag] || 'generic';
|
|
171
|
+
if (tag === 'input') {
|
|
172
|
+
const type = el.getAttribute('type') || '';
|
|
173
|
+
return INPUT_TYPE_ROLE_MAP.get(type) || 'textbox';
|
|
174
|
+
}
|
|
175
|
+
return TAG_ROLE_MAP.get(tag) || 'generic';
|
|
69
176
|
}
|
|
70
177
|
|
|
71
178
|
/**
|
|
@@ -99,7 +206,7 @@
|
|
|
99
206
|
if (type === 'submit' && val && val.trim()) return val.trim();
|
|
100
207
|
if (input.value && input.value.length < 50 && input.value.trim()) return input.value.trim();
|
|
101
208
|
}
|
|
102
|
-
if (
|
|
209
|
+
if (TEXT_LABEL_TAGS.has(tag)) {
|
|
103
210
|
let text = '';
|
|
104
211
|
for (let i = 0; i < el.childNodes.length; i++) {
|
|
105
212
|
const n = el.childNodes[i];
|
|
@@ -107,7 +214,7 @@
|
|
|
107
214
|
}
|
|
108
215
|
if (text.trim()) return text.trim();
|
|
109
216
|
}
|
|
110
|
-
if (
|
|
217
|
+
if (HEADING_TAG_RE.test(tag)) {
|
|
111
218
|
const t = el.textContent;
|
|
112
219
|
if (t && t.trim()) return t.trim().substring(0, MAX_LINE_LABEL);
|
|
113
220
|
}
|
|
@@ -130,12 +237,45 @@
|
|
|
130
237
|
return '';
|
|
131
238
|
}
|
|
132
239
|
|
|
240
|
+
function getCachedComputedStyle(el, state) {
|
|
241
|
+
const cache = state && state.styleCache;
|
|
242
|
+
if (cache && cache.has(el)) return cache.get(el);
|
|
243
|
+
const cs = window.getComputedStyle(/** @type {HTMLElement} */ (el));
|
|
244
|
+
if (cache) cache.set(el, cs);
|
|
245
|
+
return cs;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function getCachedBoundingRect(el, state) {
|
|
249
|
+
const cache = state && state.rectCache;
|
|
250
|
+
if (cache && cache.has(el)) return cache.get(el);
|
|
251
|
+
const rect = /** @type {HTMLElement} */ (el).getBoundingClientRect();
|
|
252
|
+
if (cache) cache.set(el, rect);
|
|
253
|
+
return rect;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function getCachedLabel(el, state) {
|
|
257
|
+
const cache = state && state.labelCache;
|
|
258
|
+
if (cache && cache.has(el)) return cache.get(el);
|
|
259
|
+
const label = inferLabel(el);
|
|
260
|
+
if (cache) cache.set(el, label);
|
|
261
|
+
return label;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function getCachedInteractive(el, state) {
|
|
265
|
+
const cache = state && state.interactiveCache;
|
|
266
|
+
if (cache && cache.has(el)) return cache.get(el);
|
|
267
|
+
const interactive = isInteractive(el);
|
|
268
|
+
if (cache) cache.set(el, interactive);
|
|
269
|
+
return interactive;
|
|
270
|
+
}
|
|
271
|
+
|
|
133
272
|
/**
|
|
134
273
|
* Check if element is visible in DOM
|
|
135
274
|
* @param {Element} el
|
|
275
|
+
* @param {{styleCache?: WeakMap<Element, CSSStyleDeclaration>}=} state
|
|
136
276
|
*/
|
|
137
|
-
function isVisible(el) {
|
|
138
|
-
const cs =
|
|
277
|
+
function isVisible(el, state) {
|
|
278
|
+
const cs = getCachedComputedStyle(el, state);
|
|
139
279
|
if (cs.display === 'none' || cs.visibility === 'hidden' || cs.opacity === '0') return false;
|
|
140
280
|
const he = /** @type {HTMLElement} */ (el);
|
|
141
281
|
return he.offsetWidth > 0 && he.offsetHeight > 0;
|
|
@@ -148,8 +288,7 @@
|
|
|
148
288
|
function isInteractive(el) {
|
|
149
289
|
// Native interactive tags
|
|
150
290
|
const tag = el.tagName.toLowerCase();
|
|
151
|
-
if (
|
|
152
|
-
return true;
|
|
291
|
+
if (INTERACTIVE_TAGS.has(tag)) return true;
|
|
153
292
|
|
|
154
293
|
// Generic interactive hints
|
|
155
294
|
if (el.getAttribute('onclick') != null) return true;
|
|
@@ -163,23 +302,7 @@
|
|
|
163
302
|
|
|
164
303
|
// ARIA roles commonly used by custom elements
|
|
165
304
|
const role = (el.getAttribute && el.getAttribute('role')) || '';
|
|
166
|
-
|
|
167
|
-
'button',
|
|
168
|
-
'link',
|
|
169
|
-
'checkbox',
|
|
170
|
-
'radio',
|
|
171
|
-
'switch',
|
|
172
|
-
'slider',
|
|
173
|
-
'option',
|
|
174
|
-
'menuitem',
|
|
175
|
-
'textbox',
|
|
176
|
-
'searchbox',
|
|
177
|
-
'combobox',
|
|
178
|
-
'spinbutton',
|
|
179
|
-
'tab',
|
|
180
|
-
'treeitem',
|
|
181
|
-
]);
|
|
182
|
-
if (role && interactiveRoles.has(role.toLowerCase())) return true;
|
|
305
|
+
if (role && INTERACTIVE_ROLES.has(role.toLowerCase())) return true;
|
|
183
306
|
|
|
184
307
|
// Shadow host case: treat host as interactive if its open shadow root contains
|
|
185
308
|
// an interactive control (textarea/input/select/button/a or contenteditable).
|
|
@@ -187,9 +310,7 @@
|
|
|
187
310
|
const anyEl = /** @type {any} */ (el);
|
|
188
311
|
const sr = anyEl && anyEl.shadowRoot ? anyEl.shadowRoot : null;
|
|
189
312
|
if (sr) {
|
|
190
|
-
const inner = sr.querySelector(
|
|
191
|
-
'input, textarea, select, button, a[href], [contenteditable="true"], [role="button"], [role="link"], [role="textbox"], [role="combobox"], [role="searchbox"], [role="menuitem"], [role="option"], [role="switch"], [role="radio"], [role="checkbox"], [role="tab"], [role="slider"]',
|
|
192
|
-
);
|
|
313
|
+
const inner = sr.querySelector(SHADOW_INTERACTIVE_SELECTOR);
|
|
193
314
|
if (inner) return true;
|
|
194
315
|
}
|
|
195
316
|
} catch (_) {
|
|
@@ -204,24 +325,7 @@
|
|
|
204
325
|
*/
|
|
205
326
|
function isStructural(el) {
|
|
206
327
|
const tag = el.tagName.toLowerCase();
|
|
207
|
-
if (
|
|
208
|
-
[
|
|
209
|
-
'h1',
|
|
210
|
-
'h2',
|
|
211
|
-
'h3',
|
|
212
|
-
'h4',
|
|
213
|
-
'h5',
|
|
214
|
-
'h6',
|
|
215
|
-
'nav',
|
|
216
|
-
'main',
|
|
217
|
-
'header',
|
|
218
|
-
'footer',
|
|
219
|
-
'section',
|
|
220
|
-
'article',
|
|
221
|
-
'aside',
|
|
222
|
-
].includes(tag)
|
|
223
|
-
)
|
|
224
|
-
return true;
|
|
328
|
+
if (STRUCTURAL_TAGS.has(tag)) return true;
|
|
225
329
|
return el.getAttribute('role') != null;
|
|
226
330
|
}
|
|
227
331
|
|
|
@@ -232,6 +336,7 @@
|
|
|
232
336
|
function isFormishContainer(el) {
|
|
233
337
|
const tag = el.tagName.toLowerCase();
|
|
234
338
|
const role = (el.getAttribute && el.getAttribute('role')) || '';
|
|
339
|
+
if (FORMISH_TAGS.has(tag) || FORMISH_ROLES.has(role)) return true;
|
|
235
340
|
const id = /** @type {HTMLElement} */ (el).id || '';
|
|
236
341
|
// Normalize className for HTML/SVG elements
|
|
237
342
|
let cls = '';
|
|
@@ -247,15 +352,6 @@
|
|
|
247
352
|
/* ignore */
|
|
248
353
|
}
|
|
249
354
|
return (
|
|
250
|
-
role === 'search' ||
|
|
251
|
-
role === 'form' ||
|
|
252
|
-
role === 'group' ||
|
|
253
|
-
role === 'toolbar' ||
|
|
254
|
-
role === 'navigation' ||
|
|
255
|
-
tag === 'form' ||
|
|
256
|
-
tag === 'fieldset' ||
|
|
257
|
-
tag === 'nav' ||
|
|
258
|
-
tag === 'legend' ||
|
|
259
355
|
id.includes('search') ||
|
|
260
356
|
cls.includes('search') ||
|
|
261
357
|
id.includes('form') ||
|
|
@@ -462,14 +558,15 @@
|
|
|
462
558
|
* Whether to include element in tree under config
|
|
463
559
|
* @param {Element} el
|
|
464
560
|
* @param {{filter?: 'all'|'interactive'}} cfg
|
|
561
|
+
* @param {object=} state
|
|
465
562
|
*/
|
|
466
|
-
function shouldInclude(el, cfg) {
|
|
563
|
+
function shouldInclude(el, cfg, state) {
|
|
467
564
|
const tag = el.tagName.toLowerCase();
|
|
468
|
-
if (
|
|
565
|
+
if (EXCLUDED_TAGS.has(tag)) return false;
|
|
469
566
|
if (el.getAttribute('aria-hidden') === 'true') return false;
|
|
470
|
-
if (!isVisible(el)) return false;
|
|
567
|
+
if (!isVisible(el, state)) return false;
|
|
471
568
|
if (cfg.filter !== 'all') {
|
|
472
|
-
const r =
|
|
569
|
+
const r = getCachedBoundingRect(el, state);
|
|
473
570
|
if (!(
|
|
474
571
|
r.top < window.innerHeight &&
|
|
475
572
|
r.bottom > 0 &&
|
|
@@ -478,10 +575,10 @@
|
|
|
478
575
|
))
|
|
479
576
|
return false;
|
|
480
577
|
}
|
|
481
|
-
if (cfg.filter === 'interactive') return
|
|
482
|
-
if (
|
|
578
|
+
if (cfg.filter === 'interactive') return getCachedInteractive(el, state);
|
|
579
|
+
if (getCachedInteractive(el, state)) return true;
|
|
483
580
|
if (isStructural(el)) return true;
|
|
484
|
-
if (
|
|
581
|
+
if (getCachedLabel(el, state).length > 0) return true;
|
|
485
582
|
return isFormishContainer(el);
|
|
486
583
|
}
|
|
487
584
|
|
|
@@ -496,7 +593,7 @@
|
|
|
496
593
|
const idSel = `#${CSS.escape(/** @type {HTMLElement} */ (el).id)}`;
|
|
497
594
|
if (document.querySelectorAll(idSel).length === 1) return idSel;
|
|
498
595
|
}
|
|
499
|
-
for (const attr of
|
|
596
|
+
for (const attr of SELECTOR_IDENTITY_ATTRS) {
|
|
500
597
|
const attrValue = el.getAttribute(attr);
|
|
501
598
|
if (attrValue) {
|
|
502
599
|
const s = `[${attr}="${CSS.escape(attrValue)}"]`;
|
|
@@ -537,22 +634,12 @@
|
|
|
537
634
|
if (state.processed >= MAX_NODES) return;
|
|
538
635
|
if (state.visited.has(el)) return;
|
|
539
636
|
state.visited.add(el);
|
|
540
|
-
const include = shouldInclude(el, cfg) || depth === 0;
|
|
637
|
+
const include = shouldInclude(el, cfg, state) || depth === 0;
|
|
541
638
|
if (include) {
|
|
542
639
|
const role = inferRole(el);
|
|
543
|
-
let label =
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
if (window.__mcpElementMap[k].deref && window.__mcpElementMap[k].deref() === el) {
|
|
547
|
-
refId = k;
|
|
548
|
-
break;
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
if (!refId) {
|
|
552
|
-
refId = `ref_${++window.__mcpRefCounter}`;
|
|
553
|
-
window.__mcpElementMap[refId] = new WeakRef(el);
|
|
554
|
-
}
|
|
555
|
-
const rect = /** @type {HTMLElement} */ (el).getBoundingClientRect();
|
|
640
|
+
let label = getCachedLabel(el, state);
|
|
641
|
+
const refId = getOrAssignElementRef(el);
|
|
642
|
+
const rect = getCachedBoundingRect(el, state);
|
|
556
643
|
const cx = Math.round(rect.left + rect.width / 2);
|
|
557
644
|
const cy = Math.round(rect.top + rect.height / 2);
|
|
558
645
|
let line = `${' '.repeat(depth)}- ${role}`;
|
|
@@ -572,7 +659,7 @@
|
|
|
572
659
|
try {
|
|
573
660
|
const disabled = el.hasAttribute('disabled') || el.getAttribute('aria-disabled') === 'true';
|
|
574
661
|
if (disabled) line += ` disabled`;
|
|
575
|
-
const cs =
|
|
662
|
+
const cs = getCachedComputedStyle(el, state);
|
|
576
663
|
if (cs && cs.pointerEvents === 'none') line += ` pe=none`;
|
|
577
664
|
} catch (_) {
|
|
578
665
|
/* ignore style issues */
|
|
@@ -582,7 +669,7 @@
|
|
|
582
669
|
state.processed++;
|
|
583
670
|
|
|
584
671
|
// Only collect ref mapping for interactive elements to limit cost
|
|
585
|
-
if (
|
|
672
|
+
if (getCachedInteractive(el, state) && refMap.length < REF_MAP_LIMIT) {
|
|
586
673
|
refMap.push({
|
|
587
674
|
ref: /** @type {string} */ (refId),
|
|
588
675
|
selector: generateSelector(el),
|
|
@@ -637,7 +724,17 @@
|
|
|
637
724
|
}
|
|
638
725
|
|
|
639
726
|
const refMap = [];
|
|
640
|
-
const state = {
|
|
727
|
+
const state = {
|
|
728
|
+
processed: 0,
|
|
729
|
+
included: 0,
|
|
730
|
+
visited: new WeakSet(),
|
|
731
|
+
styleCache: new WeakMap(),
|
|
732
|
+
rectCache: new WeakMap(),
|
|
733
|
+
labelCache: new WeakMap(),
|
|
734
|
+
interactiveCache: new WeakMap(),
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
syncAndPruneElementRefs();
|
|
641
738
|
|
|
642
739
|
// Determine root element (body or refId-specified element)
|
|
643
740
|
let focus = null;
|
|
@@ -655,13 +752,7 @@
|
|
|
655
752
|
}
|
|
656
753
|
|
|
657
754
|
if (root) traverse(root, 0, cfg, out, refMap, state);
|
|
658
|
-
|
|
659
|
-
if (!window.__mcpElementMap[k].deref || !window.__mcpElementMap[k].deref())
|
|
660
|
-
delete window.__mcpElementMap[k];
|
|
661
|
-
}
|
|
662
|
-
const pageContent = out
|
|
663
|
-
.filter((line) => !/^\s*- generic \[ref=ref_\d+\]$/.test(line))
|
|
664
|
-
.join('\n');
|
|
755
|
+
const pageContent = out.filter((line) => !EMPTY_GENERIC_LINE_RE.test(line)).join('\n');
|
|
665
756
|
const end = performance && performance.now ? performance.now() : Date.now();
|
|
666
757
|
return {
|
|
667
758
|
pageContent,
|
|
@@ -705,7 +796,7 @@
|
|
|
705
796
|
|
|
706
797
|
function resolveRef(ref) {
|
|
707
798
|
const map = window.__mcpElementMap || window.__claudeElementMap || {};
|
|
708
|
-
const weak = map[ref];
|
|
799
|
+
const weak = Object.prototype.hasOwnProperty.call(map, ref) ? map[ref] : null;
|
|
709
800
|
return weak && typeof weak.deref === 'function' ? weak.deref() : null;
|
|
710
801
|
}
|
|
711
802
|
|
|
@@ -737,38 +828,107 @@
|
|
|
737
828
|
};
|
|
738
829
|
}
|
|
739
830
|
|
|
831
|
+
let activePickerCancel = null;
|
|
832
|
+
|
|
833
|
+
function resolveFrameTargetOrigin(frame, contentWindow) {
|
|
834
|
+
try {
|
|
835
|
+
const origin = contentWindow?.location?.origin;
|
|
836
|
+
if (origin && origin !== 'null' && /^https?:\/\//i.test(origin)) {
|
|
837
|
+
return origin;
|
|
838
|
+
}
|
|
839
|
+
} catch {}
|
|
840
|
+
try {
|
|
841
|
+
const src =
|
|
842
|
+
(typeof frame?.getAttribute === 'function' && frame.getAttribute('src')) || frame?.src;
|
|
843
|
+
if (src) {
|
|
844
|
+
const parsed = new URL(src, location.href);
|
|
845
|
+
if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
|
|
846
|
+
return parsed.origin;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
} catch {}
|
|
850
|
+
return '*';
|
|
851
|
+
}
|
|
852
|
+
|
|
740
853
|
function forwardHoverRefToChildren(ref) {
|
|
741
854
|
return new Promise((resolve) => {
|
|
742
855
|
const frames = Array.from(document.querySelectorAll('iframe, frame'));
|
|
743
|
-
|
|
856
|
+
const targetEntries = [];
|
|
857
|
+
const targetWindows = new Set();
|
|
858
|
+
for (const frame of frames) {
|
|
859
|
+
try {
|
|
860
|
+
const contentWindow = frame && frame.contentWindow;
|
|
861
|
+
if (contentWindow && !targetWindows.has(contentWindow)) {
|
|
862
|
+
targetWindows.add(contentWindow);
|
|
863
|
+
targetEntries.push({ frame, contentWindow });
|
|
864
|
+
}
|
|
865
|
+
} catch {}
|
|
866
|
+
}
|
|
867
|
+
if (targetWindows.size === 0) {
|
|
744
868
|
resolve({ success: false, error: `ref "${ref}" not found` });
|
|
745
869
|
return;
|
|
746
870
|
}
|
|
747
871
|
const reqId = `hover_ref_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
872
|
+
const respondedWindows = new Set();
|
|
873
|
+
let lastErrorResult = null;
|
|
874
|
+
let settled = false;
|
|
875
|
+
let timeoutHandle = null;
|
|
876
|
+
|
|
877
|
+
const settle = (result) => {
|
|
878
|
+
if (settled) return;
|
|
879
|
+
settled = true;
|
|
880
|
+
window.removeEventListener('message', listener, true);
|
|
881
|
+
if (timeoutHandle !== null) {
|
|
882
|
+
clearTimeout(timeoutHandle);
|
|
883
|
+
timeoutHandle = null;
|
|
884
|
+
}
|
|
885
|
+
resolve(result);
|
|
886
|
+
};
|
|
887
|
+
|
|
748
888
|
const listener = (ev) => {
|
|
889
|
+
if (settled) return;
|
|
749
890
|
const data = ev?.data;
|
|
750
891
|
if (!data || data.type !== 'rr-bridge-hover-ref-result' || data.reqId !== reqId) return;
|
|
751
892
|
// Verify response comes from one of the targeted child frames
|
|
752
|
-
|
|
753
|
-
if (
|
|
754
|
-
|
|
755
|
-
|
|
893
|
+
if (!targetWindows.has(ev.source)) return;
|
|
894
|
+
if (data.result?.success) {
|
|
895
|
+
settle(data.result);
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
respondedWindows.add(ev.source);
|
|
899
|
+
lastErrorResult = data.result || {
|
|
900
|
+
success: false,
|
|
901
|
+
error: `ref "${ref}" not found in child frame`,
|
|
902
|
+
};
|
|
903
|
+
if (respondedWindows.size >= targetWindows.size) {
|
|
904
|
+
settle(lastErrorResult);
|
|
905
|
+
}
|
|
756
906
|
};
|
|
757
907
|
window.addEventListener('message', listener, true);
|
|
758
|
-
setTimeout(() => {
|
|
759
|
-
|
|
760
|
-
|
|
908
|
+
timeoutHandle = setTimeout(() => {
|
|
909
|
+
settle(
|
|
910
|
+
lastErrorResult || { success: false, error: `ref "${ref}" not found in child frames` },
|
|
911
|
+
);
|
|
761
912
|
}, 1500);
|
|
762
|
-
for (const frame of
|
|
913
|
+
for (const { frame, contentWindow } of targetEntries) {
|
|
763
914
|
try {
|
|
764
|
-
|
|
765
|
-
|
|
915
|
+
const targetOrigin = resolveFrameTargetOrigin(frame, contentWindow);
|
|
916
|
+
contentWindow.postMessage({ type: 'rr-bridge-hover-ref', reqId, ref }, targetOrigin);
|
|
917
|
+
} catch {
|
|
918
|
+
respondedWindows.add(contentWindow);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
if (respondedWindows.size >= targetWindows.size) {
|
|
922
|
+
settle(
|
|
923
|
+
lastErrorResult || { success: false, error: `ref "${ref}" not found in child frames` },
|
|
924
|
+
);
|
|
766
925
|
}
|
|
767
926
|
});
|
|
768
927
|
}
|
|
769
928
|
|
|
770
929
|
// Chrome message bridge for ping and tree generation
|
|
771
930
|
chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
|
|
931
|
+
if (!request || typeof request !== 'object') return false;
|
|
772
932
|
try {
|
|
773
933
|
if (
|
|
774
934
|
request &&
|
|
@@ -822,15 +982,29 @@
|
|
|
822
982
|
root.style.opacity = '0.5';
|
|
823
983
|
}
|
|
824
984
|
sendResponse({ success: true });
|
|
825
|
-
return
|
|
985
|
+
return false;
|
|
826
986
|
} catch (e) {
|
|
827
987
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
828
|
-
return
|
|
988
|
+
return false;
|
|
829
989
|
}
|
|
830
990
|
}
|
|
831
991
|
// Element picker: start a temporary overlay to let user pick an element
|
|
832
992
|
if (request && request.action === 'rr_picker_start') {
|
|
993
|
+
let responded = false;
|
|
994
|
+
const respondOnce = (payload) => {
|
|
995
|
+
if (responded) return;
|
|
996
|
+
responded = true;
|
|
997
|
+
try {
|
|
998
|
+
sendResponse(payload);
|
|
999
|
+
} catch {}
|
|
1000
|
+
};
|
|
833
1001
|
try {
|
|
1002
|
+
if (typeof activePickerCancel === 'function') {
|
|
1003
|
+
try {
|
|
1004
|
+
activePickerCancel('preempted');
|
|
1005
|
+
} catch {}
|
|
1006
|
+
activePickerCancel = null;
|
|
1007
|
+
}
|
|
834
1008
|
// state
|
|
835
1009
|
const state = { active: true };
|
|
836
1010
|
const hostId = '__rr_picker_host__';
|
|
@@ -883,6 +1057,14 @@
|
|
|
883
1057
|
document.removeEventListener('keydown', onKey, true);
|
|
884
1058
|
} catch {}
|
|
885
1059
|
state.active = false;
|
|
1060
|
+
if (activePickerCancel === cancelPicker) {
|
|
1061
|
+
activePickerCancel = null;
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
|
|
1065
|
+
const cancelPicker = (reason) => {
|
|
1066
|
+
cleanup();
|
|
1067
|
+
respondOnce({ success: false, cancelled: true, reason });
|
|
886
1068
|
};
|
|
887
1069
|
|
|
888
1070
|
const onMove = (e) => {
|
|
@@ -988,58 +1170,49 @@
|
|
|
988
1170
|
const el = e.target instanceof Element ? e.target : null;
|
|
989
1171
|
if (!el) {
|
|
990
1172
|
cleanup();
|
|
991
|
-
|
|
992
|
-
return
|
|
1173
|
+
respondOnce({ success: false, error: 'no element' });
|
|
1174
|
+
return;
|
|
993
1175
|
}
|
|
994
1176
|
// create ref
|
|
995
|
-
try {
|
|
996
|
-
if (!window.__mcpElementMap) window.__mcpElementMap = window.__claudeElementMap || {};
|
|
997
|
-
if (!window.__mcpRefCounter) window.__mcpRefCounter = window.__claudeRefCounter || 0;
|
|
998
|
-
window.__claudeElementMap = window.__mcpElementMap;
|
|
999
|
-
window.__claudeRefCounter = window.__mcpRefCounter;
|
|
1000
|
-
} catch {}
|
|
1001
1177
|
let refId = null;
|
|
1002
1178
|
try {
|
|
1003
|
-
|
|
1004
|
-
if (window.__mcpElementMap[k].deref && window.__mcpElementMap[k].deref() === el) {
|
|
1005
|
-
refId = k;
|
|
1006
|
-
break;
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
if (!refId) {
|
|
1010
|
-
refId = `ref_${++window.__mcpRefCounter}`;
|
|
1011
|
-
window.__mcpElementMap[refId] = new WeakRef(el);
|
|
1012
|
-
}
|
|
1179
|
+
refId = getOrAssignElementRef(el);
|
|
1013
1180
|
} catch {}
|
|
1014
1181
|
const cands = computeCandidates(el);
|
|
1015
1182
|
cleanup();
|
|
1016
|
-
|
|
1017
|
-
return true;
|
|
1183
|
+
respondOnce({ success: true, ref: refId, candidates: cands });
|
|
1018
1184
|
};
|
|
1019
1185
|
const onKey = (e) => {
|
|
1020
1186
|
if (e.key === 'Escape') {
|
|
1021
1187
|
cleanup();
|
|
1022
|
-
|
|
1188
|
+
respondOnce({ success: false, cancelled: true });
|
|
1023
1189
|
}
|
|
1024
1190
|
};
|
|
1191
|
+
activePickerCancel = cancelPicker;
|
|
1025
1192
|
document.addEventListener('mousemove', onMove, true);
|
|
1026
1193
|
document.addEventListener('click', onClick, true);
|
|
1027
1194
|
document.addEventListener('keydown', onKey, true);
|
|
1028
1195
|
return true; // async
|
|
1029
1196
|
} catch (e) {
|
|
1030
|
-
|
|
1031
|
-
return
|
|
1197
|
+
respondOnce({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1198
|
+
return false;
|
|
1032
1199
|
}
|
|
1033
1200
|
}
|
|
1034
1201
|
if (request && request.action === 'rr_picker_stop') {
|
|
1035
1202
|
try {
|
|
1203
|
+
if (typeof activePickerCancel === 'function') {
|
|
1204
|
+
try {
|
|
1205
|
+
activePickerCancel('stopped');
|
|
1206
|
+
} catch {}
|
|
1207
|
+
activePickerCancel = null;
|
|
1208
|
+
}
|
|
1036
1209
|
const host = document.getElementById('__rr_picker_host__');
|
|
1037
1210
|
if (host) host.remove();
|
|
1038
1211
|
sendResponse({ success: true });
|
|
1039
|
-
return
|
|
1212
|
+
return false;
|
|
1040
1213
|
} catch (e) {
|
|
1041
1214
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1042
|
-
return
|
|
1215
|
+
return false;
|
|
1043
1216
|
}
|
|
1044
1217
|
}
|
|
1045
1218
|
if (request && request.action === 'generateAccessibilityTree') {
|
|
@@ -1049,10 +1222,10 @@
|
|
|
1049
1222
|
});
|
|
1050
1223
|
if (result && result.error) {
|
|
1051
1224
|
sendResponse({ success: false, error: result.error });
|
|
1052
|
-
return
|
|
1225
|
+
return false;
|
|
1053
1226
|
}
|
|
1054
1227
|
sendResponse({ success: true, ...result });
|
|
1055
|
-
return
|
|
1228
|
+
return false;
|
|
1056
1229
|
}
|
|
1057
1230
|
if (request && request.action === 'ensureRefForSelector') {
|
|
1058
1231
|
try {
|
|
@@ -1081,7 +1254,7 @@
|
|
|
1081
1254
|
success: false,
|
|
1082
1255
|
error: `Composite frame selector not found: ${frameSel}`,
|
|
1083
1256
|
});
|
|
1084
|
-
return
|
|
1257
|
+
return false;
|
|
1085
1258
|
}
|
|
1086
1259
|
const cw = frameEl.contentWindow;
|
|
1087
1260
|
if (!cw) {
|
|
@@ -1089,7 +1262,7 @@
|
|
|
1089
1262
|
success: false,
|
|
1090
1263
|
error: 'Unable to obtain contentWindow of target frame',
|
|
1091
1264
|
});
|
|
1092
|
-
return
|
|
1265
|
+
return false;
|
|
1093
1266
|
}
|
|
1094
1267
|
// Bridge to child frame via postMessage with timeout
|
|
1095
1268
|
const reqId = `rrc_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
@@ -1172,7 +1345,7 @@
|
|
|
1172
1345
|
}
|
|
1173
1346
|
} catch (e) {
|
|
1174
1347
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1175
|
-
return
|
|
1348
|
+
return false;
|
|
1176
1349
|
}
|
|
1177
1350
|
}
|
|
1178
1351
|
// Support CSS selector, XPath, or visible text search
|
|
@@ -1261,63 +1434,53 @@
|
|
|
1261
1434
|
} else if (isXPath) {
|
|
1262
1435
|
if (!sel) {
|
|
1263
1436
|
sendResponse({ success: false, error: 'selector is required' });
|
|
1264
|
-
return
|
|
1437
|
+
return false;
|
|
1265
1438
|
}
|
|
1266
1439
|
const result = queryXPathWithUniquenessCheck(sel, allowMultiple);
|
|
1267
1440
|
if (result.error) {
|
|
1268
1441
|
sendResponse({ success: false, error: result.error });
|
|
1269
|
-
return
|
|
1442
|
+
return false;
|
|
1270
1443
|
}
|
|
1271
1444
|
if (result.matchCount === 0) {
|
|
1272
1445
|
sendResponse({ success: false, error: `selector not found: ${sel}` });
|
|
1273
|
-
return
|
|
1446
|
+
return false;
|
|
1274
1447
|
}
|
|
1275
1448
|
if (!allowMultiple && result.matchCount > 1) {
|
|
1276
1449
|
sendResponse({
|
|
1277
1450
|
success: false,
|
|
1278
1451
|
error: `Selector "${sel}" matched multiple elements. Please refine the selector to match only one element.`,
|
|
1279
1452
|
});
|
|
1280
|
-
return
|
|
1453
|
+
return false;
|
|
1281
1454
|
}
|
|
1282
1455
|
el = result.element;
|
|
1283
1456
|
} else {
|
|
1284
1457
|
if (!sel) {
|
|
1285
1458
|
sendResponse({ success: false, error: 'selector is required' });
|
|
1286
|
-
return
|
|
1459
|
+
return false;
|
|
1287
1460
|
}
|
|
1288
1461
|
const result = querySelectorWithUniquenessCheck(sel, allowMultiple);
|
|
1289
1462
|
if (result.error) {
|
|
1290
1463
|
sendResponse({ success: false, error: result.error });
|
|
1291
|
-
return
|
|
1464
|
+
return false;
|
|
1292
1465
|
}
|
|
1293
1466
|
if (result.matchCount === 0) {
|
|
1294
1467
|
sendResponse({ success: false, error: `selector not found: ${sel}` });
|
|
1295
|
-
return
|
|
1468
|
+
return false;
|
|
1296
1469
|
}
|
|
1297
1470
|
if (!allowMultiple && result.matchCount > 1) {
|
|
1298
1471
|
sendResponse({
|
|
1299
1472
|
success: false,
|
|
1300
1473
|
error: `Selector "${sel}" matched multiple elements. Please refine the selector to match only one element.`,
|
|
1301
1474
|
});
|
|
1302
|
-
return
|
|
1475
|
+
return false;
|
|
1303
1476
|
}
|
|
1304
1477
|
el = result.element;
|
|
1305
1478
|
}
|
|
1306
1479
|
if (!el) {
|
|
1307
1480
|
sendResponse({ success: false, error: `selector not found: ${sel}` });
|
|
1308
|
-
return
|
|
1309
|
-
}
|
|
1310
|
-
let refId = null;
|
|
1311
|
-
for (const k in window.__mcpElementMap) {
|
|
1312
|
-
if (window.__mcpElementMap[k].deref && window.__mcpElementMap[k].deref() === el) {
|
|
1313
|
-
refId = k;
|
|
1314
|
-
break;
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
if (!refId) {
|
|
1318
|
-
refId = `ref_${++window.__mcpRefCounter}`;
|
|
1319
|
-
window.__mcpElementMap[refId] = new WeakRef(el);
|
|
1481
|
+
return false;
|
|
1320
1482
|
}
|
|
1483
|
+
const refId = getOrAssignElementRef(el);
|
|
1321
1484
|
const rect = /** @type {HTMLElement} */ (el).getBoundingClientRect();
|
|
1322
1485
|
sendResponse({
|
|
1323
1486
|
success: true,
|
|
@@ -1327,10 +1490,10 @@
|
|
|
1327
1490
|
y: Math.round(rect.top + rect.height / 2),
|
|
1328
1491
|
},
|
|
1329
1492
|
});
|
|
1330
|
-
return
|
|
1493
|
+
return false;
|
|
1331
1494
|
} catch (e) {
|
|
1332
1495
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1333
|
-
return
|
|
1496
|
+
return false;
|
|
1334
1497
|
}
|
|
1335
1498
|
}
|
|
1336
1499
|
if (request && request.action === 'dispatchHoverForRef') {
|
|
@@ -1347,12 +1510,12 @@
|
|
|
1347
1510
|
const name = String(request.name || '').trim();
|
|
1348
1511
|
if (!sel || !name) {
|
|
1349
1512
|
sendResponse({ success: false, error: 'selector and name are required' });
|
|
1350
|
-
return
|
|
1513
|
+
return false;
|
|
1351
1514
|
}
|
|
1352
1515
|
const el = document.querySelector(sel) || querySelectorDeepFirst(sel);
|
|
1353
1516
|
if (!el) {
|
|
1354
1517
|
sendResponse({ success: false, error: `selector not found: ${sel}` });
|
|
1355
|
-
return
|
|
1518
|
+
return false;
|
|
1356
1519
|
}
|
|
1357
1520
|
let value = null;
|
|
1358
1521
|
if (name === 'text' || name === 'textContent') {
|
|
@@ -1367,10 +1530,10 @@
|
|
|
1367
1530
|
value = el.getAttribute(name);
|
|
1368
1531
|
}
|
|
1369
1532
|
sendResponse({ success: true, value });
|
|
1370
|
-
return
|
|
1533
|
+
return false;
|
|
1371
1534
|
} catch (e) {
|
|
1372
1535
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1373
|
-
return
|
|
1536
|
+
return false;
|
|
1374
1537
|
}
|
|
1375
1538
|
}
|
|
1376
1539
|
if (request && request.action === 'collectVariables') {
|
|
@@ -1396,7 +1559,7 @@
|
|
|
1396
1559
|
values[key] = val;
|
|
1397
1560
|
}
|
|
1398
1561
|
sendResponse({ success: true, values });
|
|
1399
|
-
return
|
|
1562
|
+
return false;
|
|
1400
1563
|
}
|
|
1401
1564
|
// Build overlay form
|
|
1402
1565
|
const hostId = '__rr_var_overlay__';
|
|
@@ -1507,7 +1670,7 @@
|
|
|
1507
1670
|
return true; // async
|
|
1508
1671
|
} catch (e) {
|
|
1509
1672
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1510
|
-
return
|
|
1673
|
+
return false;
|
|
1511
1674
|
}
|
|
1512
1675
|
}
|
|
1513
1676
|
if (request && request.action === 'resolveRef') {
|
|
@@ -1518,7 +1681,7 @@
|
|
|
1518
1681
|
const el = weak && typeof weak.deref === 'function' ? weak.deref() : null;
|
|
1519
1682
|
if (!el || !(el instanceof Element)) {
|
|
1520
1683
|
sendResponse({ success: false, error: `ref "${ref}" not found or expired` });
|
|
1521
|
-
return
|
|
1684
|
+
return false;
|
|
1522
1685
|
}
|
|
1523
1686
|
const rect = /** @type {HTMLElement} */ (el).getBoundingClientRect();
|
|
1524
1687
|
sendResponse({
|
|
@@ -1590,10 +1753,10 @@
|
|
|
1590
1753
|
return generateSelector(el);
|
|
1591
1754
|
})(),
|
|
1592
1755
|
});
|
|
1593
|
-
return
|
|
1756
|
+
return false;
|
|
1594
1757
|
} catch (e) {
|
|
1595
1758
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1596
|
-
return
|
|
1759
|
+
return false;
|
|
1597
1760
|
}
|
|
1598
1761
|
}
|
|
1599
1762
|
if (request && request.action === 'verifyFingerprint') {
|
|
@@ -1602,14 +1765,14 @@
|
|
|
1602
1765
|
const fingerprint = String(request.fingerprint || '').trim();
|
|
1603
1766
|
if (!ref || !fingerprint) {
|
|
1604
1767
|
sendResponse({ success: false, error: 'ref and fingerprint are required' });
|
|
1605
|
-
return
|
|
1768
|
+
return false;
|
|
1606
1769
|
}
|
|
1607
1770
|
const map = window.__mcpElementMap || window.__claudeElementMap;
|
|
1608
1771
|
const weak = map && map[ref];
|
|
1609
1772
|
const el = weak && typeof weak.deref === 'function' ? weak.deref() : null;
|
|
1610
1773
|
if (!el || !(el instanceof Element)) {
|
|
1611
1774
|
sendResponse({ success: false, error: `ref "${ref}" not found or expired` });
|
|
1612
|
-
return
|
|
1775
|
+
return false;
|
|
1613
1776
|
}
|
|
1614
1777
|
// Verify fingerprint: parse stored fingerprint and compare against current element
|
|
1615
1778
|
const parts = fingerprint.split('|');
|
|
@@ -1618,7 +1781,7 @@
|
|
|
1618
1781
|
// Tag names must match
|
|
1619
1782
|
if (storedTag !== currentTag) {
|
|
1620
1783
|
sendResponse({ success: true, match: false });
|
|
1621
|
-
return
|
|
1784
|
+
return false;
|
|
1622
1785
|
}
|
|
1623
1786
|
// If stored fingerprint includes an id, current element must have the same id
|
|
1624
1787
|
const storedIdPart = parts.find((p) => p.startsWith('id='));
|
|
@@ -1627,14 +1790,14 @@
|
|
|
1627
1790
|
const currentId = el.id ? String(el.id).trim() : '';
|
|
1628
1791
|
if (storedId !== currentId) {
|
|
1629
1792
|
sendResponse({ success: true, match: false });
|
|
1630
|
-
return
|
|
1793
|
+
return false;
|
|
1631
1794
|
}
|
|
1632
1795
|
}
|
|
1633
1796
|
sendResponse({ success: true, match: true });
|
|
1634
|
-
return
|
|
1797
|
+
return false;
|
|
1635
1798
|
} catch (e) {
|
|
1636
1799
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1637
|
-
return
|
|
1800
|
+
return false;
|
|
1638
1801
|
}
|
|
1639
1802
|
}
|
|
1640
1803
|
if (request && request.action === 'focusByRef') {
|
|
@@ -1645,7 +1808,7 @@
|
|
|
1645
1808
|
const el = weak && typeof weak.deref === 'function' ? weak.deref() : null;
|
|
1646
1809
|
if (!el || !(el instanceof Element)) {
|
|
1647
1810
|
sendResponse({ success: false, error: `ref "${ref}" not found or expired` });
|
|
1648
|
-
return
|
|
1811
|
+
return false;
|
|
1649
1812
|
}
|
|
1650
1813
|
try {
|
|
1651
1814
|
/** @type {HTMLElement} */ (el).scrollIntoView({
|
|
@@ -1658,15 +1821,15 @@
|
|
|
1658
1821
|
/** @type {HTMLElement} */ (el).focus && /** @type {HTMLElement} */ (el).focus();
|
|
1659
1822
|
} catch {}
|
|
1660
1823
|
sendResponse({ success: true });
|
|
1661
|
-
return
|
|
1824
|
+
return false;
|
|
1662
1825
|
} catch (e) {
|
|
1663
1826
|
sendResponse({ success: false, error: String(e && e.message ? e.message : e) });
|
|
1664
|
-
return
|
|
1827
|
+
return false;
|
|
1665
1828
|
}
|
|
1666
1829
|
}
|
|
1667
1830
|
} catch (e) {
|
|
1668
1831
|
sendResponse({ success: false, error: e && e.message ? e.message : String(e) });
|
|
1669
|
-
return
|
|
1832
|
+
return false;
|
|
1670
1833
|
}
|
|
1671
1834
|
return false;
|
|
1672
1835
|
});
|
|
@@ -1846,22 +2009,7 @@
|
|
|
1846
2009
|
respond({ success: false, error: 'Element not found in child frame' });
|
|
1847
2010
|
return;
|
|
1848
2011
|
}
|
|
1849
|
-
|
|
1850
|
-
if (!window.__mcpRefCounter) window.__mcpRefCounter = window.__claudeRefCounter || 0;
|
|
1851
|
-
window.__claudeElementMap = window.__mcpElementMap;
|
|
1852
|
-
window.__claudeRefCounter = window.__mcpRefCounter;
|
|
1853
|
-
let refId = null;
|
|
1854
|
-
for (const k in window.__mcpElementMap) {
|
|
1855
|
-
const w = window.__mcpElementMap[k];
|
|
1856
|
-
if (w && typeof w.deref === 'function' && w.deref && w.deref() === el) {
|
|
1857
|
-
refId = k;
|
|
1858
|
-
break;
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
if (!refId) {
|
|
1862
|
-
refId = `ref_${++window.__mcpRefCounter}`;
|
|
1863
|
-
window.__mcpElementMap[refId] = new WeakRef(el);
|
|
1864
|
-
}
|
|
2012
|
+
const refId = getOrAssignElementRef(el);
|
|
1865
2013
|
const rect = el.getBoundingClientRect();
|
|
1866
2014
|
respond({
|
|
1867
2015
|
success: true,
|