@ejazullah/browser-mcp 0.0.56
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 +860 -0
- package/cli.js +19 -0
- package/index.d.ts +23 -0
- package/index.js +1061 -0
- package/lib/auth.js +82 -0
- package/lib/browserContextFactory.js +205 -0
- package/lib/browserServerBackend.js +125 -0
- package/lib/config.js +266 -0
- package/lib/context.js +232 -0
- package/lib/databaseLogger.js +264 -0
- package/lib/extension/cdpRelay.js +346 -0
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/extension/main.js +26 -0
- package/lib/fileUtils.js +32 -0
- package/lib/httpServer.js +39 -0
- package/lib/index.js +39 -0
- package/lib/javascript.js +49 -0
- package/lib/log.js +21 -0
- package/lib/loop/loop.js +69 -0
- package/lib/loop/loopClaude.js +152 -0
- package/lib/loop/loopOpenAI.js +143 -0
- package/lib/loop/main.js +60 -0
- package/lib/loopTools/context.js +66 -0
- package/lib/loopTools/main.js +49 -0
- package/lib/loopTools/perform.js +32 -0
- package/lib/loopTools/snapshot.js +29 -0
- package/lib/loopTools/tool.js +18 -0
- package/lib/manualPromise.js +111 -0
- package/lib/mcp/inProcessTransport.js +72 -0
- package/lib/mcp/server.js +93 -0
- package/lib/mcp/transport.js +217 -0
- package/lib/mongoDBLogger.js +252 -0
- package/lib/package.js +20 -0
- package/lib/program.js +113 -0
- package/lib/response.js +172 -0
- package/lib/sessionLog.js +156 -0
- package/lib/tab.js +266 -0
- package/lib/tools/cdp.js +169 -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/extraction.js +217 -0
- package/lib/tools/files.js +44 -0
- package/lib/tools/forms.js +180 -0
- package/lib/tools/getext.js +99 -0
- package/lib/tools/install.js +53 -0
- package/lib/tools/interactions.js +191 -0
- package/lib/tools/keyboard.js +86 -0
- package/lib/tools/mouse.js +99 -0
- package/lib/tools/navigate.js +70 -0
- package/lib/tools/network.js +41 -0
- package/lib/tools/pdf.js +40 -0
- package/lib/tools/screenshot.js +75 -0
- package/lib/tools/selectors.js +233 -0
- package/lib/tools/snapshot.js +169 -0
- package/lib/tools/states.js +147 -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 +56 -0
- package/lib/tools.js +64 -0
- package/lib/utils.js +26 -0
- package/openapi.json +683 -0
- package/package.json +92 -0
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
// Scroll to element or position
|
|
21
|
+
const scrollTo = defineTabTool({
|
|
22
|
+
capability: 'core',
|
|
23
|
+
schema: {
|
|
24
|
+
name: 'browser_scroll_to',
|
|
25
|
+
title: 'Scroll to element or position',
|
|
26
|
+
description: 'Scroll to a specific element or coordinate position',
|
|
27
|
+
inputSchema: z.object({
|
|
28
|
+
element: z.string().optional().describe('Human-readable element description (if scrolling to element)'),
|
|
29
|
+
ref: z.string().optional().describe('Element reference (if scrolling to element)'),
|
|
30
|
+
x: z.number().optional().describe('X coordinate to scroll to (if scrolling to position)'),
|
|
31
|
+
y: z.number().optional().describe('Y coordinate to scroll to (if scrolling to position)'),
|
|
32
|
+
behavior: z.enum(['auto', 'smooth']).optional().default('auto').describe('Scroll behavior'),
|
|
33
|
+
}),
|
|
34
|
+
type: 'destructive',
|
|
35
|
+
},
|
|
36
|
+
handle: async (tab, params, response) => {
|
|
37
|
+
try {
|
|
38
|
+
if (params.element && params.ref) {
|
|
39
|
+
// Scroll to element
|
|
40
|
+
const locator = await tab.refLocator({ element: params.element, ref: params.ref });
|
|
41
|
+
await locator.scrollIntoViewIfNeeded();
|
|
42
|
+
response.addCode(`await page.${await generateLocator(locator)}.scrollIntoViewIfNeeded();`);
|
|
43
|
+
response.addResult('Scrolled to element successfully');
|
|
44
|
+
}
|
|
45
|
+
else if (params.x !== undefined && params.y !== undefined) {
|
|
46
|
+
// Scroll to coordinates
|
|
47
|
+
await tab.page.evaluate(({ x, y, behavior }) => {
|
|
48
|
+
window.scrollTo({ left: x, top: y, behavior });
|
|
49
|
+
}, { x: params.x, y: params.y, behavior: params.behavior });
|
|
50
|
+
response.addCode(`await page.evaluate(() => window.scrollTo({ left: ${params.x}, top: ${params.y}, behavior: '${params.behavior}' }));`);
|
|
51
|
+
response.addResult(`Scrolled to position (${params.x}, ${params.y})`);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
response.addError('Either provide element+ref OR x+y coordinates');
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
response.setIncludeSnapshot();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
response.addError(`Failed to scroll: ${error}`);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
// Get page scroll position
|
|
65
|
+
const getScrollPosition = defineTabTool({
|
|
66
|
+
capability: 'core',
|
|
67
|
+
schema: {
|
|
68
|
+
name: 'browser_get_scroll_position',
|
|
69
|
+
title: 'Get scroll position',
|
|
70
|
+
description: 'Get the current scroll position of the page',
|
|
71
|
+
inputSchema: z.object({}),
|
|
72
|
+
type: 'readOnly',
|
|
73
|
+
},
|
|
74
|
+
handle: async (tab, params, response) => {
|
|
75
|
+
try {
|
|
76
|
+
const position = await tab.page.evaluate(() => ({
|
|
77
|
+
x: window.scrollX,
|
|
78
|
+
y: window.scrollY,
|
|
79
|
+
}));
|
|
80
|
+
response.addCode(`const position = await page.evaluate(() => ({ x: window.scrollX, y: window.scrollY }));`);
|
|
81
|
+
response.addResult(`Current scroll position: x=${position.x}, y=${position.y}`);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
response.addError(`Failed to get scroll position: ${error}`);
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
// Focus on element
|
|
89
|
+
const focusElement = defineTabTool({
|
|
90
|
+
capability: 'core',
|
|
91
|
+
schema: {
|
|
92
|
+
name: 'browser_focus_element',
|
|
93
|
+
title: 'Focus on element',
|
|
94
|
+
description: 'Set focus on a specific element',
|
|
95
|
+
inputSchema: elementSchema,
|
|
96
|
+
type: 'destructive',
|
|
97
|
+
},
|
|
98
|
+
handle: async (tab, params, response) => {
|
|
99
|
+
try {
|
|
100
|
+
const locator = await tab.refLocator(params);
|
|
101
|
+
await locator.focus();
|
|
102
|
+
response.addCode(`await page.${await generateLocator(locator)}.focus();`);
|
|
103
|
+
response.addResult('Element focused successfully');
|
|
104
|
+
response.setIncludeSnapshot();
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
response.addError(`Failed to focus element: ${error}`);
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
// Blur (remove focus from) element
|
|
112
|
+
const blurElement = defineTabTool({
|
|
113
|
+
capability: 'core',
|
|
114
|
+
schema: {
|
|
115
|
+
name: 'browser_blur_element',
|
|
116
|
+
title: 'Remove focus from element',
|
|
117
|
+
description: 'Remove focus from a specific element',
|
|
118
|
+
inputSchema: elementSchema,
|
|
119
|
+
type: 'destructive',
|
|
120
|
+
},
|
|
121
|
+
handle: async (tab, params, response) => {
|
|
122
|
+
try {
|
|
123
|
+
const locator = await tab.refLocator(params);
|
|
124
|
+
await locator.blur();
|
|
125
|
+
response.addCode(`await page.${await generateLocator(locator)}.blur();`);
|
|
126
|
+
response.addResult('Element blurred successfully');
|
|
127
|
+
response.setIncludeSnapshot();
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
response.addError(`Failed to blur element: ${error}`);
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
// Double click
|
|
135
|
+
const doubleClick = defineTabTool({
|
|
136
|
+
capability: 'core',
|
|
137
|
+
schema: {
|
|
138
|
+
name: 'browser_double_click',
|
|
139
|
+
title: 'Double click element',
|
|
140
|
+
description: 'Perform a double click on an element',
|
|
141
|
+
inputSchema: elementSchema,
|
|
142
|
+
type: 'destructive',
|
|
143
|
+
},
|
|
144
|
+
handle: async (tab, params, response) => {
|
|
145
|
+
try {
|
|
146
|
+
const locator = await tab.refLocator(params);
|
|
147
|
+
await tab.waitForCompletion(async () => {
|
|
148
|
+
await locator.dblclick();
|
|
149
|
+
});
|
|
150
|
+
response.addCode(`await page.${await generateLocator(locator)}.dblclick();`);
|
|
151
|
+
response.addResult('Double click performed successfully');
|
|
152
|
+
response.setIncludeSnapshot();
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
response.addError(`Failed to double click: ${error}`);
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
// Right click (context menu)
|
|
160
|
+
const rightClick = defineTabTool({
|
|
161
|
+
capability: 'core',
|
|
162
|
+
schema: {
|
|
163
|
+
name: 'browser_right_click',
|
|
164
|
+
title: 'Right click element',
|
|
165
|
+
description: 'Perform a right click on an element to open context menu',
|
|
166
|
+
inputSchema: elementSchema,
|
|
167
|
+
type: 'destructive',
|
|
168
|
+
},
|
|
169
|
+
handle: async (tab, params, response) => {
|
|
170
|
+
try {
|
|
171
|
+
const locator = await tab.refLocator(params);
|
|
172
|
+
await tab.waitForCompletion(async () => {
|
|
173
|
+
await locator.click({ button: 'right' });
|
|
174
|
+
});
|
|
175
|
+
response.addCode(`await page.${await generateLocator(locator)}.click({ button: 'right' });`);
|
|
176
|
+
response.addResult('Right click performed successfully');
|
|
177
|
+
response.setIncludeSnapshot();
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
response.addError(`Failed to right click: ${error}`);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
export default [
|
|
185
|
+
scrollTo,
|
|
186
|
+
getScrollPosition,
|
|
187
|
+
focusElement,
|
|
188
|
+
blurElement,
|
|
189
|
+
doubleClick,
|
|
190
|
+
rightClick,
|
|
191
|
+
];
|
|
@@ -0,0 +1,86 @@
|
|
|
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 '../javascript.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
|
+
// Get locator with selector information for database logging
|
|
57
|
+
const { locator, selector, resolvedSelector } = await tab.refLocatorWithSelector(params);
|
|
58
|
+
// Store element interaction data for database logging
|
|
59
|
+
response.setElementInteraction({
|
|
60
|
+
elementRef: params.ref,
|
|
61
|
+
elementDescription: params.element,
|
|
62
|
+
playwrightSelector: selector,
|
|
63
|
+
resolvedSelector: resolvedSelector,
|
|
64
|
+
});
|
|
65
|
+
await tab.waitForCompletion(async () => {
|
|
66
|
+
if (params.slowly) {
|
|
67
|
+
response.setIncludeSnapshot();
|
|
68
|
+
response.addCode(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
|
|
69
|
+
await locator.pressSequentially(params.text);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
response.addCode(`await page.${await generateLocator(locator)}.fill(${javascript.quote(params.text)});`);
|
|
73
|
+
await locator.fill(params.text);
|
|
74
|
+
}
|
|
75
|
+
if (params.submit) {
|
|
76
|
+
response.setIncludeSnapshot();
|
|
77
|
+
response.addCode(`await page.${await generateLocator(locator)}.press('Enter');`);
|
|
78
|
+
await locator.press('Enter');
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
export default [
|
|
84
|
+
pressKey,
|
|
85
|
+
type,
|
|
86
|
+
];
|
|
@@ -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
|
+
];
|
|
@@ -0,0 +1,70 @@
|
|
|
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 { defineTool, defineTabTool } from './tool.js';
|
|
18
|
+
const navigate = defineTool({
|
|
19
|
+
capability: 'core',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_navigate',
|
|
22
|
+
title: 'Navigate to a URL',
|
|
23
|
+
description: 'Navigate to a URL',
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
url: z.string().describe('The URL to navigate to'),
|
|
26
|
+
}),
|
|
27
|
+
type: 'destructive',
|
|
28
|
+
},
|
|
29
|
+
handle: async (context, params, response) => {
|
|
30
|
+
const tab = await context.ensureTab();
|
|
31
|
+
await tab.navigate(params.url);
|
|
32
|
+
response.setIncludeSnapshot();
|
|
33
|
+
response.addCode(`await page.goto('${params.url}');`);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
const goBack = defineTabTool({
|
|
37
|
+
capability: 'core',
|
|
38
|
+
schema: {
|
|
39
|
+
name: 'browser_navigate_back',
|
|
40
|
+
title: 'Go back',
|
|
41
|
+
description: 'Go back to the previous page',
|
|
42
|
+
inputSchema: z.object({}),
|
|
43
|
+
type: 'readOnly',
|
|
44
|
+
},
|
|
45
|
+
handle: async (tab, params, response) => {
|
|
46
|
+
await tab.page.goBack();
|
|
47
|
+
response.setIncludeSnapshot();
|
|
48
|
+
response.addCode(`await page.goBack();`);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
const goForward = defineTabTool({
|
|
52
|
+
capability: 'core',
|
|
53
|
+
schema: {
|
|
54
|
+
name: 'browser_navigate_forward',
|
|
55
|
+
title: 'Go forward',
|
|
56
|
+
description: 'Go forward to the next page',
|
|
57
|
+
inputSchema: z.object({}),
|
|
58
|
+
type: 'readOnly',
|
|
59
|
+
},
|
|
60
|
+
handle: async (tab, params, response) => {
|
|
61
|
+
await tab.page.goForward();
|
|
62
|
+
response.setIncludeSnapshot();
|
|
63
|
+
response.addCode(`await page.goForward();`);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
export default [
|
|
67
|
+
navigate,
|
|
68
|
+
goBack,
|
|
69
|
+
goForward,
|
|
70
|
+
];
|
|
@@ -0,0 +1,41 @@
|
|
|
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 requests = defineTabTool({
|
|
19
|
+
capability: 'core',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_network_requests',
|
|
22
|
+
title: 'List network requests',
|
|
23
|
+
description: 'Returns all network requests since loading the page',
|
|
24
|
+
inputSchema: z.object({}),
|
|
25
|
+
type: 'readOnly',
|
|
26
|
+
},
|
|
27
|
+
handle: async (tab, params, response) => {
|
|
28
|
+
const requests = tab.requests();
|
|
29
|
+
[...requests.entries()].forEach(([req, res]) => response.addResult(renderRequest(req, res)));
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
function renderRequest(request, response) {
|
|
33
|
+
const result = [];
|
|
34
|
+
result.push(`[${request.method().toUpperCase()}] ${request.url()}`);
|
|
35
|
+
if (response)
|
|
36
|
+
result.push(`=> [${response.status()}] ${response.statusText()}`);
|
|
37
|
+
return result.join(' ');
|
|
38
|
+
}
|
|
39
|
+
export default [
|
|
40
|
+
requests,
|
|
41
|
+
];
|
package/lib/tools/pdf.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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 '../javascript.js';
|
|
19
|
+
const pdfSchema = z.object({
|
|
20
|
+
filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
|
|
21
|
+
});
|
|
22
|
+
const pdf = defineTabTool({
|
|
23
|
+
capability: 'pdf',
|
|
24
|
+
schema: {
|
|
25
|
+
name: 'browser_pdf_save',
|
|
26
|
+
title: 'Save as PDF',
|
|
27
|
+
description: 'Save page as PDF',
|
|
28
|
+
inputSchema: pdfSchema,
|
|
29
|
+
type: 'readOnly',
|
|
30
|
+
},
|
|
31
|
+
handle: async (tab, params, response) => {
|
|
32
|
+
const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.pdf`);
|
|
33
|
+
response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
|
|
34
|
+
response.addResult(`Saved page as ${fileName}`);
|
|
35
|
+
await tab.page.pdf({ path: fileName });
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
export default [
|
|
39
|
+
pdf,
|
|
40
|
+
];
|
|
@@ -0,0 +1,75 @@
|
|
|
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 '../javascript.js';
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
|
+
const screenshotSchema = z.object({
|
|
21
|
+
type: z.enum(['png', 'jpeg']).default('png').describe('Image format for the screenshot. Default is png.'),
|
|
22
|
+
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
|
|
23
|
+
element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
|
|
24
|
+
ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
|
|
25
|
+
fullPage: z.boolean().optional().describe('When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.'),
|
|
26
|
+
}).refine(data => {
|
|
27
|
+
return !!data.element === !!data.ref;
|
|
28
|
+
}, {
|
|
29
|
+
message: 'Both element and ref must be provided or neither.',
|
|
30
|
+
path: ['ref', 'element']
|
|
31
|
+
}).refine(data => {
|
|
32
|
+
return !(data.fullPage && (data.element || data.ref));
|
|
33
|
+
}, {
|
|
34
|
+
message: 'fullPage cannot be used with element screenshots.',
|
|
35
|
+
path: ['fullPage']
|
|
36
|
+
});
|
|
37
|
+
const screenshot = defineTabTool({
|
|
38
|
+
capability: 'core',
|
|
39
|
+
schema: {
|
|
40
|
+
name: 'browser_take_screenshot',
|
|
41
|
+
title: 'Take a screenshot',
|
|
42
|
+
description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
|
|
43
|
+
inputSchema: screenshotSchema,
|
|
44
|
+
type: 'readOnly',
|
|
45
|
+
},
|
|
46
|
+
handle: async (tab, params, response) => {
|
|
47
|
+
const fileType = params.type || 'png';
|
|
48
|
+
const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
|
|
49
|
+
const options = {
|
|
50
|
+
type: fileType,
|
|
51
|
+
quality: fileType === 'png' ? undefined : 90,
|
|
52
|
+
scale: 'css',
|
|
53
|
+
path: fileName,
|
|
54
|
+
...(params.fullPage !== undefined && { fullPage: params.fullPage })
|
|
55
|
+
};
|
|
56
|
+
const isElementScreenshot = params.element && params.ref;
|
|
57
|
+
const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
|
|
58
|
+
response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
|
|
59
|
+
// Only get snapshot when element screenshot is needed
|
|
60
|
+
const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
|
|
61
|
+
if (locator)
|
|
62
|
+
response.addCode(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
|
|
63
|
+
else
|
|
64
|
+
response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
|
|
65
|
+
const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
|
|
66
|
+
response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
|
|
67
|
+
response.addImage({
|
|
68
|
+
contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
|
|
69
|
+
data: buffer
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
export default [
|
|
74
|
+
screenshot,
|
|
75
|
+
];
|