@playwright/mcp 0.0.36-alpha-2025-09-04 → 0.0.37-alpha-2025-09-08
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/README.md +81 -46
- package/cli.js +7 -1
- package/config.d.ts +24 -0
- package/index.js +1 -1
- package/package.json +14 -39
- package/lib/browser/browserContextFactory.js +0 -251
- package/lib/browser/browserServerBackend.js +0 -79
- package/lib/browser/codegen.js +0 -54
- package/lib/browser/config.js +0 -263
- package/lib/browser/context.js +0 -223
- package/lib/browser/response.js +0 -165
- package/lib/browser/sessionLog.js +0 -126
- package/lib/browser/tab.js +0 -251
- package/lib/browser/tools/common.js +0 -57
- package/lib/browser/tools/console.js +0 -35
- package/lib/browser/tools/dialogs.js +0 -49
- package/lib/browser/tools/evaluate.js +0 -88
- package/lib/browser/tools/files.js +0 -46
- package/lib/browser/tools/form.js +0 -92
- package/lib/browser/tools/install.js +0 -57
- package/lib/browser/tools/keyboard.js +0 -113
- package/lib/browser/tools/mouse.js +0 -101
- package/lib/browser/tools/navigate.js +0 -56
- package/lib/browser/tools/network.js +0 -43
- package/lib/browser/tools/pdf.js +0 -76
- package/lib/browser/tools/screenshot.js +0 -115
- package/lib/browser/tools/snapshot.js +0 -175
- package/lib/browser/tools/tabs.js +0 -61
- package/lib/browser/tools/tool.js +0 -37
- package/lib/browser/tools/utils.js +0 -79
- package/lib/browser/tools/verify.js +0 -172
- package/lib/browser/tools/wait.js +0 -57
- package/lib/browser/tools.js +0 -61
- package/lib/extension/cdpRelay.js +0 -395
- package/lib/extension/extensionContextFactory.js +0 -93
- package/lib/extension/protocol.js +0 -21
- package/lib/index.js +0 -75
- package/lib/log.js +0 -28
- package/lib/package.js +0 -24
- package/lib/program.js +0 -161
- package/lib/sdk/bundle.js +0 -79
- package/lib/sdk/http.js +0 -175
- package/lib/sdk/inProcessTransport.js +0 -67
- package/lib/sdk/manualPromise.js +0 -113
- package/lib/sdk/mdb.js +0 -237
- package/lib/sdk/proxyBackend.js +0 -141
- package/lib/sdk/server.js +0 -164
- package/lib/sdk/tool.js +0 -36
- package/lib/vscode/host.js +0 -199
- package/lib/vscode/main.js +0 -97
package/lib/browser/codegen.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Microsoft Corporation.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.escapeWithQuotes = escapeWithQuotes;
|
|
19
|
-
exports.quote = quote;
|
|
20
|
-
exports.formatObject = formatObject;
|
|
21
|
-
// adapted from:
|
|
22
|
-
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
|
|
23
|
-
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/server/codegen/javascript.ts
|
|
24
|
-
// NOTE: this function should not be used to escape any selectors.
|
|
25
|
-
function escapeWithQuotes(text, char = '\'') {
|
|
26
|
-
const stringified = JSON.stringify(text);
|
|
27
|
-
const escapedText = stringified.substring(1, stringified.length - 1).replace(/\\"/g, '"');
|
|
28
|
-
if (char === '\'')
|
|
29
|
-
return char + escapedText.replace(/[']/g, '\\\'') + char;
|
|
30
|
-
if (char === '"')
|
|
31
|
-
return char + escapedText.replace(/["]/g, '\\"') + char;
|
|
32
|
-
if (char === '`')
|
|
33
|
-
return char + escapedText.replace(/[`]/g, '\\`') + char;
|
|
34
|
-
throw new Error('Invalid escape char');
|
|
35
|
-
}
|
|
36
|
-
function quote(text) {
|
|
37
|
-
return escapeWithQuotes(text, '\'');
|
|
38
|
-
}
|
|
39
|
-
function formatObject(value, indent = ' ') {
|
|
40
|
-
if (typeof value === 'string')
|
|
41
|
-
return quote(value);
|
|
42
|
-
if (Array.isArray(value))
|
|
43
|
-
return `[${value.map(o => formatObject(o)).join(', ')}]`;
|
|
44
|
-
if (typeof value === 'object') {
|
|
45
|
-
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
|
|
46
|
-
if (!keys.length)
|
|
47
|
-
return '{}';
|
|
48
|
-
const tokens = [];
|
|
49
|
-
for (const key of keys)
|
|
50
|
-
tokens.push(`${key}: ${formatObject(value[key])}`);
|
|
51
|
-
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
|
|
52
|
-
}
|
|
53
|
-
return String(value);
|
|
54
|
-
}
|
package/lib/browser/config.js
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Microsoft Corporation.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
-
};
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.resolveConfig = resolveConfig;
|
|
22
|
-
exports.resolveCLIConfig = resolveCLIConfig;
|
|
23
|
-
exports.configFromCLIOptions = configFromCLIOptions;
|
|
24
|
-
exports.outputFile = outputFile;
|
|
25
|
-
exports.semicolonSeparatedList = semicolonSeparatedList;
|
|
26
|
-
exports.commaSeparatedList = commaSeparatedList;
|
|
27
|
-
const fs_1 = __importDefault(require("fs"));
|
|
28
|
-
const os_1 = __importDefault(require("os"));
|
|
29
|
-
const path_1 = __importDefault(require("path"));
|
|
30
|
-
const playwright_1 = require("playwright");
|
|
31
|
-
const defaultConfig = {
|
|
32
|
-
browser: {
|
|
33
|
-
browserName: 'chromium',
|
|
34
|
-
launchOptions: {
|
|
35
|
-
channel: 'chrome',
|
|
36
|
-
headless: os_1.default.platform() === 'linux' && !process.env.DISPLAY,
|
|
37
|
-
chromiumSandbox: true,
|
|
38
|
-
},
|
|
39
|
-
contextOptions: {
|
|
40
|
-
viewport: null,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
network: {
|
|
44
|
-
allowedOrigins: undefined,
|
|
45
|
-
blockedOrigins: undefined,
|
|
46
|
-
},
|
|
47
|
-
server: {},
|
|
48
|
-
saveTrace: false,
|
|
49
|
-
};
|
|
50
|
-
async function resolveConfig(config) {
|
|
51
|
-
return mergeConfig(defaultConfig, config);
|
|
52
|
-
}
|
|
53
|
-
async function resolveCLIConfig(cliOptions) {
|
|
54
|
-
const configInFile = await loadConfig(cliOptions.config);
|
|
55
|
-
const envOverrides = configFromEnv();
|
|
56
|
-
const cliOverrides = configFromCLIOptions(cliOptions);
|
|
57
|
-
let result = defaultConfig;
|
|
58
|
-
result = mergeConfig(result, configInFile);
|
|
59
|
-
result = mergeConfig(result, envOverrides);
|
|
60
|
-
result = mergeConfig(result, cliOverrides);
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
function configFromCLIOptions(cliOptions) {
|
|
64
|
-
let browserName;
|
|
65
|
-
let channel;
|
|
66
|
-
switch (cliOptions.browser) {
|
|
67
|
-
case 'chrome':
|
|
68
|
-
case 'chrome-beta':
|
|
69
|
-
case 'chrome-canary':
|
|
70
|
-
case 'chrome-dev':
|
|
71
|
-
case 'chromium':
|
|
72
|
-
case 'msedge':
|
|
73
|
-
case 'msedge-beta':
|
|
74
|
-
case 'msedge-canary':
|
|
75
|
-
case 'msedge-dev':
|
|
76
|
-
browserName = 'chromium';
|
|
77
|
-
channel = cliOptions.browser;
|
|
78
|
-
break;
|
|
79
|
-
case 'firefox':
|
|
80
|
-
browserName = 'firefox';
|
|
81
|
-
break;
|
|
82
|
-
case 'webkit':
|
|
83
|
-
browserName = 'webkit';
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
// Launch options
|
|
87
|
-
const launchOptions = {
|
|
88
|
-
channel,
|
|
89
|
-
executablePath: cliOptions.executablePath,
|
|
90
|
-
headless: cliOptions.headless,
|
|
91
|
-
};
|
|
92
|
-
// --no-sandbox was passed, disable the sandbox
|
|
93
|
-
if (cliOptions.sandbox === false)
|
|
94
|
-
launchOptions.chromiumSandbox = false;
|
|
95
|
-
if (cliOptions.proxyServer) {
|
|
96
|
-
launchOptions.proxy = {
|
|
97
|
-
server: cliOptions.proxyServer
|
|
98
|
-
};
|
|
99
|
-
if (cliOptions.proxyBypass)
|
|
100
|
-
launchOptions.proxy.bypass = cliOptions.proxyBypass;
|
|
101
|
-
}
|
|
102
|
-
if (cliOptions.device && cliOptions.cdpEndpoint)
|
|
103
|
-
throw new Error('Device emulation is not supported with cdpEndpoint.');
|
|
104
|
-
// Context options
|
|
105
|
-
const contextOptions = cliOptions.device ? playwright_1.devices[cliOptions.device] : {};
|
|
106
|
-
if (cliOptions.storageState)
|
|
107
|
-
contextOptions.storageState = cliOptions.storageState;
|
|
108
|
-
if (cliOptions.userAgent)
|
|
109
|
-
contextOptions.userAgent = cliOptions.userAgent;
|
|
110
|
-
if (cliOptions.viewportSize) {
|
|
111
|
-
try {
|
|
112
|
-
const [width, height] = cliOptions.viewportSize.split(',').map(n => +n);
|
|
113
|
-
if (isNaN(width) || isNaN(height))
|
|
114
|
-
throw new Error('bad values');
|
|
115
|
-
contextOptions.viewport = { width, height };
|
|
116
|
-
}
|
|
117
|
-
catch (e) {
|
|
118
|
-
throw new Error('Invalid viewport size format: use "width,height", for example --viewport-size="800,600"');
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (cliOptions.ignoreHttpsErrors)
|
|
122
|
-
contextOptions.ignoreHTTPSErrors = true;
|
|
123
|
-
if (cliOptions.blockServiceWorkers)
|
|
124
|
-
contextOptions.serviceWorkers = 'block';
|
|
125
|
-
const result = {
|
|
126
|
-
browser: {
|
|
127
|
-
browserName,
|
|
128
|
-
isolated: cliOptions.isolated,
|
|
129
|
-
userDataDir: cliOptions.userDataDir,
|
|
130
|
-
launchOptions,
|
|
131
|
-
contextOptions,
|
|
132
|
-
cdpEndpoint: cliOptions.cdpEndpoint,
|
|
133
|
-
},
|
|
134
|
-
server: {
|
|
135
|
-
port: cliOptions.port,
|
|
136
|
-
host: cliOptions.host,
|
|
137
|
-
},
|
|
138
|
-
capabilities: cliOptions.caps,
|
|
139
|
-
network: {
|
|
140
|
-
allowedOrigins: cliOptions.allowedOrigins,
|
|
141
|
-
blockedOrigins: cliOptions.blockedOrigins,
|
|
142
|
-
},
|
|
143
|
-
saveSession: cliOptions.saveSession,
|
|
144
|
-
saveTrace: cliOptions.saveTrace,
|
|
145
|
-
outputDir: cliOptions.outputDir,
|
|
146
|
-
imageResponses: cliOptions.imageResponses,
|
|
147
|
-
};
|
|
148
|
-
return result;
|
|
149
|
-
}
|
|
150
|
-
function configFromEnv() {
|
|
151
|
-
const options = {};
|
|
152
|
-
options.allowedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_ALLOWED_ORIGINS);
|
|
153
|
-
options.blockedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_BLOCKED_ORIGINS);
|
|
154
|
-
options.blockServiceWorkers = envToBoolean(process.env.PLAYWRIGHT_MCP_BLOCK_SERVICE_WORKERS);
|
|
155
|
-
options.browser = envToString(process.env.PLAYWRIGHT_MCP_BROWSER);
|
|
156
|
-
options.caps = commaSeparatedList(process.env.PLAYWRIGHT_MCP_CAPS);
|
|
157
|
-
options.cdpEndpoint = envToString(process.env.PLAYWRIGHT_MCP_CDP_ENDPOINT);
|
|
158
|
-
options.config = envToString(process.env.PLAYWRIGHT_MCP_CONFIG);
|
|
159
|
-
options.device = envToString(process.env.PLAYWRIGHT_MCP_DEVICE);
|
|
160
|
-
options.executablePath = envToString(process.env.PLAYWRIGHT_MCP_EXECUTABLE_PATH);
|
|
161
|
-
options.headless = envToBoolean(process.env.PLAYWRIGHT_MCP_HEADLESS);
|
|
162
|
-
options.host = envToString(process.env.PLAYWRIGHT_MCP_HOST);
|
|
163
|
-
options.ignoreHttpsErrors = envToBoolean(process.env.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
|
|
164
|
-
options.isolated = envToBoolean(process.env.PLAYWRIGHT_MCP_ISOLATED);
|
|
165
|
-
if (process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES === 'omit')
|
|
166
|
-
options.imageResponses = 'omit';
|
|
167
|
-
options.sandbox = envToBoolean(process.env.PLAYWRIGHT_MCP_SANDBOX);
|
|
168
|
-
options.outputDir = envToString(process.env.PLAYWRIGHT_MCP_OUTPUT_DIR);
|
|
169
|
-
options.port = envToNumber(process.env.PLAYWRIGHT_MCP_PORT);
|
|
170
|
-
options.proxyBypass = envToString(process.env.PLAYWRIGHT_MCP_PROXY_BYPASS);
|
|
171
|
-
options.proxyServer = envToString(process.env.PLAYWRIGHT_MCP_PROXY_SERVER);
|
|
172
|
-
options.saveTrace = envToBoolean(process.env.PLAYWRIGHT_MCP_SAVE_TRACE);
|
|
173
|
-
options.storageState = envToString(process.env.PLAYWRIGHT_MCP_STORAGE_STATE);
|
|
174
|
-
options.userAgent = envToString(process.env.PLAYWRIGHT_MCP_USER_AGENT);
|
|
175
|
-
options.userDataDir = envToString(process.env.PLAYWRIGHT_MCP_USER_DATA_DIR);
|
|
176
|
-
options.viewportSize = envToString(process.env.PLAYWRIGHT_MCP_VIEWPORT_SIZE);
|
|
177
|
-
return configFromCLIOptions(options);
|
|
178
|
-
}
|
|
179
|
-
async function loadConfig(configFile) {
|
|
180
|
-
if (!configFile)
|
|
181
|
-
return {};
|
|
182
|
-
try {
|
|
183
|
-
return JSON.parse(await fs_1.default.promises.readFile(configFile, 'utf8'));
|
|
184
|
-
}
|
|
185
|
-
catch (error) {
|
|
186
|
-
throw new Error(`Failed to load config file: ${configFile}, ${error}`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
async function outputFile(config, rootPath, name) {
|
|
190
|
-
var _a, _b;
|
|
191
|
-
const outputDir = (_b = (_a = config.outputDir) !== null && _a !== void 0 ? _a : (rootPath ? path_1.default.join(rootPath, '.playwright-mcp') : undefined)) !== null && _b !== void 0 ? _b : path_1.default.join(os_1.default.tmpdir(), 'playwright-mcp-output', sanitizeForFilePath(new Date().toISOString()));
|
|
192
|
-
await fs_1.default.promises.mkdir(outputDir, { recursive: true });
|
|
193
|
-
const fileName = sanitizeForFilePath(name);
|
|
194
|
-
return path_1.default.join(outputDir, fileName);
|
|
195
|
-
}
|
|
196
|
-
function pickDefined(obj) {
|
|
197
|
-
return Object.fromEntries(Object.entries(obj !== null && obj !== void 0 ? obj : {}).filter(([_, v]) => v !== undefined));
|
|
198
|
-
}
|
|
199
|
-
function mergeConfig(base, overrides) {
|
|
200
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
201
|
-
const browser = {
|
|
202
|
-
...pickDefined(base.browser),
|
|
203
|
-
...pickDefined(overrides.browser),
|
|
204
|
-
browserName: (_d = (_b = (_a = overrides.browser) === null || _a === void 0 ? void 0 : _a.browserName) !== null && _b !== void 0 ? _b : (_c = base.browser) === null || _c === void 0 ? void 0 : _c.browserName) !== null && _d !== void 0 ? _d : 'chromium',
|
|
205
|
-
isolated: (_h = (_f = (_e = overrides.browser) === null || _e === void 0 ? void 0 : _e.isolated) !== null && _f !== void 0 ? _f : (_g = base.browser) === null || _g === void 0 ? void 0 : _g.isolated) !== null && _h !== void 0 ? _h : false,
|
|
206
|
-
launchOptions: {
|
|
207
|
-
...pickDefined((_j = base.browser) === null || _j === void 0 ? void 0 : _j.launchOptions),
|
|
208
|
-
...pickDefined((_k = overrides.browser) === null || _k === void 0 ? void 0 : _k.launchOptions),
|
|
209
|
-
...{ assistantMode: true },
|
|
210
|
-
},
|
|
211
|
-
contextOptions: {
|
|
212
|
-
...pickDefined((_l = base.browser) === null || _l === void 0 ? void 0 : _l.contextOptions),
|
|
213
|
-
...pickDefined((_m = overrides.browser) === null || _m === void 0 ? void 0 : _m.contextOptions),
|
|
214
|
-
},
|
|
215
|
-
};
|
|
216
|
-
if (browser.browserName !== 'chromium' && browser.launchOptions)
|
|
217
|
-
delete browser.launchOptions.channel;
|
|
218
|
-
return {
|
|
219
|
-
...pickDefined(base),
|
|
220
|
-
...pickDefined(overrides),
|
|
221
|
-
browser,
|
|
222
|
-
network: {
|
|
223
|
-
...pickDefined(base.network),
|
|
224
|
-
...pickDefined(overrides.network),
|
|
225
|
-
},
|
|
226
|
-
server: {
|
|
227
|
-
...pickDefined(base.server),
|
|
228
|
-
...pickDefined(overrides.server),
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
function semicolonSeparatedList(value) {
|
|
233
|
-
if (!value)
|
|
234
|
-
return undefined;
|
|
235
|
-
return value.split(';').map(v => v.trim());
|
|
236
|
-
}
|
|
237
|
-
function commaSeparatedList(value) {
|
|
238
|
-
if (!value)
|
|
239
|
-
return undefined;
|
|
240
|
-
return value.split(',').map(v => v.trim());
|
|
241
|
-
}
|
|
242
|
-
function envToNumber(value) {
|
|
243
|
-
if (!value)
|
|
244
|
-
return undefined;
|
|
245
|
-
return +value;
|
|
246
|
-
}
|
|
247
|
-
function envToBoolean(value) {
|
|
248
|
-
if (value === 'true' || value === '1')
|
|
249
|
-
return true;
|
|
250
|
-
if (value === 'false' || value === '0')
|
|
251
|
-
return false;
|
|
252
|
-
return undefined;
|
|
253
|
-
}
|
|
254
|
-
function envToString(value) {
|
|
255
|
-
return value ? value.trim() : undefined;
|
|
256
|
-
}
|
|
257
|
-
function sanitizeForFilePath(s) {
|
|
258
|
-
const sanitize = (s) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
|
259
|
-
const separator = s.lastIndexOf('.');
|
|
260
|
-
if (separator === -1)
|
|
261
|
-
return sanitize(s);
|
|
262
|
-
return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
|
|
263
|
-
}
|
package/lib/browser/context.js
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Microsoft Corporation.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
-
};
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.InputRecorder = exports.Context = void 0;
|
|
22
|
-
const debug_1 = __importDefault(require("debug"));
|
|
23
|
-
const log_1 = require("../log");
|
|
24
|
-
const tab_1 = require("./tab");
|
|
25
|
-
const config_1 = require("./config");
|
|
26
|
-
const testDebug = (0, debug_1.default)('pw:mcp:test');
|
|
27
|
-
class Context {
|
|
28
|
-
constructor(options) {
|
|
29
|
-
this._tabs = [];
|
|
30
|
-
this._abortController = new AbortController();
|
|
31
|
-
this.tools = options.tools;
|
|
32
|
-
this.config = options.config;
|
|
33
|
-
this.sessionLog = options.sessionLog;
|
|
34
|
-
this.options = options;
|
|
35
|
-
this._browserContextFactory = options.browserContextFactory;
|
|
36
|
-
this._clientInfo = options.clientInfo;
|
|
37
|
-
testDebug('create context');
|
|
38
|
-
Context._allContexts.add(this);
|
|
39
|
-
}
|
|
40
|
-
static async disposeAll() {
|
|
41
|
-
await Promise.all([...Context._allContexts].map(context => context.dispose()));
|
|
42
|
-
}
|
|
43
|
-
tabs() {
|
|
44
|
-
return this._tabs;
|
|
45
|
-
}
|
|
46
|
-
currentTab() {
|
|
47
|
-
return this._currentTab;
|
|
48
|
-
}
|
|
49
|
-
currentTabOrDie() {
|
|
50
|
-
if (!this._currentTab)
|
|
51
|
-
throw new Error('No open pages available. Use the "browser_navigate" tool to navigate to a page first.');
|
|
52
|
-
return this._currentTab;
|
|
53
|
-
}
|
|
54
|
-
async newTab() {
|
|
55
|
-
const { browserContext } = await this._ensureBrowserContext();
|
|
56
|
-
const page = await browserContext.newPage();
|
|
57
|
-
this._currentTab = this._tabs.find(t => t.page === page);
|
|
58
|
-
return this._currentTab;
|
|
59
|
-
}
|
|
60
|
-
async selectTab(index) {
|
|
61
|
-
const tab = this._tabs[index];
|
|
62
|
-
if (!tab)
|
|
63
|
-
throw new Error(`Tab ${index} not found`);
|
|
64
|
-
await tab.page.bringToFront();
|
|
65
|
-
this._currentTab = tab;
|
|
66
|
-
return tab;
|
|
67
|
-
}
|
|
68
|
-
async ensureTab() {
|
|
69
|
-
const { browserContext } = await this._ensureBrowserContext();
|
|
70
|
-
if (!this._currentTab)
|
|
71
|
-
await browserContext.newPage();
|
|
72
|
-
return this._currentTab;
|
|
73
|
-
}
|
|
74
|
-
async closeTab(index) {
|
|
75
|
-
const tab = index === undefined ? this._currentTab : this._tabs[index];
|
|
76
|
-
if (!tab)
|
|
77
|
-
throw new Error(`Tab ${index} not found`);
|
|
78
|
-
const url = tab.page.url();
|
|
79
|
-
await tab.page.close();
|
|
80
|
-
return url;
|
|
81
|
-
}
|
|
82
|
-
async outputFile(name) {
|
|
83
|
-
return (0, config_1.outputFile)(this.config, this._clientInfo.rootPath, name);
|
|
84
|
-
}
|
|
85
|
-
_onPageCreated(page) {
|
|
86
|
-
const tab = new tab_1.Tab(this, page, tab => this._onPageClosed(tab));
|
|
87
|
-
this._tabs.push(tab);
|
|
88
|
-
if (!this._currentTab)
|
|
89
|
-
this._currentTab = tab;
|
|
90
|
-
}
|
|
91
|
-
_onPageClosed(tab) {
|
|
92
|
-
const index = this._tabs.indexOf(tab);
|
|
93
|
-
if (index === -1)
|
|
94
|
-
return;
|
|
95
|
-
this._tabs.splice(index, 1);
|
|
96
|
-
if (this._currentTab === tab)
|
|
97
|
-
this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)];
|
|
98
|
-
if (!this._tabs.length)
|
|
99
|
-
void this.closeBrowserContext();
|
|
100
|
-
}
|
|
101
|
-
async closeBrowserContext() {
|
|
102
|
-
if (!this._closeBrowserContextPromise)
|
|
103
|
-
this._closeBrowserContextPromise = this._closeBrowserContextImpl().catch(log_1.logUnhandledError);
|
|
104
|
-
await this._closeBrowserContextPromise;
|
|
105
|
-
this._closeBrowserContextPromise = undefined;
|
|
106
|
-
}
|
|
107
|
-
isRunningTool() {
|
|
108
|
-
return this._runningToolName !== undefined;
|
|
109
|
-
}
|
|
110
|
-
setRunningTool(name) {
|
|
111
|
-
this._runningToolName = name;
|
|
112
|
-
}
|
|
113
|
-
async _closeBrowserContextImpl() {
|
|
114
|
-
if (!this._browserContextPromise)
|
|
115
|
-
return;
|
|
116
|
-
testDebug('close context');
|
|
117
|
-
const promise = this._browserContextPromise;
|
|
118
|
-
this._browserContextPromise = undefined;
|
|
119
|
-
await promise.then(async ({ browserContext, close }) => {
|
|
120
|
-
if (this.config.saveTrace)
|
|
121
|
-
await browserContext.tracing.stop();
|
|
122
|
-
await close();
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
async dispose() {
|
|
126
|
-
this._abortController.abort('MCP context disposed');
|
|
127
|
-
await this.closeBrowserContext();
|
|
128
|
-
Context._allContexts.delete(this);
|
|
129
|
-
}
|
|
130
|
-
async _setupRequestInterception(context) {
|
|
131
|
-
var _a, _b, _c, _d;
|
|
132
|
-
if ((_b = (_a = this.config.network) === null || _a === void 0 ? void 0 : _a.allowedOrigins) === null || _b === void 0 ? void 0 : _b.length) {
|
|
133
|
-
await context.route('**', route => route.abort('blockedbyclient'));
|
|
134
|
-
for (const origin of this.config.network.allowedOrigins)
|
|
135
|
-
await context.route(`*://${origin}/**`, route => route.continue());
|
|
136
|
-
}
|
|
137
|
-
if ((_d = (_c = this.config.network) === null || _c === void 0 ? void 0 : _c.blockedOrigins) === null || _d === void 0 ? void 0 : _d.length) {
|
|
138
|
-
for (const origin of this.config.network.blockedOrigins)
|
|
139
|
-
await context.route(`*://${origin}/**`, route => route.abort('blockedbyclient'));
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
_ensureBrowserContext() {
|
|
143
|
-
if (!this._browserContextPromise) {
|
|
144
|
-
this._browserContextPromise = this._setupBrowserContext();
|
|
145
|
-
this._browserContextPromise.catch(() => {
|
|
146
|
-
this._browserContextPromise = undefined;
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
return this._browserContextPromise;
|
|
150
|
-
}
|
|
151
|
-
async _setupBrowserContext() {
|
|
152
|
-
if (this._closeBrowserContextPromise)
|
|
153
|
-
throw new Error('Another browser context is being closed.');
|
|
154
|
-
// TODO: move to the browser context factory to make it based on isolation mode.
|
|
155
|
-
const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal, this._runningToolName);
|
|
156
|
-
const { browserContext } = result;
|
|
157
|
-
await this._setupRequestInterception(browserContext);
|
|
158
|
-
if (this.sessionLog)
|
|
159
|
-
await InputRecorder.create(this, browserContext);
|
|
160
|
-
for (const page of browserContext.pages())
|
|
161
|
-
this._onPageCreated(page);
|
|
162
|
-
browserContext.on('page', page => this._onPageCreated(page));
|
|
163
|
-
if (this.config.saveTrace) {
|
|
164
|
-
await browserContext.tracing.start({
|
|
165
|
-
name: 'trace',
|
|
166
|
-
screenshots: false,
|
|
167
|
-
snapshots: true,
|
|
168
|
-
sources: false,
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
return result;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
exports.Context = Context;
|
|
175
|
-
Context._allContexts = new Set();
|
|
176
|
-
class InputRecorder {
|
|
177
|
-
constructor(context, browserContext) {
|
|
178
|
-
this._context = context;
|
|
179
|
-
this._browserContext = browserContext;
|
|
180
|
-
}
|
|
181
|
-
static async create(context, browserContext) {
|
|
182
|
-
const recorder = new InputRecorder(context, browserContext);
|
|
183
|
-
await recorder._initialize();
|
|
184
|
-
return recorder;
|
|
185
|
-
}
|
|
186
|
-
async _initialize() {
|
|
187
|
-
const sessionLog = this._context.sessionLog;
|
|
188
|
-
await this._browserContext._enableRecorder({
|
|
189
|
-
mode: 'recording',
|
|
190
|
-
recorderMode: 'api',
|
|
191
|
-
}, {
|
|
192
|
-
actionAdded: (page, data, code) => {
|
|
193
|
-
if (this._context.isRunningTool())
|
|
194
|
-
return;
|
|
195
|
-
const tab = tab_1.Tab.forPage(page);
|
|
196
|
-
if (tab)
|
|
197
|
-
sessionLog.logUserAction(data.action, tab, code, false);
|
|
198
|
-
},
|
|
199
|
-
actionUpdated: (page, data, code) => {
|
|
200
|
-
if (this._context.isRunningTool())
|
|
201
|
-
return;
|
|
202
|
-
const tab = tab_1.Tab.forPage(page);
|
|
203
|
-
if (tab)
|
|
204
|
-
sessionLog.logUserAction(data.action, tab, code, true);
|
|
205
|
-
},
|
|
206
|
-
signalAdded: (page, data) => {
|
|
207
|
-
if (this._context.isRunningTool())
|
|
208
|
-
return;
|
|
209
|
-
if (data.signal.name !== 'navigation')
|
|
210
|
-
return;
|
|
211
|
-
const tab = tab_1.Tab.forPage(page);
|
|
212
|
-
const navigateAction = {
|
|
213
|
-
name: 'navigate',
|
|
214
|
-
url: data.signal.url,
|
|
215
|
-
signals: [],
|
|
216
|
-
};
|
|
217
|
-
if (tab)
|
|
218
|
-
sessionLog.logUserAction(navigateAction, tab, `await page.goto('${data.signal.url}');`, false);
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
exports.InputRecorder = InputRecorder;
|