@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,139 @@
|
|
|
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, defineTool } from './tool.js';
|
|
18
|
+
import * as javascript from '../utils/codegen.js';
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
|
+
const snapshot = defineTool({
|
|
21
|
+
capability: 'core',
|
|
22
|
+
schema: {
|
|
23
|
+
name: 'browser_snapshot',
|
|
24
|
+
title: 'Page snapshot',
|
|
25
|
+
description: 'Capture accessibility snapshot of the current page, this is better than screenshot',
|
|
26
|
+
inputSchema: z.object({}),
|
|
27
|
+
type: 'readOnly',
|
|
28
|
+
},
|
|
29
|
+
handle: async (context, params, response) => {
|
|
30
|
+
await context.ensureTab();
|
|
31
|
+
response.setIncludeSnapshot();
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
export const elementSchema = z.object({
|
|
35
|
+
element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
36
|
+
ref: z.string().describe('Exact target element reference from the page snapshot'),
|
|
37
|
+
});
|
|
38
|
+
const clickSchema = elementSchema.extend({
|
|
39
|
+
doubleClick: z.boolean().optional().describe('Whether to perform a double click instead of a single click'),
|
|
40
|
+
button: z.enum(['left', 'right', 'middle']).optional().describe('Button to click, defaults to left'),
|
|
41
|
+
});
|
|
42
|
+
const click = defineTabTool({
|
|
43
|
+
capability: 'core',
|
|
44
|
+
schema: {
|
|
45
|
+
name: 'browser_click',
|
|
46
|
+
title: 'Click',
|
|
47
|
+
description: 'Perform click on a web page',
|
|
48
|
+
inputSchema: clickSchema,
|
|
49
|
+
type: 'destructive',
|
|
50
|
+
},
|
|
51
|
+
handle: async (tab, params, response) => {
|
|
52
|
+
response.setIncludeSnapshot();
|
|
53
|
+
const locator = await tab.refLocator(params);
|
|
54
|
+
const button = params.button;
|
|
55
|
+
const buttonAttr = button ? `{ button: '${button}' }` : '';
|
|
56
|
+
if (params.doubleClick)
|
|
57
|
+
response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
|
|
58
|
+
else
|
|
59
|
+
response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
|
|
60
|
+
await tab.waitForCompletion(async () => {
|
|
61
|
+
if (params.doubleClick)
|
|
62
|
+
await locator.dblclick({ button });
|
|
63
|
+
else
|
|
64
|
+
await locator.click({ button });
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
const drag = defineTabTool({
|
|
69
|
+
capability: 'core',
|
|
70
|
+
schema: {
|
|
71
|
+
name: 'browser_drag',
|
|
72
|
+
title: 'Drag mouse',
|
|
73
|
+
description: 'Perform drag and drop between two elements',
|
|
74
|
+
inputSchema: z.object({
|
|
75
|
+
startElement: z.string().describe('Human-readable source element description used to obtain the permission to interact with the element'),
|
|
76
|
+
startRef: z.string().describe('Exact source element reference from the page snapshot'),
|
|
77
|
+
endElement: z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),
|
|
78
|
+
endRef: z.string().describe('Exact target element reference from the page snapshot'),
|
|
79
|
+
}),
|
|
80
|
+
type: 'destructive',
|
|
81
|
+
},
|
|
82
|
+
handle: async (tab, params, response) => {
|
|
83
|
+
response.setIncludeSnapshot();
|
|
84
|
+
const [startLocator, endLocator] = await tab.refLocators([
|
|
85
|
+
{ ref: params.startRef, element: params.startElement },
|
|
86
|
+
{ ref: params.endRef, element: params.endElement },
|
|
87
|
+
]);
|
|
88
|
+
await tab.waitForCompletion(async () => {
|
|
89
|
+
await startLocator.dragTo(endLocator);
|
|
90
|
+
});
|
|
91
|
+
response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
const hover = defineTabTool({
|
|
95
|
+
capability: 'core',
|
|
96
|
+
schema: {
|
|
97
|
+
name: 'browser_hover',
|
|
98
|
+
title: 'Hover mouse',
|
|
99
|
+
description: 'Hover over element on page',
|
|
100
|
+
inputSchema: elementSchema,
|
|
101
|
+
type: 'readOnly',
|
|
102
|
+
},
|
|
103
|
+
handle: async (tab, params, response) => {
|
|
104
|
+
response.setIncludeSnapshot();
|
|
105
|
+
const locator = await tab.refLocator(params);
|
|
106
|
+
response.addCode(`await page.${await generateLocator(locator)}.hover();`);
|
|
107
|
+
await tab.waitForCompletion(async () => {
|
|
108
|
+
await locator.hover();
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const selectOptionSchema = elementSchema.extend({
|
|
113
|
+
values: z.array(z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
|
|
114
|
+
});
|
|
115
|
+
const selectOption = defineTabTool({
|
|
116
|
+
capability: 'core',
|
|
117
|
+
schema: {
|
|
118
|
+
name: 'browser_select_option',
|
|
119
|
+
title: 'Select option',
|
|
120
|
+
description: 'Select an option in a dropdown',
|
|
121
|
+
inputSchema: selectOptionSchema,
|
|
122
|
+
type: 'destructive',
|
|
123
|
+
},
|
|
124
|
+
handle: async (tab, params, response) => {
|
|
125
|
+
response.setIncludeSnapshot();
|
|
126
|
+
const locator = await tab.refLocator(params);
|
|
127
|
+
response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
|
|
128
|
+
await tab.waitForCompletion(async () => {
|
|
129
|
+
await locator.selectOption(params.values);
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
export default [
|
|
134
|
+
snapshot,
|
|
135
|
+
click,
|
|
136
|
+
drag,
|
|
137
|
+
hover,
|
|
138
|
+
selectOption,
|
|
139
|
+
];
|
|
@@ -0,0 +1,87 @@
|
|
|
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 } from './tool.js';
|
|
18
|
+
const listTabs = defineTool({
|
|
19
|
+
capability: 'core-tabs',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_tab_list',
|
|
22
|
+
title: 'List tabs',
|
|
23
|
+
description: 'List browser tabs',
|
|
24
|
+
inputSchema: z.object({}),
|
|
25
|
+
type: 'readOnly',
|
|
26
|
+
},
|
|
27
|
+
handle: async (context, params, response) => {
|
|
28
|
+
await context.ensureTab();
|
|
29
|
+
response.setIncludeTabs();
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const selectTab = defineTool({
|
|
33
|
+
capability: 'core-tabs',
|
|
34
|
+
schema: {
|
|
35
|
+
name: 'browser_tab_select',
|
|
36
|
+
title: 'Select a tab',
|
|
37
|
+
description: 'Select a tab by index',
|
|
38
|
+
inputSchema: z.object({
|
|
39
|
+
index: z.number().describe('The index of the tab to select'),
|
|
40
|
+
}),
|
|
41
|
+
type: 'readOnly',
|
|
42
|
+
},
|
|
43
|
+
handle: async (context, params, response) => {
|
|
44
|
+
await context.selectTab(params.index);
|
|
45
|
+
response.setIncludeSnapshot();
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
const newTab = defineTool({
|
|
49
|
+
capability: 'core-tabs',
|
|
50
|
+
schema: {
|
|
51
|
+
name: 'browser_tab_new',
|
|
52
|
+
title: 'Open a new tab',
|
|
53
|
+
description: 'Open a new tab',
|
|
54
|
+
inputSchema: z.object({
|
|
55
|
+
url: z.string().optional().describe('The URL to navigate to in the new tab. If not provided, the new tab will be blank.'),
|
|
56
|
+
}),
|
|
57
|
+
type: 'readOnly',
|
|
58
|
+
},
|
|
59
|
+
handle: async (context, params, response) => {
|
|
60
|
+
const tab = await context.newTab();
|
|
61
|
+
if (params.url)
|
|
62
|
+
await tab.navigate(params.url);
|
|
63
|
+
response.setIncludeSnapshot();
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const closeTab = defineTool({
|
|
67
|
+
capability: 'core-tabs',
|
|
68
|
+
schema: {
|
|
69
|
+
name: 'browser_tab_close',
|
|
70
|
+
title: 'Close a tab',
|
|
71
|
+
description: 'Close a tab',
|
|
72
|
+
inputSchema: z.object({
|
|
73
|
+
index: z.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'),
|
|
74
|
+
}),
|
|
75
|
+
type: 'destructive',
|
|
76
|
+
},
|
|
77
|
+
handle: async (context, params, response) => {
|
|
78
|
+
await context.closeTab(params.index);
|
|
79
|
+
response.setIncludeSnapshot();
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
export default [
|
|
83
|
+
listTabs,
|
|
84
|
+
newTab,
|
|
85
|
+
selectTab,
|
|
86
|
+
closeTab,
|
|
87
|
+
];
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
export function defineTool(tool) {
|
|
17
|
+
return tool;
|
|
18
|
+
}
|
|
19
|
+
export function defineTabTool(tool) {
|
|
20
|
+
return {
|
|
21
|
+
...tool,
|
|
22
|
+
handle: async (context, params, response) => {
|
|
23
|
+
const tab = context.currentTabOrDie();
|
|
24
|
+
const modalStates = tab.modalStates().map(state => state.type);
|
|
25
|
+
if (tool.clearsModalState && !modalStates.includes(tool.clearsModalState))
|
|
26
|
+
response.addError(`Error: The tool "${tool.schema.name}" can only be used when there is related modal state present.\n` + tab.modalStatesMarkdown().join('\n'));
|
|
27
|
+
else if (!tool.clearsModalState && modalStates.length)
|
|
28
|
+
response.addError(`Error: Tool "${tool.schema.name}" does not handle the modal state.\n` + tab.modalStatesMarkdown().join('\n'));
|
|
29
|
+
else
|
|
30
|
+
return tool.handle(tab, params, response);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
// @ts-ignore
|
|
17
|
+
import { asLocator } from 'playwright-core/lib/utils';
|
|
18
|
+
export async function waitForCompletion(tab, callback) {
|
|
19
|
+
const requests = new Set();
|
|
20
|
+
let frameNavigated = false;
|
|
21
|
+
let waitCallback = () => { };
|
|
22
|
+
const waitBarrier = new Promise(f => { waitCallback = f; });
|
|
23
|
+
const requestListener = (request) => requests.add(request);
|
|
24
|
+
const requestFinishedListener = (request) => {
|
|
25
|
+
requests.delete(request);
|
|
26
|
+
if (!requests.size)
|
|
27
|
+
waitCallback();
|
|
28
|
+
};
|
|
29
|
+
const frameNavigateListener = (frame) => {
|
|
30
|
+
if (frame.parentFrame())
|
|
31
|
+
return;
|
|
32
|
+
frameNavigated = true;
|
|
33
|
+
dispose();
|
|
34
|
+
clearTimeout(timeout);
|
|
35
|
+
void tab.waitForLoadState('load').then(waitCallback);
|
|
36
|
+
};
|
|
37
|
+
const onTimeout = () => {
|
|
38
|
+
dispose();
|
|
39
|
+
waitCallback();
|
|
40
|
+
};
|
|
41
|
+
tab.page.on('request', requestListener);
|
|
42
|
+
tab.page.on('requestfinished', requestFinishedListener);
|
|
43
|
+
tab.page.on('framenavigated', frameNavigateListener);
|
|
44
|
+
const timeout = setTimeout(onTimeout, 10000);
|
|
45
|
+
const dispose = () => {
|
|
46
|
+
tab.page.off('request', requestListener);
|
|
47
|
+
tab.page.off('requestfinished', requestFinishedListener);
|
|
48
|
+
tab.page.off('framenavigated', frameNavigateListener);
|
|
49
|
+
clearTimeout(timeout);
|
|
50
|
+
};
|
|
51
|
+
try {
|
|
52
|
+
const result = await callback();
|
|
53
|
+
if (!requests.size && !frameNavigated)
|
|
54
|
+
waitCallback();
|
|
55
|
+
await waitBarrier;
|
|
56
|
+
await tab.waitForTimeout(1000);
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
dispose();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export async function generateLocator(locator) {
|
|
64
|
+
try {
|
|
65
|
+
const { resolvedSelector } = await locator._resolveSelector();
|
|
66
|
+
return asLocator('javascript', resolvedSelector);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
throw new Error('Ref not found, likely because element was removed. Use browser_get_snapshot to see what elements are currently on the page.');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export async function callOnPageNoTrace(page, callback) {
|
|
73
|
+
return await page._wrapApiCall(() => callback(page), { internal: true });
|
|
74
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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 } from './tool.js';
|
|
18
|
+
const wait = defineTool({
|
|
19
|
+
capability: 'core',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_wait_for',
|
|
22
|
+
title: 'Wait for',
|
|
23
|
+
description: 'Wait for text to appear or disappear or a specified time to pass',
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
time: z.number().optional().describe('The time to wait in seconds'),
|
|
26
|
+
text: z.string().optional().describe('The text to wait for'),
|
|
27
|
+
textGone: z.string().optional().describe('The text to wait for to disappear'),
|
|
28
|
+
}),
|
|
29
|
+
type: 'readOnly',
|
|
30
|
+
},
|
|
31
|
+
handle: async (context, params, response) => {
|
|
32
|
+
if (!params.text && !params.textGone && !params.time)
|
|
33
|
+
throw new Error('Either time, text or textGone must be provided');
|
|
34
|
+
if (params.time) {
|
|
35
|
+
response.addCode(`await new Promise(f => setTimeout(f, ${params.time} * 1000));`);
|
|
36
|
+
await new Promise(f => setTimeout(f, Math.min(30000, params.time * 1000)));
|
|
37
|
+
}
|
|
38
|
+
const tab = context.currentTabOrDie();
|
|
39
|
+
const locator = params.text ? tab.page.getByText(params.text).first() : undefined;
|
|
40
|
+
const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : undefined;
|
|
41
|
+
if (goneLocator) {
|
|
42
|
+
response.addCode(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`);
|
|
43
|
+
await goneLocator.waitFor({ state: 'hidden' });
|
|
44
|
+
}
|
|
45
|
+
if (locator) {
|
|
46
|
+
response.addCode(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`);
|
|
47
|
+
await locator.waitFor({ state: 'visible' });
|
|
48
|
+
}
|
|
49
|
+
response.addResult(`Waited for ${params.text || params.textGone || params.time}`);
|
|
50
|
+
response.setIncludeSnapshot();
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
export default [
|
|
54
|
+
wait,
|
|
55
|
+
];
|
package/lib/tools.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
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 common from './tools/common.js';
|
|
17
|
+
import console from './tools/console.js';
|
|
18
|
+
// import dialogs from './tools/dialogs.js';
|
|
19
|
+
// import files from './tools/files.js';
|
|
20
|
+
// import install from './tools/install.js';
|
|
21
|
+
import keyboard from './tools/keyboard.js';
|
|
22
|
+
import navigate from './tools/navigate.js';
|
|
23
|
+
import network from './tools/network.js';
|
|
24
|
+
import networkSearch from './tools/networkSearch.js';
|
|
25
|
+
import networkDetail from './tools/networkDetail.js';
|
|
26
|
+
// import pdf from './tools/pdf.js';
|
|
27
|
+
import snapshot from './tools/snapshot.js';
|
|
28
|
+
// import tabs from './tools/tabs.js';
|
|
29
|
+
// import screenshot from './tools/screenshot.js';
|
|
30
|
+
// import wait from './tools/wait.js';
|
|
31
|
+
// import javascript from './tools/javascript.js';
|
|
32
|
+
import evaluate from './tools/evaluate.js';
|
|
33
|
+
import getVisibleHtml from './tools/getVisibleHtml.js';
|
|
34
|
+
import getSnapshot from './tools/getSnapshot.js';
|
|
35
|
+
import repl from './tools/repl.js';
|
|
36
|
+
import extractFrameworkState from './tools/extractFrameworkState.js';
|
|
37
|
+
import scroll from './tools/scroll.js';
|
|
38
|
+
import { toolNameSchema } from './hooks/schema.js';
|
|
39
|
+
export const allTools = [
|
|
40
|
+
...common,
|
|
41
|
+
...console,
|
|
42
|
+
// ...dialogs(true),
|
|
43
|
+
// ...files(true),
|
|
44
|
+
// ...install,
|
|
45
|
+
...keyboard,
|
|
46
|
+
...navigate,
|
|
47
|
+
...network,
|
|
48
|
+
...networkSearch,
|
|
49
|
+
...networkDetail,
|
|
50
|
+
// ...pdf,
|
|
51
|
+
// ...screenshot,
|
|
52
|
+
...snapshot,
|
|
53
|
+
// ...javascript,
|
|
54
|
+
...evaluate,
|
|
55
|
+
// ...tabs(true),
|
|
56
|
+
// ...testing,
|
|
57
|
+
// ...wait,
|
|
58
|
+
...getVisibleHtml,
|
|
59
|
+
...getSnapshot,
|
|
60
|
+
...repl,
|
|
61
|
+
...extractFrameworkState,
|
|
62
|
+
...scroll,
|
|
63
|
+
];
|
|
64
|
+
export function filteredTools(config) {
|
|
65
|
+
return allTools.filter(tool => (tool.capability.startsWith('core') || config.capabilities?.includes(tool.capability)) &&
|
|
66
|
+
toolNameSchema.safeParse(tool.schema.name).success);
|
|
67
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
// adapted from:
|
|
17
|
+
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
|
|
18
|
+
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/server/codegen/javascript.ts
|
|
19
|
+
// NOTE: this function should not be used to escape any selectors.
|
|
20
|
+
export function escapeWithQuotes(text, char = '\'') {
|
|
21
|
+
const stringified = JSON.stringify(text);
|
|
22
|
+
const escapedText = stringified.substring(1, stringified.length - 1).replace(/\\"/g, '"');
|
|
23
|
+
if (char === '\'')
|
|
24
|
+
return char + escapedText.replace(/[']/g, '\\\'') + char;
|
|
25
|
+
if (char === '"')
|
|
26
|
+
return char + escapedText.replace(/["]/g, '\\"') + char;
|
|
27
|
+
if (char === '`')
|
|
28
|
+
return char + escapedText.replace(/[`]/g, '\\`') + char;
|
|
29
|
+
throw new Error('Invalid escape char');
|
|
30
|
+
}
|
|
31
|
+
export function quote(text) {
|
|
32
|
+
return escapeWithQuotes(text, '\'');
|
|
33
|
+
}
|
|
34
|
+
export function formatObject(value, indent = ' ') {
|
|
35
|
+
if (typeof value === 'string')
|
|
36
|
+
return quote(value);
|
|
37
|
+
if (Array.isArray(value))
|
|
38
|
+
return `[${value.map(o => formatObject(o)).join(', ')}]`;
|
|
39
|
+
if (typeof value === 'object') {
|
|
40
|
+
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
|
|
41
|
+
if (!keys.length)
|
|
42
|
+
return '{}';
|
|
43
|
+
const tokens = [];
|
|
44
|
+
for (const key of keys)
|
|
45
|
+
tokens.push(`${key}: ${formatObject(value[key])}`);
|
|
46
|
+
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
|
|
47
|
+
}
|
|
48
|
+
return String(value);
|
|
49
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { dirname, resolve } from 'node:path';
|
|
2
|
+
import { findUpSync } from 'find-up';
|
|
3
|
+
const lockFile = findUpSync(['bun.lock', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'], { cwd: process.cwd() });
|
|
4
|
+
if (!lockFile)
|
|
5
|
+
throw new Error('Could not find project root (no lock file found)');
|
|
6
|
+
export const extensionPath = resolve(dirname(lockFile), 'src/extensions/uBlockOriginLite');
|
|
@@ -0,0 +1,36 @@
|
|
|
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 os from 'node:os';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
export function cacheDir() {
|
|
19
|
+
let cacheDirectory;
|
|
20
|
+
if (process.platform === 'linux')
|
|
21
|
+
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
|
22
|
+
else if (process.platform === 'darwin')
|
|
23
|
+
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
|
24
|
+
else if (process.platform === 'win32')
|
|
25
|
+
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
|
26
|
+
else
|
|
27
|
+
throw new Error('Unsupported platform: ' + process.platform);
|
|
28
|
+
return path.join(cacheDirectory, 'ms-playwright');
|
|
29
|
+
}
|
|
30
|
+
export function sanitizeForFilePath(s) {
|
|
31
|
+
const sanitize = (s) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
|
32
|
+
const separator = s.lastIndexOf('.');
|
|
33
|
+
if (separator === -1)
|
|
34
|
+
return sanitize(s);
|
|
35
|
+
return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
|
|
36
|
+
}
|