@wordbricks/playwright-mcp 0.1.24 → 0.1.26
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/lib/browserContextFactory.js +399 -0
- package/lib/browserServerBackend.js +86 -0
- package/lib/config.js +300 -0
- package/lib/context.js +311 -0
- package/lib/extension/cdpRelay.js +352 -0
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/frameworkPatterns.js +35 -0
- package/lib/hooks/antiBotDetectionHook.js +178 -0
- package/lib/hooks/core.js +145 -0
- package/lib/hooks/eventConsumer.js +52 -0
- package/lib/hooks/events.js +42 -0
- package/lib/hooks/formatToolCallEvent.js +12 -0
- package/lib/hooks/frameworkStateHook.js +182 -0
- package/lib/hooks/grouping.js +72 -0
- package/lib/hooks/jsonLdDetectionHook.js +182 -0
- package/lib/hooks/networkFilters.js +82 -0
- package/lib/hooks/networkSetup.js +61 -0
- package/lib/hooks/networkTrackingHook.js +67 -0
- package/lib/hooks/pageHeightHook.js +75 -0
- package/lib/hooks/registry.js +41 -0
- package/lib/hooks/requireTabHook.js +26 -0
- package/lib/hooks/schema.js +89 -0
- package/lib/hooks/waitHook.js +33 -0
- package/lib/index.js +41 -0
- package/lib/mcp/inProcessTransport.js +71 -0
- package/lib/mcp/proxyBackend.js +130 -0
- package/lib/mcp/server.js +91 -0
- package/lib/mcp/tool.js +44 -0
- package/lib/mcp/transport.js +188 -0
- package/lib/playwrightTransformer.js +520 -0
- package/lib/program.js +112 -0
- package/lib/response.js +192 -0
- package/lib/sessionLog.js +123 -0
- package/lib/tab.js +251 -0
- package/lib/tools/common.js +55 -0
- package/lib/tools/console.js +33 -0
- package/lib/tools/dialogs.js +50 -0
- package/lib/tools/evaluate.js +62 -0
- package/lib/tools/extractFrameworkState.js +225 -0
- package/lib/tools/files.js +48 -0
- package/lib/tools/form.js +66 -0
- package/lib/tools/getSnapshot.js +36 -0
- package/lib/tools/getVisibleHtml.js +68 -0
- package/lib/tools/install.js +51 -0
- package/lib/tools/keyboard.js +83 -0
- package/lib/tools/mouse.js +97 -0
- package/lib/tools/navigate.js +66 -0
- package/lib/tools/network.js +121 -0
- package/lib/tools/networkDetail.js +238 -0
- package/lib/tools/networkSearch/bodySearch.js +161 -0
- package/lib/tools/networkSearch/grouping.js +37 -0
- package/lib/tools/networkSearch/helpers.js +32 -0
- package/lib/tools/networkSearch/searchHtml.js +76 -0
- package/lib/tools/networkSearch/types.js +1 -0
- package/lib/tools/networkSearch/urlSearch.js +124 -0
- package/lib/tools/networkSearch.js +278 -0
- package/lib/tools/pdf.js +41 -0
- package/lib/tools/repl.js +414 -0
- package/lib/tools/screenshot.js +103 -0
- package/lib/tools/scroll.js +131 -0
- package/lib/tools/snapshot.js +161 -0
- package/lib/tools/tabs.js +62 -0
- package/lib/tools/tool.js +35 -0
- package/lib/tools/utils.js +78 -0
- package/lib/tools/wait.js +60 -0
- package/lib/tools.js +68 -0
- package/lib/utils/adBlockFilter.js +90 -0
- package/lib/utils/codegen.js +55 -0
- package/lib/utils/extensionPath.js +10 -0
- package/lib/utils/fileUtils.js +40 -0
- package/lib/utils/graphql.js +269 -0
- package/lib/utils/guid.js +22 -0
- package/lib/utils/httpServer.js +39 -0
- package/lib/utils/log.js +21 -0
- package/lib/utils/manualPromise.js +111 -0
- package/lib/utils/networkFormat.js +14 -0
- package/lib/utils/package.js +20 -0
- package/lib/utils/result.js +2 -0
- package/lib/utils/sanitizeHtml.js +130 -0
- package/lib/utils/truncate.js +103 -0
- package/lib/utils/withTimeout.js +7 -0
- package/package.json +11 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { defineTabTool } from "./tool.js";
|
|
18
|
+
export const handleDialog = defineTabTool({
|
|
19
|
+
capability: "core",
|
|
20
|
+
schema: {
|
|
21
|
+
name: "browser_handle_dialog",
|
|
22
|
+
title: "Handle a dialog",
|
|
23
|
+
description: "Handle a dialog",
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
accept: z.boolean().describe("Whether to accept the dialog."),
|
|
26
|
+
promptText: z
|
|
27
|
+
.string()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe("The text of the prompt in case of a prompt dialog."),
|
|
30
|
+
}),
|
|
31
|
+
type: "action",
|
|
32
|
+
},
|
|
33
|
+
handle: async (tab, params, response) => {
|
|
34
|
+
response.setIncludeSnapshot();
|
|
35
|
+
const dialogState = tab
|
|
36
|
+
.modalStates()
|
|
37
|
+
.find((state) => state.type === "dialog");
|
|
38
|
+
if (!dialogState)
|
|
39
|
+
throw new Error("No dialog visible");
|
|
40
|
+
tab.clearModalState(dialogState);
|
|
41
|
+
await tab.waitForCompletion(async () => {
|
|
42
|
+
if (params.accept)
|
|
43
|
+
await dialogState.dialog.accept(params.promptText);
|
|
44
|
+
else
|
|
45
|
+
await dialogState.dialog.dismiss();
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
clearsModalState: "dialog",
|
|
49
|
+
});
|
|
50
|
+
export default [handleDialog];
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import * as javascript from "../utils/codegen.js";
|
|
18
|
+
import { defineTabTool } from "./tool.js";
|
|
19
|
+
import { generateLocator } from "./utils.js";
|
|
20
|
+
const evaluateSchema = z.object({
|
|
21
|
+
function: z
|
|
22
|
+
.string()
|
|
23
|
+
.describe("() => { /* code */ } or (element) => { /* code */ } when element is provided"),
|
|
24
|
+
element: z
|
|
25
|
+
.string()
|
|
26
|
+
.optional()
|
|
27
|
+
.describe("Human-readable element description used to obtain permission to interact with the element"),
|
|
28
|
+
ref: z
|
|
29
|
+
.string()
|
|
30
|
+
.optional()
|
|
31
|
+
.describe("Exact target element reference from the page snapshot"),
|
|
32
|
+
});
|
|
33
|
+
const evaluate = defineTabTool({
|
|
34
|
+
capability: "core",
|
|
35
|
+
schema: {
|
|
36
|
+
name: "browser_evaluate",
|
|
37
|
+
title: "Evaluate JavaScript",
|
|
38
|
+
description: "Evaluate JavaScript expression on page or element",
|
|
39
|
+
inputSchema: evaluateSchema,
|
|
40
|
+
type: "action",
|
|
41
|
+
},
|
|
42
|
+
handle: async (tab, params, response) => {
|
|
43
|
+
response.setIncludeSnapshot();
|
|
44
|
+
let locator;
|
|
45
|
+
if (params.ref && params.element) {
|
|
46
|
+
locator = await tab.refLocator({
|
|
47
|
+
ref: params.ref,
|
|
48
|
+
element: params.element,
|
|
49
|
+
});
|
|
50
|
+
response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
|
|
54
|
+
}
|
|
55
|
+
await tab.waitForCompletion(async () => {
|
|
56
|
+
const receiver = locator ?? tab.page;
|
|
57
|
+
const result = await receiver._evaluateFunction(params.function);
|
|
58
|
+
response.addResult(JSON.stringify(result, null, 2) || "undefined");
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
export default [evaluate];
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { FRAMEWORK_STATE_PATTERNS, MAX_DISPLAY_ITEMS, } from "../frameworkPatterns.js";
|
|
3
|
+
import { truncate } from "../utils/truncate.js";
|
|
4
|
+
import { defineTabTool } from "./tool.js";
|
|
5
|
+
const extractFrameworkStateSchema = z.object({
|
|
6
|
+
framework: z
|
|
7
|
+
.string()
|
|
8
|
+
.optional()
|
|
9
|
+
.describe('Optional: specific key to extract from (e.g., "__remixContext")'),
|
|
10
|
+
path: z
|
|
11
|
+
.string()
|
|
12
|
+
.optional()
|
|
13
|
+
.describe('Path to extract (e.g., "state.loaderData")'),
|
|
14
|
+
});
|
|
15
|
+
const extractFrameworkState = defineTabTool({
|
|
16
|
+
capability: "core",
|
|
17
|
+
schema: {
|
|
18
|
+
name: "browser_extract_framework_state",
|
|
19
|
+
title: "Extract framework state",
|
|
20
|
+
description: "Extract framework state data from web pages (React, Redux, Remix, etc.)",
|
|
21
|
+
inputSchema: extractFrameworkStateSchema,
|
|
22
|
+
type: "readOnly",
|
|
23
|
+
},
|
|
24
|
+
handle: async (tab, params, response) => {
|
|
25
|
+
const { framework, // Optional: specific key to extract from
|
|
26
|
+
path, // Path to extract
|
|
27
|
+
} = params;
|
|
28
|
+
const result = await tab.page.evaluate(({ targetKey, targetPath, patterns }) => {
|
|
29
|
+
const foundStates = [];
|
|
30
|
+
const errors = [];
|
|
31
|
+
// If specific key requested, only check that one
|
|
32
|
+
const keysToCheck = targetKey ? [targetKey] : patterns;
|
|
33
|
+
// Check each pattern
|
|
34
|
+
for (const key of keysToCheck) {
|
|
35
|
+
if (key in window) {
|
|
36
|
+
try {
|
|
37
|
+
let data = window[key];
|
|
38
|
+
// If data is string, try to parse as JSON
|
|
39
|
+
if (typeof data === "string") {
|
|
40
|
+
try {
|
|
41
|
+
data = JSON.parse(data);
|
|
42
|
+
}
|
|
43
|
+
catch (parseErr) {
|
|
44
|
+
throw new Error(`Failed to parse string as JSON: ${parseErr.message}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Apply path if provided
|
|
48
|
+
if (targetPath && data) {
|
|
49
|
+
// Split path while respecting bracket-quoted segments like ['a.b']
|
|
50
|
+
const splitPath = (p) => {
|
|
51
|
+
const parts = [];
|
|
52
|
+
let cur = "";
|
|
53
|
+
let inBracket = false;
|
|
54
|
+
let inQuote = null;
|
|
55
|
+
for (let i = 0; i < p.length; i++) {
|
|
56
|
+
const ch = p[i];
|
|
57
|
+
if (inQuote) {
|
|
58
|
+
cur += ch;
|
|
59
|
+
if (ch === inQuote && p[i - 1] !== "\\")
|
|
60
|
+
inQuote = null;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (inBracket) {
|
|
64
|
+
cur += ch;
|
|
65
|
+
if (ch === '"' || ch === "'") {
|
|
66
|
+
inQuote = ch;
|
|
67
|
+
}
|
|
68
|
+
else if (ch === "]") {
|
|
69
|
+
inBracket = false;
|
|
70
|
+
parts.push(cur);
|
|
71
|
+
cur = "";
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (ch === ".") {
|
|
76
|
+
if (cur) {
|
|
77
|
+
parts.push(cur);
|
|
78
|
+
cur = "";
|
|
79
|
+
}
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (ch === "[") {
|
|
83
|
+
if (cur) {
|
|
84
|
+
parts.push(cur);
|
|
85
|
+
cur = "";
|
|
86
|
+
}
|
|
87
|
+
cur = "[";
|
|
88
|
+
inBracket = true;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
cur += ch;
|
|
92
|
+
}
|
|
93
|
+
if (cur)
|
|
94
|
+
parts.push(cur);
|
|
95
|
+
return parts;
|
|
96
|
+
};
|
|
97
|
+
const pathParts = splitPath(targetPath);
|
|
98
|
+
for (const part of pathParts) {
|
|
99
|
+
if (data && typeof data === "object") {
|
|
100
|
+
// Handle array index with prop: prop[123]
|
|
101
|
+
const arrayMatch = part.match(/^(.+)\[(\d+)\]$/);
|
|
102
|
+
if (arrayMatch) {
|
|
103
|
+
const prop = arrayMatch[1];
|
|
104
|
+
const index = Number.parseInt(arrayMatch[2], 10);
|
|
105
|
+
const propData = data[prop];
|
|
106
|
+
data = Array.isArray(propData)
|
|
107
|
+
? propData[index]
|
|
108
|
+
: undefined;
|
|
109
|
+
}
|
|
110
|
+
else if (part.match(/^\[(\d+)\]$/)) {
|
|
111
|
+
// Handle standalone array index: [123]
|
|
112
|
+
const indexMatch = part.match(/^\[(\d+)\]$/);
|
|
113
|
+
if (indexMatch) {
|
|
114
|
+
const index = Number.parseInt(indexMatch[1], 10);
|
|
115
|
+
data = data?.[index];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
// Handle quoted key: ['key.with.dot'] or ["key.with.dot"]
|
|
120
|
+
const bracketMatch = part.match(/^\[['"]([^'"]+)['"]\]$/);
|
|
121
|
+
if (bracketMatch)
|
|
122
|
+
data = data[bracketMatch[1]];
|
|
123
|
+
else
|
|
124
|
+
data = data[part];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
data = undefined;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (data !== undefined) {
|
|
134
|
+
foundStates.push({
|
|
135
|
+
key: key,
|
|
136
|
+
data: data,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
errors.push(`${key}: ${e instanceof Error ? e.message : String(e)}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
count: foundStates.length,
|
|
147
|
+
states: foundStates,
|
|
148
|
+
errors: errors.length > 0 ? errors : undefined,
|
|
149
|
+
};
|
|
150
|
+
}, {
|
|
151
|
+
targetKey: framework,
|
|
152
|
+
targetPath: path,
|
|
153
|
+
patterns: FRAMEWORK_STATE_PATTERNS,
|
|
154
|
+
});
|
|
155
|
+
// Extract data from results
|
|
156
|
+
if (result.states.length === 0) {
|
|
157
|
+
if (framework) {
|
|
158
|
+
if (path) {
|
|
159
|
+
throw new Error(`Path "${path}" not found in framework state '${framework}'.`);
|
|
160
|
+
}
|
|
161
|
+
throw new Error(`Key '${framework}' not found in window. ` +
|
|
162
|
+
"Available framework states can be detected by the frameworkStateHook.");
|
|
163
|
+
}
|
|
164
|
+
throw new Error("No framework state found. " +
|
|
165
|
+
"Available framework states can be detected by the frameworkStateHook.");
|
|
166
|
+
}
|
|
167
|
+
// Prepare final data
|
|
168
|
+
let finalData;
|
|
169
|
+
const extractedKeys = result.states.map((s) => s.key);
|
|
170
|
+
if (result.states.length === 1) {
|
|
171
|
+
finalData = result.states[0].data;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
finalData = {};
|
|
175
|
+
for (const state of result.states)
|
|
176
|
+
finalData[state.key] = state.data;
|
|
177
|
+
}
|
|
178
|
+
// Limit to MAX_DISPLAY_ITEMS for display if array
|
|
179
|
+
const totalItems = Array.isArray(finalData)
|
|
180
|
+
? finalData.length
|
|
181
|
+
: typeof finalData === "object" && finalData !== null
|
|
182
|
+
? Object.keys(finalData).length
|
|
183
|
+
: 1;
|
|
184
|
+
let displayData = finalData;
|
|
185
|
+
let isTruncated = false;
|
|
186
|
+
if (Array.isArray(finalData) && finalData.length > MAX_DISPLAY_ITEMS) {
|
|
187
|
+
displayData = finalData.slice(0, MAX_DISPLAY_ITEMS);
|
|
188
|
+
isTruncated = true;
|
|
189
|
+
}
|
|
190
|
+
else if (typeof finalData === "object" &&
|
|
191
|
+
finalData !== null &&
|
|
192
|
+
!Array.isArray(finalData)) {
|
|
193
|
+
const keys = Object.keys(finalData);
|
|
194
|
+
if (keys.length > MAX_DISPLAY_ITEMS) {
|
|
195
|
+
displayData = {};
|
|
196
|
+
keys.slice(0, MAX_DISPLAY_ITEMS).forEach((key) => {
|
|
197
|
+
displayData[key] = finalData[key];
|
|
198
|
+
});
|
|
199
|
+
isTruncated = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Truncate for readability and cap output
|
|
203
|
+
const truncatedDisplay = truncate(displayData, {
|
|
204
|
+
maxStringLength: 1000,
|
|
205
|
+
maxItems: MAX_DISPLAY_ITEMS,
|
|
206
|
+
});
|
|
207
|
+
const outputStr = JSON.stringify(truncatedDisplay, null, 2);
|
|
208
|
+
const summary = [
|
|
209
|
+
`Found ${result.count} framework state(s)`,
|
|
210
|
+
result.states.length > 1
|
|
211
|
+
? `Extracted from: ${extractedKeys.join(", ")}`
|
|
212
|
+
: `Using: ${result.states[0]?.key}`,
|
|
213
|
+
isTruncated
|
|
214
|
+
? `Showing first ${MAX_DISPLAY_ITEMS} of ${totalItems} results`
|
|
215
|
+
: null,
|
|
216
|
+
path ? `Extracted path: ${path}` : null,
|
|
217
|
+
result.errors ? `Errors: ${result.errors.join("; ")}` : null,
|
|
218
|
+
]
|
|
219
|
+
.filter(Boolean)
|
|
220
|
+
.join("\n");
|
|
221
|
+
response.addResult(summary + "\n\n" + outputStr);
|
|
222
|
+
response.setIncludeSnapshot();
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
export default [extractFrameworkState];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { defineTabTool } from "./tool.js";
|
|
18
|
+
export const uploadFile = defineTabTool({
|
|
19
|
+
capability: "core",
|
|
20
|
+
schema: {
|
|
21
|
+
name: "browser_file_upload",
|
|
22
|
+
title: "Upload files",
|
|
23
|
+
description: "Upload one or multiple files",
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
paths: z
|
|
26
|
+
.array(z.string())
|
|
27
|
+
.optional()
|
|
28
|
+
.describe("The absolute paths to the files to upload. Can be single file or multiple files. If omitted, file chooser is cancelled."),
|
|
29
|
+
}),
|
|
30
|
+
type: "action",
|
|
31
|
+
},
|
|
32
|
+
handle: async (tab, params, response) => {
|
|
33
|
+
response.setIncludeSnapshot();
|
|
34
|
+
const modalState = tab
|
|
35
|
+
.modalStates()
|
|
36
|
+
.find((state) => state.type === "fileChooser");
|
|
37
|
+
if (!modalState)
|
|
38
|
+
throw new Error("No file chooser visible");
|
|
39
|
+
response.addCode(`await fileChooser.setFiles(${JSON.stringify(params.paths)})`);
|
|
40
|
+
tab.clearModalState(modalState);
|
|
41
|
+
await tab.waitForCompletion(async () => {
|
|
42
|
+
if (params.paths)
|
|
43
|
+
await modalState.fileChooser.setFiles(params.paths);
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
clearsModalState: "fileChooser",
|
|
47
|
+
});
|
|
48
|
+
export default [uploadFile];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import * as codegen from "../utils/codegen.js";
|
|
18
|
+
import { defineTabTool } from "./tool.js";
|
|
19
|
+
import { generateLocator } from "./utils.js";
|
|
20
|
+
const fillForm = defineTabTool({
|
|
21
|
+
capability: "core",
|
|
22
|
+
schema: {
|
|
23
|
+
name: "browser_fill_form",
|
|
24
|
+
title: "Fill form",
|
|
25
|
+
description: "Fill multiple form fields",
|
|
26
|
+
inputSchema: z.object({
|
|
27
|
+
fields: z
|
|
28
|
+
.array(z.object({
|
|
29
|
+
name: z.string().describe("Human-readable field name"),
|
|
30
|
+
type: z
|
|
31
|
+
.enum(["textbox", "checkbox", "radio", "combobox", "slider"])
|
|
32
|
+
.describe("Type of the field"),
|
|
33
|
+
ref: z
|
|
34
|
+
.string()
|
|
35
|
+
.describe("Exact target field reference from the page snapshot"),
|
|
36
|
+
value: z
|
|
37
|
+
.string()
|
|
38
|
+
.describe("Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option."),
|
|
39
|
+
}))
|
|
40
|
+
.describe("Fields to fill in"),
|
|
41
|
+
}),
|
|
42
|
+
type: "input",
|
|
43
|
+
},
|
|
44
|
+
handle: async (tab, params, response) => {
|
|
45
|
+
for (const field of params.fields) {
|
|
46
|
+
const locator = await tab.refLocator({
|
|
47
|
+
element: field.name,
|
|
48
|
+
ref: field.ref,
|
|
49
|
+
});
|
|
50
|
+
if (field.type === "textbox" || field.type === "slider") {
|
|
51
|
+
const secret = tab.context.lookupSecret(field.value);
|
|
52
|
+
await locator.fill(secret.value);
|
|
53
|
+
response.addCode(`await page.${await generateLocator(locator)}.fill(${secret.code});`);
|
|
54
|
+
}
|
|
55
|
+
else if (field.type === "checkbox" || field.type === "radio") {
|
|
56
|
+
await locator.setChecked(field.value === "true");
|
|
57
|
+
response.addCode(`await page.${await generateLocator(locator)}.setChecked(${field.value});`);
|
|
58
|
+
}
|
|
59
|
+
else if (field.type === "combobox") {
|
|
60
|
+
await locator.selectOption({ label: field.value });
|
|
61
|
+
response.addCode(`await page.${await generateLocator(locator)}.selectOption(${codegen.quote(field.value)});`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
export default [fillForm];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { truncate } from "../utils/truncate.js";
|
|
3
|
+
import { defineTool } from "./tool.js";
|
|
4
|
+
const MAX_LENGTH = 100_000;
|
|
5
|
+
const getSnapshotSchema = z.object({});
|
|
6
|
+
const getSnapshot = defineTool({
|
|
7
|
+
capability: "core",
|
|
8
|
+
schema: {
|
|
9
|
+
name: "browser_get_snapshot",
|
|
10
|
+
title: "Get snapshot",
|
|
11
|
+
description: "Get accessibility snapshot of the current page",
|
|
12
|
+
inputSchema: getSnapshotSchema,
|
|
13
|
+
type: "readOnly",
|
|
14
|
+
},
|
|
15
|
+
handle: async (context, _params, response) => {
|
|
16
|
+
const tab = await context.ensureTab();
|
|
17
|
+
try {
|
|
18
|
+
const snapshot = await tab.captureSnapshot();
|
|
19
|
+
const lines = [];
|
|
20
|
+
lines.push(`### Page state`);
|
|
21
|
+
lines.push(`- Page URL: ${snapshot.url}`);
|
|
22
|
+
lines.push(`- Page Title: ${snapshot.title}`);
|
|
23
|
+
lines.push(`- Page Snapshot:`);
|
|
24
|
+
lines.push("```yaml");
|
|
25
|
+
let aria = snapshot.ariaSnapshot || "";
|
|
26
|
+
aria = String(truncate(aria, { maxStringLength: MAX_LENGTH }));
|
|
27
|
+
lines.push(aria);
|
|
28
|
+
lines.push("```");
|
|
29
|
+
response.addResult(lines.join("\n"));
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
response.addResult(`Failed to get snapshot: ${String(error)}`);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
export default [getSnapshot];
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { sanitizeHtml } from "../utils/sanitizeHtml.js";
|
|
3
|
+
import { defineTool } from "./tool.js";
|
|
4
|
+
const getVisibleHtmlSchema = z.object({
|
|
5
|
+
selector: z
|
|
6
|
+
.string()
|
|
7
|
+
.optional()
|
|
8
|
+
.describe("CSS selector for a specific element (optional)"),
|
|
9
|
+
cleanHtml: z
|
|
10
|
+
.boolean()
|
|
11
|
+
.optional()
|
|
12
|
+
.default(true)
|
|
13
|
+
.describe("Clean HTML (true) or raw HTML (false)"),
|
|
14
|
+
removeScripts: z
|
|
15
|
+
.boolean()
|
|
16
|
+
.optional()
|
|
17
|
+
.default(true)
|
|
18
|
+
.describe("Remove scripts (true) or keep them (false)"),
|
|
19
|
+
removeStyles: z
|
|
20
|
+
.boolean()
|
|
21
|
+
.optional()
|
|
22
|
+
.default(true)
|
|
23
|
+
.describe("Remove styles (true) or keep them (false)"),
|
|
24
|
+
});
|
|
25
|
+
const getVisibleHtml = defineTool({
|
|
26
|
+
capability: "core",
|
|
27
|
+
schema: {
|
|
28
|
+
name: "browser_get_visible_html",
|
|
29
|
+
title: "Get visible HTML",
|
|
30
|
+
description: "Get HTML content of the page or a specific element",
|
|
31
|
+
inputSchema: getVisibleHtmlSchema,
|
|
32
|
+
type: "readOnly",
|
|
33
|
+
},
|
|
34
|
+
handle: async (context, params, response) => {
|
|
35
|
+
const tab = await context.ensureTab();
|
|
36
|
+
const page = tab.page;
|
|
37
|
+
try {
|
|
38
|
+
const { selector, cleanHtml, removeScripts, removeStyles } = params;
|
|
39
|
+
// Get the HTML content
|
|
40
|
+
let htmlContent;
|
|
41
|
+
if (selector) {
|
|
42
|
+
// If a selector is provided, get only the HTML for that element
|
|
43
|
+
const element = await page.$(selector);
|
|
44
|
+
if (!element)
|
|
45
|
+
throw new Error(`Element with selector "${selector}" not found`);
|
|
46
|
+
htmlContent = await page.evaluate((el) => el.outerHTML, element);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Otherwise get the full page HTML
|
|
50
|
+
htmlContent = await page.content();
|
|
51
|
+
}
|
|
52
|
+
// Determine if we need to apply filters
|
|
53
|
+
const shouldRemoveScripts = removeScripts || cleanHtml;
|
|
54
|
+
const shouldRemoveStyles = removeStyles || cleanHtml;
|
|
55
|
+
// Apply filters server-side to avoid TrustedHTML/Trusted Types restrictions in page context
|
|
56
|
+
if (shouldRemoveScripts || shouldRemoveStyles)
|
|
57
|
+
htmlContent = sanitizeHtml(htmlContent, {
|
|
58
|
+
shouldRemoveScripts,
|
|
59
|
+
shouldRemoveStyles,
|
|
60
|
+
});
|
|
61
|
+
response.addResult(`HTML content:\n${htmlContent}`);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
response.addResult(`Failed to get visible HTML content: ${error.message}`);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
export default [getVisibleHtml];
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { fork } from "child_process";
|
|
17
|
+
import path from "path";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
import { defineTool } from "./tool.js";
|
|
20
|
+
const install = defineTool({
|
|
21
|
+
capability: "core-install",
|
|
22
|
+
schema: {
|
|
23
|
+
name: "browser_install",
|
|
24
|
+
title: "Install the browser specified in the config",
|
|
25
|
+
description: "Install the browser specified in the config. Call this if you get an error about the browser not being installed.",
|
|
26
|
+
inputSchema: z.object({}),
|
|
27
|
+
type: "action",
|
|
28
|
+
},
|
|
29
|
+
handle: async (context, params, response) => {
|
|
30
|
+
const channel = context.config.browser?.launchOptions?.channel ??
|
|
31
|
+
context.config.browser?.browserName ??
|
|
32
|
+
"chrome";
|
|
33
|
+
const cliPath = path.join(require.resolve("playwright/package.json"), "../cli.js");
|
|
34
|
+
const child = fork(cliPath, ["install", channel], {
|
|
35
|
+
stdio: "pipe",
|
|
36
|
+
});
|
|
37
|
+
const output = [];
|
|
38
|
+
child.stdout?.on("data", (data) => output.push(data.toString()));
|
|
39
|
+
child.stderr?.on("data", (data) => output.push(data.toString()));
|
|
40
|
+
await new Promise((resolve, reject) => {
|
|
41
|
+
child.on("close", (code) => {
|
|
42
|
+
if (code === 0)
|
|
43
|
+
resolve();
|
|
44
|
+
else
|
|
45
|
+
reject(new Error(`Failed to install browser: ${output.join("")}`));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
response.setIncludeTabs();
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
export default [install];
|