@wordbricks/playwright-mcp 0.1.3
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 +202 -0
- package/README.md +624 -0
- package/cli-wrapper.js +47 -0
- package/cli.js +18 -0
- package/config.d.ts +119 -0
- package/index.d.ts +23 -0
- package/index.js +19 -0
- package/lib/browserContextFactory.js +289 -0
- package/lib/browserServerBackend.js +82 -0
- package/lib/config.js +246 -0
- package/lib/context.js +236 -0
- package/lib/extension/cdpRelay.js +346 -0
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/frameworkPatterns.js +35 -0
- package/lib/hooks/core.js +144 -0
- package/lib/hooks/eventConsumer.js +39 -0
- package/lib/hooks/events.js +42 -0
- package/lib/hooks/formatToolCallEvent.js +16 -0
- package/lib/hooks/frameworkStateHook.js +182 -0
- package/lib/hooks/grouping.js +72 -0
- package/lib/hooks/jsonLdDetectionHook.js +175 -0
- package/lib/hooks/networkFilters.js +74 -0
- package/lib/hooks/networkSetup.js +56 -0
- package/lib/hooks/networkTrackingHook.js +55 -0
- package/lib/hooks/pageHeightHook.js +75 -0
- package/lib/hooks/registry.js +39 -0
- package/lib/hooks/requireTabHook.js +26 -0
- package/lib/hooks/schema.js +75 -0
- package/lib/hooks/waitHook.js +33 -0
- package/lib/index.js +39 -0
- package/lib/loop/loop.js +69 -0
- package/lib/loop/loopClaude.js +152 -0
- package/lib/loop/loopOpenAI.js +141 -0
- package/lib/loop/main.js +60 -0
- package/lib/loopTools/context.js +66 -0
- package/lib/loopTools/main.js +51 -0
- package/lib/loopTools/perform.js +32 -0
- package/lib/loopTools/snapshot.js +29 -0
- package/lib/loopTools/tool.js +18 -0
- package/lib/mcp/inProcessTransport.js +72 -0
- package/lib/mcp/proxyBackend.js +115 -0
- package/lib/mcp/server.js +86 -0
- package/lib/mcp/tool.js +29 -0
- package/lib/mcp/transport.js +181 -0
- package/lib/playwrightTransformer.js +497 -0
- package/lib/program.js +111 -0
- package/lib/response.js +186 -0
- package/lib/sessionLog.js +121 -0
- package/lib/tab.js +249 -0
- package/lib/tools/common.js +55 -0
- package/lib/tools/console.js +33 -0
- package/lib/tools/dialogs.js +47 -0
- package/lib/tools/evaluate.js +53 -0
- package/lib/tools/extractFrameworkState.js +214 -0
- package/lib/tools/files.js +44 -0
- package/lib/tools/getSnapshot.js +37 -0
- package/lib/tools/getVisibleHtml.js +52 -0
- package/lib/tools/install.js +53 -0
- package/lib/tools/keyboard.js +78 -0
- package/lib/tools/mouse.js +99 -0
- package/lib/tools/navigate.js +70 -0
- package/lib/tools/network.js +123 -0
- package/lib/tools/networkDetail.js +231 -0
- package/lib/tools/networkSearch/bodySearch.js +141 -0
- package/lib/tools/networkSearch/grouping.js +28 -0
- package/lib/tools/networkSearch/helpers.js +32 -0
- package/lib/tools/networkSearch/searchHtml.js +65 -0
- package/lib/tools/networkSearch/types.js +1 -0
- package/lib/tools/networkSearch/urlSearch.js +82 -0
- package/lib/tools/networkSearch.js +168 -0
- package/lib/tools/pdf.js +40 -0
- package/lib/tools/repl.js +402 -0
- package/lib/tools/screenshot.js +79 -0
- package/lib/tools/scroll.js +126 -0
- package/lib/tools/snapshot.js +139 -0
- package/lib/tools/tabs.js +87 -0
- package/lib/tools/tool.js +33 -0
- package/lib/tools/utils.js +74 -0
- package/lib/tools/wait.js +55 -0
- package/lib/tools.js +67 -0
- package/lib/utils/codegen.js +49 -0
- package/lib/utils/extensionPath.js +6 -0
- package/lib/utils/fileUtils.js +36 -0
- package/lib/utils/graphql.js +258 -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 +12 -0
- package/lib/utils/package.js +20 -0
- package/lib/utils/result.js +2 -0
- package/lib/utils/sanitizeHtml.js +98 -0
- package/lib/utils/truncate.js +103 -0
- package/lib/utils/withTimeout.js +7 -0
- package/package.json +100 -0
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
import * as javascript from '../utils/codegen.js';
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
|
+
const evaluateSchema = z.object({
|
|
21
|
+
function: z.string().describe('() => { /* code */ } or (element) => { /* code */ } when element is provided'),
|
|
22
|
+
element: z.string().optional().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
23
|
+
ref: z.string().optional().describe('Exact target element reference from the page snapshot'),
|
|
24
|
+
});
|
|
25
|
+
const evaluate = defineTabTool({
|
|
26
|
+
capability: 'core',
|
|
27
|
+
schema: {
|
|
28
|
+
name: 'browser_evaluate',
|
|
29
|
+
title: 'Evaluate JavaScript',
|
|
30
|
+
description: 'Evaluate JavaScript expression on page or element',
|
|
31
|
+
inputSchema: evaluateSchema,
|
|
32
|
+
type: 'destructive',
|
|
33
|
+
},
|
|
34
|
+
handle: async (tab, params, response) => {
|
|
35
|
+
response.setIncludeSnapshot();
|
|
36
|
+
let locator;
|
|
37
|
+
if (params.ref && params.element) {
|
|
38
|
+
locator = await tab.refLocator({ ref: params.ref, element: params.element });
|
|
39
|
+
response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
|
|
43
|
+
}
|
|
44
|
+
await tab.waitForCompletion(async () => {
|
|
45
|
+
const receiver = locator ?? tab.page;
|
|
46
|
+
const result = await receiver._evaluateFunction(params.function);
|
|
47
|
+
response.addResult(JSON.stringify(result, null, 2) || 'undefined');
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
export default [
|
|
52
|
+
evaluate,
|
|
53
|
+
];
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { defineTabTool } from './tool.js';
|
|
3
|
+
import { FRAMEWORK_STATE_PATTERNS, MAX_DISPLAY_ITEMS } from '../frameworkPatterns.js';
|
|
4
|
+
import { truncate } from '../utils/truncate.js';
|
|
5
|
+
const extractFrameworkStateSchema = z.object({
|
|
6
|
+
framework: z.string().optional().describe('Optional: specific key to extract from (e.g., "__remixContext")'),
|
|
7
|
+
path: z.string().optional().describe('Path to extract (e.g., "state.loaderData")'),
|
|
8
|
+
});
|
|
9
|
+
const extractFrameworkState = defineTabTool({
|
|
10
|
+
capability: 'core',
|
|
11
|
+
schema: {
|
|
12
|
+
name: 'browser_extract_framework_state',
|
|
13
|
+
title: 'Extract framework state',
|
|
14
|
+
description: 'Extract framework state data from web pages (React, Redux, Remix, etc.)',
|
|
15
|
+
inputSchema: extractFrameworkStateSchema,
|
|
16
|
+
type: 'readOnly',
|
|
17
|
+
},
|
|
18
|
+
handle: async (tab, params, response) => {
|
|
19
|
+
const { framework, // Optional: specific key to extract from
|
|
20
|
+
path, // Path to extract
|
|
21
|
+
} = params;
|
|
22
|
+
const result = await tab.page.evaluate(({ targetKey, targetPath, patterns }) => {
|
|
23
|
+
const foundStates = [];
|
|
24
|
+
const errors = [];
|
|
25
|
+
// If specific key requested, only check that one
|
|
26
|
+
const keysToCheck = targetKey ? [targetKey] : patterns;
|
|
27
|
+
// Check each pattern
|
|
28
|
+
for (const key of keysToCheck) {
|
|
29
|
+
if (key in window) {
|
|
30
|
+
try {
|
|
31
|
+
let data = window[key];
|
|
32
|
+
// If data is string, try to parse as JSON
|
|
33
|
+
if (typeof data === 'string') {
|
|
34
|
+
try {
|
|
35
|
+
data = JSON.parse(data);
|
|
36
|
+
}
|
|
37
|
+
catch (parseErr) {
|
|
38
|
+
throw new Error(`Failed to parse string as JSON: ${parseErr.message}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Apply path if provided
|
|
42
|
+
if (targetPath && data) {
|
|
43
|
+
// Split path while respecting bracket-quoted segments like ['a.b']
|
|
44
|
+
const splitPath = (p) => {
|
|
45
|
+
const parts = [];
|
|
46
|
+
let cur = '';
|
|
47
|
+
let inBracket = false;
|
|
48
|
+
let inQuote = null;
|
|
49
|
+
for (let i = 0; i < p.length; i++) {
|
|
50
|
+
const ch = p[i];
|
|
51
|
+
if (inQuote) {
|
|
52
|
+
cur += ch;
|
|
53
|
+
if (ch === inQuote && p[i - 1] !== '\\')
|
|
54
|
+
inQuote = null;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (inBracket) {
|
|
58
|
+
cur += ch;
|
|
59
|
+
if (ch === '"' || ch === "'") {
|
|
60
|
+
inQuote = ch;
|
|
61
|
+
}
|
|
62
|
+
else if (ch === ']') {
|
|
63
|
+
inBracket = false;
|
|
64
|
+
parts.push(cur);
|
|
65
|
+
cur = '';
|
|
66
|
+
}
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (ch === '.') {
|
|
70
|
+
if (cur) {
|
|
71
|
+
parts.push(cur);
|
|
72
|
+
cur = '';
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (ch === '[') {
|
|
77
|
+
if (cur) {
|
|
78
|
+
parts.push(cur);
|
|
79
|
+
cur = '';
|
|
80
|
+
}
|
|
81
|
+
cur = '[';
|
|
82
|
+
inBracket = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
cur += ch;
|
|
86
|
+
}
|
|
87
|
+
if (cur)
|
|
88
|
+
parts.push(cur);
|
|
89
|
+
return parts;
|
|
90
|
+
};
|
|
91
|
+
const pathParts = splitPath(targetPath);
|
|
92
|
+
for (const part of pathParts) {
|
|
93
|
+
if (data && typeof data === 'object') {
|
|
94
|
+
// Handle array index with prop: prop[123]
|
|
95
|
+
const arrayMatch = part.match(/^(.+)\[(\d+)\]$/);
|
|
96
|
+
if (arrayMatch) {
|
|
97
|
+
const prop = arrayMatch[1];
|
|
98
|
+
const index = parseInt(arrayMatch[2], 10);
|
|
99
|
+
const propData = data[prop];
|
|
100
|
+
data = Array.isArray(propData) ? propData[index] : undefined;
|
|
101
|
+
}
|
|
102
|
+
else if (part.match(/^\[(\d+)\]$/)) {
|
|
103
|
+
// Handle standalone array index: [123]
|
|
104
|
+
const indexMatch = part.match(/^\[(\d+)\]$/);
|
|
105
|
+
if (indexMatch) {
|
|
106
|
+
const index = parseInt(indexMatch[1], 10);
|
|
107
|
+
data = data?.[index];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// Handle quoted key: ['key.with.dot'] or ["key.with.dot"]
|
|
112
|
+
const bracketMatch = part.match(/^\[['"]([^'"]+)['"]\]$/);
|
|
113
|
+
if (bracketMatch)
|
|
114
|
+
data = data[bracketMatch[1]];
|
|
115
|
+
else
|
|
116
|
+
data = data[part];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
data = undefined;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (data !== undefined) {
|
|
126
|
+
foundStates.push({
|
|
127
|
+
key: key,
|
|
128
|
+
data: data,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
errors.push(`${key}: ${e instanceof Error ? e.message : String(e)}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
count: foundStates.length,
|
|
139
|
+
states: foundStates,
|
|
140
|
+
errors: errors.length > 0 ? errors : undefined,
|
|
141
|
+
};
|
|
142
|
+
}, {
|
|
143
|
+
targetKey: framework,
|
|
144
|
+
targetPath: path,
|
|
145
|
+
patterns: FRAMEWORK_STATE_PATTERNS,
|
|
146
|
+
});
|
|
147
|
+
// Extract data from results
|
|
148
|
+
if (result.states.length === 0) {
|
|
149
|
+
if (framework) {
|
|
150
|
+
if (path) {
|
|
151
|
+
throw new Error(`Path "${path}" not found in framework state '${framework}'.`);
|
|
152
|
+
}
|
|
153
|
+
throw new Error(`Key '${framework}' not found in window. ` +
|
|
154
|
+
'Available framework states can be detected by the frameworkStateHook.');
|
|
155
|
+
}
|
|
156
|
+
throw new Error('No framework state found. ' +
|
|
157
|
+
'Available framework states can be detected by the frameworkStateHook.');
|
|
158
|
+
}
|
|
159
|
+
// Prepare final data
|
|
160
|
+
let finalData;
|
|
161
|
+
const extractedKeys = result.states.map(s => s.key);
|
|
162
|
+
if (result.states.length === 1) {
|
|
163
|
+
finalData = result.states[0].data;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
finalData = {};
|
|
167
|
+
for (const state of result.states)
|
|
168
|
+
finalData[state.key] = state.data;
|
|
169
|
+
}
|
|
170
|
+
// Limit to MAX_DISPLAY_ITEMS for display if array
|
|
171
|
+
const totalItems = Array.isArray(finalData)
|
|
172
|
+
? finalData.length
|
|
173
|
+
: typeof finalData === 'object' && finalData !== null
|
|
174
|
+
? Object.keys(finalData).length
|
|
175
|
+
: 1;
|
|
176
|
+
let displayData = finalData;
|
|
177
|
+
let isTruncated = false;
|
|
178
|
+
if (Array.isArray(finalData) && finalData.length > MAX_DISPLAY_ITEMS) {
|
|
179
|
+
displayData = finalData.slice(0, MAX_DISPLAY_ITEMS);
|
|
180
|
+
isTruncated = true;
|
|
181
|
+
}
|
|
182
|
+
else if (typeof finalData === 'object' &&
|
|
183
|
+
finalData !== null &&
|
|
184
|
+
!Array.isArray(finalData)) {
|
|
185
|
+
const keys = Object.keys(finalData);
|
|
186
|
+
if (keys.length > MAX_DISPLAY_ITEMS) {
|
|
187
|
+
displayData = {};
|
|
188
|
+
keys.slice(0, MAX_DISPLAY_ITEMS).forEach(key => {
|
|
189
|
+
displayData[key] = finalData[key];
|
|
190
|
+
});
|
|
191
|
+
isTruncated = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Truncate for readability and cap output
|
|
195
|
+
const truncatedDisplay = truncate(displayData, { maxStringLength: 1000, maxItems: MAX_DISPLAY_ITEMS });
|
|
196
|
+
const outputStr = JSON.stringify(truncatedDisplay, null, 2);
|
|
197
|
+
const summary = [
|
|
198
|
+
`Found ${result.count} framework state(s)`,
|
|
199
|
+
result.states.length > 1
|
|
200
|
+
? `Extracted from: ${extractedKeys.join(', ')}`
|
|
201
|
+
: `Using: ${result.states[0]?.key}`,
|
|
202
|
+
isTruncated
|
|
203
|
+
? `Showing first ${MAX_DISPLAY_ITEMS} of ${totalItems} results`
|
|
204
|
+
: null,
|
|
205
|
+
path ? `Extracted path: ${path}` : null,
|
|
206
|
+
result.errors ? `Errors: ${result.errors.join('; ')}` : null,
|
|
207
|
+
]
|
|
208
|
+
.filter(Boolean)
|
|
209
|
+
.join('\n');
|
|
210
|
+
response.addResult(summary + '\n\n' + outputStr);
|
|
211
|
+
response.setIncludeSnapshot();
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
export default [extractFrameworkState];
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
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.array(z.string()).describe('The absolute paths to the files to upload. Can be a single file or multiple files.'),
|
|
26
|
+
}),
|
|
27
|
+
type: 'destructive',
|
|
28
|
+
},
|
|
29
|
+
handle: async (tab, params, response) => {
|
|
30
|
+
response.setIncludeSnapshot();
|
|
31
|
+
const modalState = tab.modalStates().find(state => state.type === 'fileChooser');
|
|
32
|
+
if (!modalState)
|
|
33
|
+
throw new Error('No file chooser visible');
|
|
34
|
+
response.addCode(`await fileChooser.setFiles(${JSON.stringify(params.paths)})`);
|
|
35
|
+
tab.clearModalState(modalState);
|
|
36
|
+
await tab.waitForCompletion(async () => {
|
|
37
|
+
await modalState.fileChooser.setFiles(params.paths);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
clearsModalState: 'fileChooser',
|
|
41
|
+
});
|
|
42
|
+
export default [
|
|
43
|
+
uploadFile,
|
|
44
|
+
];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { defineTool } from './tool.js';
|
|
3
|
+
import { truncate } from '../utils/truncate.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', inputSchema: getSnapshotSchema,
|
|
12
|
+
type: 'readOnly',
|
|
13
|
+
},
|
|
14
|
+
handle: async (context, _params, response) => {
|
|
15
|
+
const tab = await context.ensureTab();
|
|
16
|
+
try {
|
|
17
|
+
const snapshot = await tab.captureSnapshot();
|
|
18
|
+
const lines = [];
|
|
19
|
+
lines.push(`### Page state`);
|
|
20
|
+
lines.push(`- Page URL: ${snapshot.url}`);
|
|
21
|
+
lines.push(`- Page Title: ${snapshot.title}`);
|
|
22
|
+
lines.push(`- Page Snapshot:`);
|
|
23
|
+
lines.push('```yaml');
|
|
24
|
+
let aria = snapshot.ariaSnapshot || '';
|
|
25
|
+
aria = String(truncate(aria, { maxStringLength: MAX_LENGTH }));
|
|
26
|
+
lines.push(aria);
|
|
27
|
+
lines.push('```');
|
|
28
|
+
response.addResult(lines.join('\n'));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
response.addResult(`Failed to get snapshot: ${String(error)}`);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
export default [
|
|
36
|
+
getSnapshot,
|
|
37
|
+
];
|
|
@@ -0,0 +1,52 @@
|
|
|
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.string().optional().describe('CSS selector for a specific element (optional)'),
|
|
6
|
+
cleanHtml: z.boolean().optional().default(true).describe('Clean HTML (true) or raw HTML (false)'),
|
|
7
|
+
removeScripts: z.boolean().optional().default(true).describe('Remove scripts (true) or keep them (false)'),
|
|
8
|
+
removeStyles: z.boolean().optional().default(true).describe('Remove styles (true) or keep them (false)'),
|
|
9
|
+
});
|
|
10
|
+
const getVisibleHtml = defineTool({
|
|
11
|
+
capability: 'core',
|
|
12
|
+
schema: {
|
|
13
|
+
name: 'browser_get_visible_html',
|
|
14
|
+
title: 'Get visible HTML',
|
|
15
|
+
description: 'Get HTML content of the page or a specific element',
|
|
16
|
+
inputSchema: getVisibleHtmlSchema,
|
|
17
|
+
type: 'readOnly',
|
|
18
|
+
},
|
|
19
|
+
handle: async (context, params, response) => {
|
|
20
|
+
const tab = await context.ensureTab();
|
|
21
|
+
const page = tab.page;
|
|
22
|
+
try {
|
|
23
|
+
const { selector, cleanHtml, removeScripts, removeStyles } = params;
|
|
24
|
+
// Get the HTML content
|
|
25
|
+
let htmlContent;
|
|
26
|
+
if (selector) {
|
|
27
|
+
// If a selector is provided, get only the HTML for that element
|
|
28
|
+
const element = await page.$(selector);
|
|
29
|
+
if (!element)
|
|
30
|
+
throw new Error(`Element with selector "${selector}" not found`);
|
|
31
|
+
htmlContent = await page.evaluate(el => el.outerHTML, element);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// Otherwise get the full page HTML
|
|
35
|
+
htmlContent = await page.content();
|
|
36
|
+
}
|
|
37
|
+
// Determine if we need to apply filters
|
|
38
|
+
const shouldRemoveScripts = removeScripts || cleanHtml;
|
|
39
|
+
const shouldRemoveStyles = removeStyles || cleanHtml;
|
|
40
|
+
// Apply filters server-side to avoid TrustedHTML/Trusted Types restrictions in page context
|
|
41
|
+
if (shouldRemoveScripts || shouldRemoveStyles)
|
|
42
|
+
htmlContent = sanitizeHtml(htmlContent, { shouldRemoveScripts, shouldRemoveStyles });
|
|
43
|
+
response.addResult(`HTML content:\n${htmlContent}`);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
response.addResult(`Failed to get visible HTML content: ${error.message}`);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
export default [
|
|
51
|
+
getVisibleHtml,
|
|
52
|
+
];
|
|
@@ -0,0 +1,53 @@
|
|
|
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 { fileURLToPath } from 'url';
|
|
19
|
+
import { z } from 'zod';
|
|
20
|
+
import { defineTool } from './tool.js';
|
|
21
|
+
const install = defineTool({
|
|
22
|
+
capability: 'core-install',
|
|
23
|
+
schema: {
|
|
24
|
+
name: 'browser_install',
|
|
25
|
+
title: 'Install the browser specified in the config',
|
|
26
|
+
description: 'Install the browser specified in the config. Call this if you get an error about the browser not being installed.',
|
|
27
|
+
inputSchema: z.object({}),
|
|
28
|
+
type: 'destructive',
|
|
29
|
+
},
|
|
30
|
+
handle: async (context, params, response) => {
|
|
31
|
+
const channel = context.config.browser?.launchOptions?.channel ?? context.config.browser?.browserName ?? 'chrome';
|
|
32
|
+
const cliUrl = import.meta.resolve('playwright/package.json');
|
|
33
|
+
const cliPath = path.join(fileURLToPath(cliUrl), '..', '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 [
|
|
52
|
+
install,
|
|
53
|
+
];
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
import { elementSchema } from './snapshot.js';
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
|
+
import * as javascript from '../utils/codegen.js';
|
|
21
|
+
const pressKey = defineTabTool({
|
|
22
|
+
capability: 'core',
|
|
23
|
+
schema: {
|
|
24
|
+
name: 'browser_press_key',
|
|
25
|
+
title: 'Press a key',
|
|
26
|
+
description: 'Press a key on the keyboard',
|
|
27
|
+
inputSchema: z.object({
|
|
28
|
+
key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
29
|
+
}),
|
|
30
|
+
type: 'destructive',
|
|
31
|
+
},
|
|
32
|
+
handle: async (tab, params, response) => {
|
|
33
|
+
response.setIncludeSnapshot();
|
|
34
|
+
response.addCode(`// Press ${params.key}`);
|
|
35
|
+
response.addCode(`await page.keyboard.press('${params.key}');`);
|
|
36
|
+
await tab.waitForCompletion(async () => {
|
|
37
|
+
await tab.page.keyboard.press(params.key);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const typeSchema = elementSchema.extend({
|
|
42
|
+
text: z.string().describe('Text to type into the element'),
|
|
43
|
+
submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
|
|
44
|
+
slowly: z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.'),
|
|
45
|
+
});
|
|
46
|
+
const type = defineTabTool({
|
|
47
|
+
capability: 'core',
|
|
48
|
+
schema: {
|
|
49
|
+
name: 'browser_type',
|
|
50
|
+
title: 'Type text',
|
|
51
|
+
description: 'Type text into editable element',
|
|
52
|
+
inputSchema: typeSchema,
|
|
53
|
+
type: 'destructive',
|
|
54
|
+
},
|
|
55
|
+
handle: async (tab, params, response) => {
|
|
56
|
+
const locator = await tab.refLocator(params);
|
|
57
|
+
await tab.waitForCompletion(async () => {
|
|
58
|
+
if (params.slowly) {
|
|
59
|
+
response.setIncludeSnapshot();
|
|
60
|
+
response.addCode(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
|
|
61
|
+
await locator.pressSequentially(params.text);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
response.addCode(`await page.${await generateLocator(locator)}.fill(${javascript.quote(params.text)});`);
|
|
65
|
+
await locator.fill(params.text);
|
|
66
|
+
}
|
|
67
|
+
if (params.submit) {
|
|
68
|
+
response.setIncludeSnapshot();
|
|
69
|
+
response.addCode(`await page.${await generateLocator(locator)}.press('Enter');`);
|
|
70
|
+
await locator.press('Enter');
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
export default [
|
|
76
|
+
pressKey,
|
|
77
|
+
type,
|
|
78
|
+
];
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
const elementSchema = z.object({
|
|
19
|
+
element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
20
|
+
});
|
|
21
|
+
const mouseMove = defineTabTool({
|
|
22
|
+
capability: 'vision',
|
|
23
|
+
schema: {
|
|
24
|
+
name: 'browser_mouse_move_xy',
|
|
25
|
+
title: 'Move mouse',
|
|
26
|
+
description: 'Move mouse to a given position',
|
|
27
|
+
inputSchema: elementSchema.extend({
|
|
28
|
+
x: z.number().describe('X coordinate'),
|
|
29
|
+
y: z.number().describe('Y coordinate'),
|
|
30
|
+
}),
|
|
31
|
+
type: 'readOnly',
|
|
32
|
+
},
|
|
33
|
+
handle: async (tab, params, response) => {
|
|
34
|
+
response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
|
|
35
|
+
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
|
|
36
|
+
await tab.waitForCompletion(async () => {
|
|
37
|
+
await tab.page.mouse.move(params.x, params.y);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const mouseClick = defineTabTool({
|
|
42
|
+
capability: 'vision',
|
|
43
|
+
schema: {
|
|
44
|
+
name: 'browser_mouse_click_xy',
|
|
45
|
+
title: 'Click',
|
|
46
|
+
description: 'Click left mouse button at a given position',
|
|
47
|
+
inputSchema: elementSchema.extend({
|
|
48
|
+
x: z.number().describe('X coordinate'),
|
|
49
|
+
y: z.number().describe('Y coordinate'),
|
|
50
|
+
}),
|
|
51
|
+
type: 'destructive',
|
|
52
|
+
},
|
|
53
|
+
handle: async (tab, params, response) => {
|
|
54
|
+
response.setIncludeSnapshot();
|
|
55
|
+
response.addCode(`// Click mouse at coordinates (${params.x}, ${params.y})`);
|
|
56
|
+
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
|
|
57
|
+
response.addCode(`await page.mouse.down();`);
|
|
58
|
+
response.addCode(`await page.mouse.up();`);
|
|
59
|
+
await tab.waitForCompletion(async () => {
|
|
60
|
+
await tab.page.mouse.move(params.x, params.y);
|
|
61
|
+
await tab.page.mouse.down();
|
|
62
|
+
await tab.page.mouse.up();
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const mouseDrag = defineTabTool({
|
|
67
|
+
capability: 'vision',
|
|
68
|
+
schema: {
|
|
69
|
+
name: 'browser_mouse_drag_xy',
|
|
70
|
+
title: 'Drag mouse',
|
|
71
|
+
description: 'Drag left mouse button to a given position',
|
|
72
|
+
inputSchema: elementSchema.extend({
|
|
73
|
+
startX: z.number().describe('Start X coordinate'),
|
|
74
|
+
startY: z.number().describe('Start Y coordinate'),
|
|
75
|
+
endX: z.number().describe('End X coordinate'),
|
|
76
|
+
endY: z.number().describe('End Y coordinate'),
|
|
77
|
+
}),
|
|
78
|
+
type: 'destructive',
|
|
79
|
+
},
|
|
80
|
+
handle: async (tab, params, response) => {
|
|
81
|
+
response.setIncludeSnapshot();
|
|
82
|
+
response.addCode(`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`);
|
|
83
|
+
response.addCode(`await page.mouse.move(${params.startX}, ${params.startY});`);
|
|
84
|
+
response.addCode(`await page.mouse.down();`);
|
|
85
|
+
response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
|
|
86
|
+
response.addCode(`await page.mouse.up();`);
|
|
87
|
+
await tab.waitForCompletion(async () => {
|
|
88
|
+
await tab.page.mouse.move(params.startX, params.startY);
|
|
89
|
+
await tab.page.mouse.down();
|
|
90
|
+
await tab.page.mouse.move(params.endX, params.endY);
|
|
91
|
+
await tab.page.mouse.up();
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
export default [
|
|
96
|
+
mouseMove,
|
|
97
|
+
mouseClick,
|
|
98
|
+
mouseDrag,
|
|
99
|
+
];
|