mcp-browser-dev-tools 0.0.1 → 0.0.2
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/LICENSE +21 -0
- package/README.md +102 -140
- package/package.json +6 -1
- package/src/app.mjs +31 -2
- package/src/browser-adapter.mjs +57 -0
- package/src/browser-launch-service.mjs +515 -0
- package/src/cdp-client.mjs +420 -5
- package/src/cli.mjs +69 -204
- package/src/config.mjs +26 -1
- package/src/firefox-bidi-client.mjs +251 -16
- package/src/index.mjs +3 -0
- package/src/logger.mjs +53 -0
- package/src/mcp-server.mjs +709 -12
- package/src/page-context.mjs +217 -0
- package/src/serve-bootstrap.mjs +290 -0
- package/src/session-events.mjs +83 -0
- package/src/tcp-relay.mjs +7 -1
- package/src/wait-for.mjs +201 -0
package/src/wait-for.mjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
function sleep(ms) {
|
|
2
|
+
return new Promise((resolve) => {
|
|
3
|
+
setTimeout(resolve, ms);
|
|
4
|
+
});
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const READY_STATE_ORDER = {
|
|
8
|
+
loading: 0,
|
|
9
|
+
interactive: 1,
|
|
10
|
+
complete: 2,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function normalizeOptionalString(value) {
|
|
14
|
+
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function normalizeSelectorState(value) {
|
|
18
|
+
if (value === "present" || value === "visible" || value === "hidden") {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return "visible";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function normalizeReadyState(value) {
|
|
26
|
+
if (value === "interactive" || value === "complete") {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function normalizeInteger(value, fallback) {
|
|
34
|
+
return Number.isInteger(value) && value > 0 ? value : fallback;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function readyStateMatches(current, expected) {
|
|
38
|
+
const currentRank = READY_STATE_ORDER[current] ?? -1;
|
|
39
|
+
const expectedRank = READY_STATE_ORDER[expected] ?? -1;
|
|
40
|
+
return currentRank >= expectedRank;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function selectorMatches(result, state) {
|
|
44
|
+
if (state === "hidden") {
|
|
45
|
+
return !result?.found || result.node?.visible === false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!result?.found) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (state === "present") {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return result.node?.visible === true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function buildTimeoutMessage(options, lastPage, lastElement) {
|
|
60
|
+
const conditions = [];
|
|
61
|
+
if (options.selector) {
|
|
62
|
+
conditions.push(
|
|
63
|
+
`selector ${JSON.stringify(options.selector)} to become ${options.state}`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (options.url) {
|
|
67
|
+
conditions.push(`url to equal ${JSON.stringify(options.url)}`);
|
|
68
|
+
}
|
|
69
|
+
if (options.urlIncludes) {
|
|
70
|
+
conditions.push(`url to include ${JSON.stringify(options.urlIncludes)}`);
|
|
71
|
+
}
|
|
72
|
+
if (options.readyState) {
|
|
73
|
+
conditions.push(`readyState to reach ${options.readyState}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const observed = [];
|
|
77
|
+
if (lastPage?.url) {
|
|
78
|
+
observed.push(`last url=${JSON.stringify(lastPage.url)}`);
|
|
79
|
+
}
|
|
80
|
+
if (lastPage?.readyState) {
|
|
81
|
+
observed.push(`last readyState=${lastPage.readyState}`);
|
|
82
|
+
}
|
|
83
|
+
if (options.selector && lastElement) {
|
|
84
|
+
observed.push(
|
|
85
|
+
`last selector found=${Boolean(lastElement.found)} visible=${lastElement.node?.visible === true}`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return `Timed out after ${options.timeoutMs}ms waiting for ${conditions.join(
|
|
90
|
+
", ",
|
|
91
|
+
)}${observed.length > 0 ? `; ${observed.join("; ")}` : ""}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function normalizeWaitForOptions(options = {}) {
|
|
95
|
+
const normalized = {
|
|
96
|
+
selector: normalizeOptionalString(options.selector),
|
|
97
|
+
state: normalizeSelectorState(options.state),
|
|
98
|
+
url: normalizeOptionalString(options.url),
|
|
99
|
+
urlIncludes: normalizeOptionalString(options.urlIncludes),
|
|
100
|
+
readyState: normalizeReadyState(options.readyState),
|
|
101
|
+
timeoutMs: normalizeInteger(options.timeoutMs, 10_000),
|
|
102
|
+
pollIntervalMs: normalizeInteger(options.pollIntervalMs, 100),
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
if (
|
|
106
|
+
!normalized.selector &&
|
|
107
|
+
!normalized.url &&
|
|
108
|
+
!normalized.urlIncludes &&
|
|
109
|
+
!normalized.readyState
|
|
110
|
+
) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
"wait_for requires at least one of selector, url, urlIncludes, or readyState",
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!normalized.selector && options.state !== undefined) {
|
|
117
|
+
throw new Error("wait_for state requires selector");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return normalized;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function waitForPageCondition({
|
|
124
|
+
getPageState,
|
|
125
|
+
inspectElement,
|
|
126
|
+
options,
|
|
127
|
+
}) {
|
|
128
|
+
const normalized = normalizeWaitForOptions(options);
|
|
129
|
+
const startedAt = Date.now();
|
|
130
|
+
let attempts = 0;
|
|
131
|
+
let lastPage = null;
|
|
132
|
+
let lastElement = null;
|
|
133
|
+
|
|
134
|
+
while (true) {
|
|
135
|
+
attempts += 1;
|
|
136
|
+
|
|
137
|
+
if (normalized.url || normalized.urlIncludes || normalized.readyState) {
|
|
138
|
+
lastPage = await getPageState();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (normalized.selector) {
|
|
142
|
+
lastElement = await inspectElement(normalized.selector);
|
|
143
|
+
if (lastElement?.error) {
|
|
144
|
+
throw new Error(lastElement.error);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const matchedSelector = normalized.selector
|
|
149
|
+
? selectorMatches(lastElement, normalized.state)
|
|
150
|
+
: true;
|
|
151
|
+
const matchedUrl = normalized.url ? lastPage?.url === normalized.url : true;
|
|
152
|
+
const matchedUrlIncludes = normalized.urlIncludes
|
|
153
|
+
? lastPage?.url?.includes(normalized.urlIncludes) === true
|
|
154
|
+
: true;
|
|
155
|
+
const matchedReadyState = normalized.readyState
|
|
156
|
+
? readyStateMatches(lastPage?.readyState, normalized.readyState)
|
|
157
|
+
: true;
|
|
158
|
+
|
|
159
|
+
if (
|
|
160
|
+
matchedSelector &&
|
|
161
|
+
matchedUrl &&
|
|
162
|
+
matchedUrlIncludes &&
|
|
163
|
+
matchedReadyState
|
|
164
|
+
) {
|
|
165
|
+
const browserFamily =
|
|
166
|
+
lastPage?.browserFamily ?? lastElement?.browserFamily ?? null;
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
browserFamily,
|
|
170
|
+
matched: true,
|
|
171
|
+
waitedMs: Date.now() - startedAt,
|
|
172
|
+
attempts,
|
|
173
|
+
timeoutMs: normalized.timeoutMs,
|
|
174
|
+
pollIntervalMs: normalized.pollIntervalMs,
|
|
175
|
+
condition: {
|
|
176
|
+
selector: normalized.selector,
|
|
177
|
+
state: normalized.selector ? normalized.state : null,
|
|
178
|
+
url: normalized.url,
|
|
179
|
+
urlIncludes: normalized.urlIncludes,
|
|
180
|
+
readyState: normalized.readyState,
|
|
181
|
+
},
|
|
182
|
+
page: lastPage,
|
|
183
|
+
element: normalized.selector
|
|
184
|
+
? {
|
|
185
|
+
selector: normalized.selector,
|
|
186
|
+
found: Boolean(lastElement?.found),
|
|
187
|
+
locator:
|
|
188
|
+
lastElement?.locator ?? lastElement?.node?.locator ?? null,
|
|
189
|
+
node: lastElement?.found ? lastElement.node : null,
|
|
190
|
+
}
|
|
191
|
+
: null,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (Date.now() - startedAt >= normalized.timeoutMs) {
|
|
196
|
+
throw new Error(buildTimeoutMessage(normalized, lastPage, lastElement));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
await sleep(normalized.pollIntervalMs);
|
|
200
|
+
}
|
|
201
|
+
}
|