@wordbricks/playwright-mcp 0.1.20 → 0.1.23
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/cli-wrapper.js +15 -14
- package/cli.js +1 -1
- package/config.d.ts +11 -6
- package/index.d.ts +7 -5
- package/index.js +1 -1
- package/package.json +34 -57
- package/LICENSE +0 -202
- package/lib/browserContextFactory.js +0 -326
- package/lib/browserServerBackend.js +0 -84
- package/lib/config.js +0 -286
- package/lib/context.js +0 -309
- package/lib/extension/cdpRelay.js +0 -346
- package/lib/extension/extensionContextFactory.js +0 -56
- package/lib/frameworkPatterns.js +0 -35
- package/lib/hooks/antiBotDetectionHook.js +0 -171
- package/lib/hooks/core.js +0 -144
- package/lib/hooks/eventConsumer.js +0 -52
- package/lib/hooks/events.js +0 -42
- package/lib/hooks/formatToolCallEvent.js +0 -16
- package/lib/hooks/frameworkStateHook.js +0 -182
- package/lib/hooks/grouping.js +0 -72
- package/lib/hooks/jsonLdDetectionHook.js +0 -175
- package/lib/hooks/networkFilters.js +0 -82
- package/lib/hooks/networkSetup.js +0 -59
- package/lib/hooks/networkTrackingHook.js +0 -67
- package/lib/hooks/pageHeightHook.js +0 -75
- package/lib/hooks/registry.js +0 -42
- package/lib/hooks/requireTabHook.js +0 -26
- package/lib/hooks/schema.js +0 -89
- package/lib/hooks/waitHook.js +0 -33
- package/lib/index.js +0 -39
- package/lib/mcp/inProcessTransport.js +0 -72
- package/lib/mcp/proxyBackend.js +0 -115
- package/lib/mcp/server.js +0 -86
- package/lib/mcp/tool.js +0 -38
- package/lib/mcp/transport.js +0 -181
- package/lib/playwrightTransformer.js +0 -497
- package/lib/program.js +0 -110
- package/lib/response.js +0 -186
- package/lib/sessionLog.js +0 -121
- package/lib/tab.js +0 -249
- package/lib/tools/common.js +0 -55
- package/lib/tools/console.js +0 -33
- package/lib/tools/dialogs.js +0 -47
- package/lib/tools/evaluate.js +0 -53
- package/lib/tools/extractFrameworkState.js +0 -214
- package/lib/tools/files.js +0 -45
- package/lib/tools/form.js +0 -57
- package/lib/tools/getSnapshot.js +0 -37
- package/lib/tools/getVisibleHtml.js +0 -52
- package/lib/tools/install.js +0 -51
- package/lib/tools/keyboard.js +0 -78
- package/lib/tools/mouse.js +0 -99
- package/lib/tools/navigate.js +0 -70
- package/lib/tools/network.js +0 -123
- package/lib/tools/networkDetail.js +0 -229
- package/lib/tools/networkSearch/bodySearch.js +0 -147
- package/lib/tools/networkSearch/grouping.js +0 -28
- package/lib/tools/networkSearch/helpers.js +0 -32
- package/lib/tools/networkSearch/searchHtml.js +0 -67
- package/lib/tools/networkSearch/types.js +0 -1
- package/lib/tools/networkSearch/urlSearch.js +0 -82
- package/lib/tools/networkSearch.js +0 -268
- package/lib/tools/pdf.js +0 -40
- package/lib/tools/repl.js +0 -402
- package/lib/tools/screenshot.js +0 -79
- package/lib/tools/scroll.js +0 -126
- package/lib/tools/snapshot.js +0 -144
- package/lib/tools/tabs.js +0 -59
- package/lib/tools/tool.js +0 -33
- package/lib/tools/utils.js +0 -74
- package/lib/tools/wait.js +0 -55
- package/lib/tools.js +0 -67
- package/lib/utils/adBlockFilter.js +0 -87
- package/lib/utils/codegen.js +0 -51
- package/lib/utils/extensionPath.js +0 -10
- package/lib/utils/fileUtils.js +0 -36
- package/lib/utils/graphql.js +0 -258
- package/lib/utils/guid.js +0 -22
- package/lib/utils/httpServer.js +0 -39
- package/lib/utils/log.js +0 -21
- package/lib/utils/manualPromise.js +0 -111
- package/lib/utils/networkFormat.js +0 -12
- package/lib/utils/package.js +0 -20
- package/lib/utils/result.js +0 -2
- package/lib/utils/sanitizeHtml.js +0 -98
- package/lib/utils/truncate.js +0 -103
- package/lib/utils/withTimeout.js +0 -7
- package/src/index.ts +0 -50
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { withTimeout } from '../../utils/withTimeout.js';
|
|
2
|
-
import { searchInHtml } from './searchHtml.js';
|
|
3
|
-
import { highlightMatch } from './helpers.js';
|
|
4
|
-
const CONTEXT_LENGTH_BODY = 400;
|
|
5
|
-
const CONTEXT_LENGTH_DEFAULT = 300;
|
|
6
|
-
const MAX_SEARCH_DEPTH = 20;
|
|
7
|
-
export const searchInObject = (obj, keyword, path, matches, source, depth = 0) => {
|
|
8
|
-
if (depth > MAX_SEARCH_DEPTH || !obj || typeof obj !== 'object')
|
|
9
|
-
return;
|
|
10
|
-
for (const key in obj) {
|
|
11
|
-
const newPath = path ? `${path}.${key}` : key;
|
|
12
|
-
const value = obj[key];
|
|
13
|
-
if (key.toLowerCase().includes(keyword)) {
|
|
14
|
-
const highlightedKey = highlightMatch(key, keyword, CONTEXT_LENGTH_DEFAULT);
|
|
15
|
-
const valueContext = typeof value === 'object' && value !== null
|
|
16
|
-
? truncateJsonPreview(value)
|
|
17
|
-
: truncateStringPreview(String(value));
|
|
18
|
-
matches.push({
|
|
19
|
-
path: newPath,
|
|
20
|
-
value: typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value),
|
|
21
|
-
context: `${highlightedKey}: ${valueContext}`,
|
|
22
|
-
source,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
if (typeof value === 'string' && value.toLowerCase().includes(keyword)) {
|
|
26
|
-
matches.push({
|
|
27
|
-
path: newPath,
|
|
28
|
-
value: value,
|
|
29
|
-
context: `${key}: ${highlightMatch(value, keyword, CONTEXT_LENGTH_DEFAULT)}`,
|
|
30
|
-
source,
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
else if (typeof value === 'number' && String(value).includes(keyword)) {
|
|
34
|
-
const valueStr = String(value);
|
|
35
|
-
matches.push({
|
|
36
|
-
path: newPath,
|
|
37
|
-
value: valueStr,
|
|
38
|
-
context: `${key}: ${highlightMatch(valueStr, keyword, CONTEXT_LENGTH_DEFAULT)}`,
|
|
39
|
-
source,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
if (typeof value === 'object' && value !== null)
|
|
43
|
-
searchInObject(value, keyword, newPath, matches, source, depth + 1);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const truncateStringPreview = (text) => text.length > CONTEXT_LENGTH_DEFAULT ? text.slice(0, CONTEXT_LENGTH_DEFAULT) + '...' : text;
|
|
47
|
-
const truncateJsonPreview = (obj) => {
|
|
48
|
-
try {
|
|
49
|
-
return JSON.stringify(obj).slice(0, CONTEXT_LENGTH_BODY) + '...';
|
|
50
|
-
}
|
|
51
|
-
catch {
|
|
52
|
-
return '[Object]';
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
export const searchInRequestBody = (request, keyword, matches) => {
|
|
56
|
-
const body = request.postData();
|
|
57
|
-
if (!body || !body.toLowerCase().includes(keyword))
|
|
58
|
-
return;
|
|
59
|
-
const requestContentType = request.headers()['content-type'] || '';
|
|
60
|
-
let handled = false;
|
|
61
|
-
if (requestContentType.includes('application/json') || body.trim().startsWith('{') || body.trim().startsWith('[')) {
|
|
62
|
-
try {
|
|
63
|
-
const parsed = JSON.parse(body);
|
|
64
|
-
searchInObject(parsed, keyword, 'request.body', matches, 'requestBody');
|
|
65
|
-
handled = true;
|
|
66
|
-
}
|
|
67
|
-
catch { }
|
|
68
|
-
}
|
|
69
|
-
if (!handled && (requestContentType.includes('text/html') || body.trim().startsWith('<'))) {
|
|
70
|
-
searchInHtml(body, keyword, 'request.body', matches, 'requestBody');
|
|
71
|
-
handled = true;
|
|
72
|
-
}
|
|
73
|
-
if (!handled && requestContentType.includes('application/x-www-form-urlencoded')) {
|
|
74
|
-
try {
|
|
75
|
-
const sp = new URLSearchParams(body);
|
|
76
|
-
for (const [k, v] of sp.entries()) {
|
|
77
|
-
const vStr = v || '';
|
|
78
|
-
if (!vStr.toLowerCase().includes(keyword))
|
|
79
|
-
continue;
|
|
80
|
-
const trimmed = vStr.trim();
|
|
81
|
-
let parsedOk = false;
|
|
82
|
-
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
83
|
-
try {
|
|
84
|
-
const parsed = JSON.parse(trimmed);
|
|
85
|
-
searchInObject(parsed, keyword, `request.body.form.${k}`, matches, 'requestBody');
|
|
86
|
-
parsedOk = true;
|
|
87
|
-
}
|
|
88
|
-
catch { }
|
|
89
|
-
}
|
|
90
|
-
if (!parsedOk) {
|
|
91
|
-
matches.push({
|
|
92
|
-
path: `request.body.form.${k}`,
|
|
93
|
-
value: vStr,
|
|
94
|
-
context: `${k}=${highlightMatch(vStr, keyword, CONTEXT_LENGTH_DEFAULT)}`,
|
|
95
|
-
source: 'requestBody',
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
handled = true;
|
|
100
|
-
}
|
|
101
|
-
catch { }
|
|
102
|
-
}
|
|
103
|
-
if (!handled) {
|
|
104
|
-
matches.push({
|
|
105
|
-
path: 'request.body',
|
|
106
|
-
value: body,
|
|
107
|
-
context: highlightMatch(body, keyword, CONTEXT_LENGTH_BODY),
|
|
108
|
-
source: 'requestBody',
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
export const searchInResponseBody = async (response, keyword, matches) => {
|
|
113
|
-
const contentTypeHeader = response.headers()['content-type'] || '';
|
|
114
|
-
if (!(contentTypeHeader.startsWith('text/') || contentTypeHeader.includes('application/json') || contentTypeHeader.includes('application/xml')))
|
|
115
|
-
return;
|
|
116
|
-
try {
|
|
117
|
-
const responseBody = await withTimeout(response.text(), 2000);
|
|
118
|
-
if (!responseBody || !responseBody.toLowerCase().includes(keyword))
|
|
119
|
-
return;
|
|
120
|
-
let handled = false;
|
|
121
|
-
const trimmed = responseBody.trimStart();
|
|
122
|
-
const xssiPrefix = ")]}'";
|
|
123
|
-
const hasXssi = trimmed.startsWith(xssiPrefix);
|
|
124
|
-
const jsonCandidate = hasXssi ? trimmed.slice(xssiPrefix.length) : trimmed;
|
|
125
|
-
if (contentTypeHeader.includes('application/json') || jsonCandidate.startsWith('{') || jsonCandidate.startsWith('[')) {
|
|
126
|
-
try {
|
|
127
|
-
const parsed = JSON.parse(jsonCandidate);
|
|
128
|
-
searchInObject(parsed, keyword, 'response.body', matches, 'responseBody');
|
|
129
|
-
handled = true;
|
|
130
|
-
}
|
|
131
|
-
catch { }
|
|
132
|
-
}
|
|
133
|
-
if (!handled && (contentTypeHeader.includes('text/html') || responseBody.trim().startsWith('<'))) {
|
|
134
|
-
searchInHtml(responseBody, keyword, 'response.body', matches, 'responseBody');
|
|
135
|
-
handled = true;
|
|
136
|
-
}
|
|
137
|
-
if (!handled) {
|
|
138
|
-
matches.push({
|
|
139
|
-
path: 'response.body',
|
|
140
|
-
value: responseBody,
|
|
141
|
-
context: highlightMatch(responseBody, keyword, CONTEXT_LENGTH_BODY),
|
|
142
|
-
source: 'responseBody',
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
catch { }
|
|
147
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export const normalizePath = (path) => {
|
|
2
|
-
if (path.includes(' > '))
|
|
3
|
-
return path.replace(/:nth-child\(\d+\)/g, ':nth-child(*)');
|
|
4
|
-
return path.replace(/\.(?:\d+)(?=\.|$)/g, '.*');
|
|
5
|
-
};
|
|
6
|
-
export const getDepth = (path) => {
|
|
7
|
-
const separators = path.match(/\.|>/g) || [];
|
|
8
|
-
return separators.length;
|
|
9
|
-
};
|
|
10
|
-
export const mergeGroupedMatches = (a, b) => {
|
|
11
|
-
const map = new Map();
|
|
12
|
-
for (const g of a)
|
|
13
|
-
map.set(g.normalized, { normalized: g.normalized, count: g.count, examples: g.examples.slice(0, 3) });
|
|
14
|
-
for (const g of b) {
|
|
15
|
-
const existing = map.get(g.normalized);
|
|
16
|
-
if (existing) {
|
|
17
|
-
existing.count += g.count;
|
|
18
|
-
for (const e of g.examples) {
|
|
19
|
-
if (existing.examples.length < 3 && !existing.examples.some(x => x.context === e.context))
|
|
20
|
-
existing.examples.push(e);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
map.set(g.normalized, { normalized: g.normalized, count: g.count, examples: g.examples.slice(0, 3) });
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return Array.from(map.values());
|
|
28
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { truncateStringTo } from '../../utils/truncate.js';
|
|
2
|
-
export const parseKeywordParams = (keyword) => {
|
|
3
|
-
const out = [];
|
|
4
|
-
try {
|
|
5
|
-
const sp = new URLSearchParams(keyword);
|
|
6
|
-
sp.forEach((value, key) => {
|
|
7
|
-
if (key)
|
|
8
|
-
out.push({ name: key, value });
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
catch { }
|
|
12
|
-
return out;
|
|
13
|
-
};
|
|
14
|
-
export const highlightMatch = (text, keyword, maxLength = 50) => {
|
|
15
|
-
const lowerText = text.toLowerCase();
|
|
16
|
-
const index = lowerText.indexOf(keyword);
|
|
17
|
-
if (index === -1)
|
|
18
|
-
return truncateStringTo(text, maxLength).text;
|
|
19
|
-
const contextBefore = Math.floor((maxLength - keyword.length) / 2);
|
|
20
|
-
const contextAfter = maxLength - keyword.length - contextBefore;
|
|
21
|
-
const start = Math.max(0, index - contextBefore);
|
|
22
|
-
const end = Math.min(text.length, index + keyword.length + contextAfter);
|
|
23
|
-
let result = '';
|
|
24
|
-
if (start > 0)
|
|
25
|
-
result += '...';
|
|
26
|
-
result += text.substring(start, index);
|
|
27
|
-
result += text.substring(index, index + keyword.length);
|
|
28
|
-
result += text.substring(index + keyword.length, end);
|
|
29
|
-
if (end < text.length)
|
|
30
|
-
result += '...';
|
|
31
|
-
return result;
|
|
32
|
-
};
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import * as cheerio from 'cheerio';
|
|
2
|
-
import { highlightMatch } from './helpers.js';
|
|
3
|
-
export const getElementPath = ($el) => {
|
|
4
|
-
const path = [];
|
|
5
|
-
let current = $el;
|
|
6
|
-
while (current.length && current[0].type === 'tag' && current[0].name !== 'html') {
|
|
7
|
-
let selector = current[0].name.toLowerCase();
|
|
8
|
-
const id = current.attr('id');
|
|
9
|
-
if (id) {
|
|
10
|
-
selector += `#${id}`;
|
|
11
|
-
path.unshift(selector);
|
|
12
|
-
break;
|
|
13
|
-
}
|
|
14
|
-
const classes = current.attr('class');
|
|
15
|
-
if (classes)
|
|
16
|
-
selector += `.${classes.trim().split(/\s+/).join('.')}`;
|
|
17
|
-
const tagName = current[0].type === 'tag' ? current[0].name : '';
|
|
18
|
-
const siblings = current.parent().children().filter((_, el) => el.type === 'tag' && el.name === tagName);
|
|
19
|
-
if (siblings.length > 1) {
|
|
20
|
-
const index = siblings.index(current) + 1;
|
|
21
|
-
selector += `:nth-child(${index})`;
|
|
22
|
-
}
|
|
23
|
-
path.unshift(selector);
|
|
24
|
-
current = current.parent();
|
|
25
|
-
}
|
|
26
|
-
return path.join(' > ');
|
|
27
|
-
};
|
|
28
|
-
export const searchInHtml = (html, keyword, basePath, matches, source) => {
|
|
29
|
-
const $ = cheerio.load(html);
|
|
30
|
-
$('*').contents().each((_, node) => {
|
|
31
|
-
if (node.type === 'text') {
|
|
32
|
-
const text = (node.data || '').trim();
|
|
33
|
-
if (text && text.toLowerCase().includes(keyword)) {
|
|
34
|
-
const parent = $(node).parent();
|
|
35
|
-
if (parent.length) {
|
|
36
|
-
const elPath = getElementPath(parent);
|
|
37
|
-
const path = elPath ? `${basePath} > ${elPath}` : basePath;
|
|
38
|
-
matches.push({
|
|
39
|
-
path,
|
|
40
|
-
value: text,
|
|
41
|
-
context: highlightMatch(text, keyword, 300),
|
|
42
|
-
source,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
$('*').each((_, el) => {
|
|
49
|
-
if (el.type === 'tag') {
|
|
50
|
-
const attrs = el.attribs;
|
|
51
|
-
if (attrs) {
|
|
52
|
-
Object.entries(attrs).forEach(([key, val]) => {
|
|
53
|
-
if (typeof val === 'string' && val.toLowerCase().includes(keyword)) {
|
|
54
|
-
const elPath = getElementPath($(el));
|
|
55
|
-
const path = elPath ? `${basePath} > ${elPath}@${key}` : `${basePath}@${key}`;
|
|
56
|
-
matches.push({
|
|
57
|
-
path,
|
|
58
|
-
value: val,
|
|
59
|
-
context: highlightMatch(val, keyword, 300),
|
|
60
|
-
source,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { highlightMatch } from './helpers.js';
|
|
2
|
-
const CONTEXT_LENGTH_DEFAULT = 300;
|
|
3
|
-
export const searchInUrls = (request, response, keyword, keywordParams, matches) => {
|
|
4
|
-
try {
|
|
5
|
-
const reqUrl = request.url();
|
|
6
|
-
const reqUrlLower = reqUrl.toLowerCase();
|
|
7
|
-
const keywordPathOnly = keyword.split('?')[0];
|
|
8
|
-
if (reqUrlLower.includes(keyword)) {
|
|
9
|
-
matches.push({ path: 'request.url', value: reqUrl, context: `url: ${highlightMatch(reqUrl, keyword, CONTEXT_LENGTH_DEFAULT)}`, source: 'requestUrl' });
|
|
10
|
-
}
|
|
11
|
-
else if (keywordPathOnly && reqUrlLower.includes(keywordPathOnly)) {
|
|
12
|
-
matches.push({ path: 'request.url', value: reqUrl, context: `url: ${highlightMatch(reqUrl, keywordPathOnly, CONTEXT_LENGTH_DEFAULT)}`, source: 'requestUrl' });
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
try {
|
|
16
|
-
const u = new URL(reqUrl);
|
|
17
|
-
const pathnameLower = u.pathname.toLowerCase();
|
|
18
|
-
if (pathnameLower.includes(keyword) || (keywordPathOnly && pathnameLower.includes(keywordPathOnly)))
|
|
19
|
-
matches.push({ path: 'request.url', value: reqUrl, context: `url: ${highlightMatch(reqUrl, keywordPathOnly || keyword, CONTEXT_LENGTH_DEFAULT)}`, source: 'requestUrl' });
|
|
20
|
-
}
|
|
21
|
-
catch { }
|
|
22
|
-
}
|
|
23
|
-
if (keywordParams.length) {
|
|
24
|
-
try {
|
|
25
|
-
const u = new URL(reqUrl);
|
|
26
|
-
for (const { name, value } of keywordParams) {
|
|
27
|
-
if (!name)
|
|
28
|
-
continue;
|
|
29
|
-
const pageVal = u.searchParams.get(name);
|
|
30
|
-
if (!pageVal)
|
|
31
|
-
continue;
|
|
32
|
-
const valLower = (value || '').toLowerCase();
|
|
33
|
-
if (!valLower)
|
|
34
|
-
continue;
|
|
35
|
-
const pageValLower = pageVal.toLowerCase();
|
|
36
|
-
if (pageValLower.includes(valLower))
|
|
37
|
-
matches.push({ path: `request.url.query.${name}`, value: pageVal, context: `${name}=${highlightMatch(pageVal, value, CONTEXT_LENGTH_DEFAULT)}`, source: 'requestUrl' });
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
catch { }
|
|
41
|
-
}
|
|
42
|
-
if (response) {
|
|
43
|
-
const resUrl = response.url();
|
|
44
|
-
const resUrlLower = resUrl.toLowerCase();
|
|
45
|
-
if (resUrlLower.includes(keyword)) {
|
|
46
|
-
matches.push({ path: 'response.url', value: resUrl, context: `url: ${highlightMatch(resUrl, keyword, CONTEXT_LENGTH_DEFAULT)}`, source: 'responseUrl' });
|
|
47
|
-
}
|
|
48
|
-
else if (keywordPathOnly && resUrlLower.includes(keywordPathOnly)) {
|
|
49
|
-
matches.push({ path: 'response.url', value: resUrl, context: `url: ${highlightMatch(resUrl, keywordPathOnly, CONTEXT_LENGTH_DEFAULT)}`, source: 'responseUrl' });
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
try {
|
|
53
|
-
const u2 = new URL(resUrl);
|
|
54
|
-
const pathnameLower2 = u2.pathname.toLowerCase();
|
|
55
|
-
if (pathnameLower2.includes(keyword) || (keywordPathOnly && pathnameLower2.includes(keywordPathOnly)))
|
|
56
|
-
matches.push({ path: 'response.url', value: resUrl, context: `url: ${highlightMatch(resUrl, keywordPathOnly || keyword, CONTEXT_LENGTH_DEFAULT)}`, source: 'responseUrl' });
|
|
57
|
-
}
|
|
58
|
-
catch { }
|
|
59
|
-
}
|
|
60
|
-
if (keywordParams.length) {
|
|
61
|
-
try {
|
|
62
|
-
const u2 = new URL(resUrl);
|
|
63
|
-
for (const { name, value } of keywordParams) {
|
|
64
|
-
if (!name)
|
|
65
|
-
continue;
|
|
66
|
-
const pageVal = u2.searchParams.get(name);
|
|
67
|
-
if (!pageVal)
|
|
68
|
-
continue;
|
|
69
|
-
const valLower = (value || '').toLowerCase();
|
|
70
|
-
if (!valLower)
|
|
71
|
-
continue;
|
|
72
|
-
const pageValLower = pageVal.toLowerCase();
|
|
73
|
-
if (pageValLower.includes(valLower))
|
|
74
|
-
matches.push({ path: `response.url.query.${name}`, value: pageVal, context: `${name}=${highlightMatch(pageVal, value, CONTEXT_LENGTH_DEFAULT)}`, source: 'responseUrl' });
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
catch { }
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
catch { }
|
|
82
|
-
};
|
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { defineTool } from './tool.js';
|
|
3
|
-
import { getNetworkEventEntry } from '../hooks/networkSetup.js';
|
|
4
|
-
import { listNetworkEvents } from '../hooks/networkTrackingHook.js';
|
|
5
|
-
import { normalizeUrlForGrouping } from '../hooks/networkFilters.js';
|
|
6
|
-
import { formatNetworkSummaryLine } from '../utils/networkFormat.js';
|
|
7
|
-
import { toJsonPathNormalized, truncateStringTo } from '../utils/truncate.js';
|
|
8
|
-
import { searchInUrls } from './networkSearch/urlSearch.js';
|
|
9
|
-
import { searchInRequestBody, searchInResponseBody } from './networkSearch/bodySearch.js';
|
|
10
|
-
import { normalizePath, getDepth, mergeGroupedMatches } from './networkSearch/grouping.js';
|
|
11
|
-
import { parseKeywordParams, highlightMatch } from './networkSearch/helpers.js';
|
|
12
|
-
const MAX_SEARCH_RESULTS = 10;
|
|
13
|
-
const MAX_GROUPS_TO_SHOW = 3;
|
|
14
|
-
const networkSearchSchema = z.object({
|
|
15
|
-
keyword: z.string().describe('Keyword or phrase; avoid generic words—specific terms reduce noise and improve precision.'),
|
|
16
|
-
});
|
|
17
|
-
const createSourceCounts = () => ({
|
|
18
|
-
requestUrl: 0,
|
|
19
|
-
requestBody: 0,
|
|
20
|
-
responseUrl: 0,
|
|
21
|
-
responseBody: 0,
|
|
22
|
-
responseHeaders: 0,
|
|
23
|
-
});
|
|
24
|
-
const accumulateSourceCounts = (records) => {
|
|
25
|
-
const counts = createSourceCounts();
|
|
26
|
-
for (const record of records)
|
|
27
|
-
counts[record.source] += 1;
|
|
28
|
-
return counts;
|
|
29
|
-
};
|
|
30
|
-
const cloneSourceCounts = (counts) => ({
|
|
31
|
-
requestUrl: counts.requestUrl,
|
|
32
|
-
requestBody: counts.requestBody,
|
|
33
|
-
responseUrl: counts.responseUrl,
|
|
34
|
-
responseBody: counts.responseBody,
|
|
35
|
-
responseHeaders: counts.responseHeaders,
|
|
36
|
-
});
|
|
37
|
-
const computeEventScore = (match) => {
|
|
38
|
-
const ageMs = Math.max(0, Date.now() - match.timestamp);
|
|
39
|
-
const recencyWindowMs = 10 * 60 * 1000;
|
|
40
|
-
const recencyBoost = 1 - Math.min(ageMs / recencyWindowMs, 1);
|
|
41
|
-
const base = match.totalMatches;
|
|
42
|
-
const responseBonus = match.sourceCounts.responseBody * 2;
|
|
43
|
-
const requestBonus = match.sourceCounts.requestBody;
|
|
44
|
-
const urlBonus = (match.sourceCounts.requestUrl + match.sourceCounts.responseUrl) * 0.25;
|
|
45
|
-
const headerBonus = match.sourceCounts.responseHeaders;
|
|
46
|
-
return base + responseBonus + requestBonus + urlBonus + headerBonus + recencyBoost;
|
|
47
|
-
};
|
|
48
|
-
const formatGroupPath = (group) => {
|
|
49
|
-
const normalized = group.normalized;
|
|
50
|
-
const jsonPath = toJsonPathNormalized(normalized);
|
|
51
|
-
if (jsonPath)
|
|
52
|
-
return `json: ${jsonPath.jsonPath}`;
|
|
53
|
-
if (!normalized.includes(' > '))
|
|
54
|
-
return normalized;
|
|
55
|
-
const withoutWildcard = normalized.replace(/:nth-child\(\*\)/g, '');
|
|
56
|
-
const trimmedPrefix = withoutWildcard.startsWith('response.body')
|
|
57
|
-
? withoutWildcard.slice('response.body'.length).trim()
|
|
58
|
-
: withoutWildcard.startsWith('request.body')
|
|
59
|
-
? withoutWildcard.slice('request.body'.length).trim()
|
|
60
|
-
: withoutWildcard;
|
|
61
|
-
const noLeadingArrow = trimmedPrefix.startsWith('>')
|
|
62
|
-
? trimmedPrefix.slice(1).trim()
|
|
63
|
-
: trimmedPrefix;
|
|
64
|
-
return `css: ${noLeadingArrow}`;
|
|
65
|
-
};
|
|
66
|
-
const extractExamples = (group) => {
|
|
67
|
-
const items = [];
|
|
68
|
-
for (const example of group.examples) {
|
|
69
|
-
const raw = example.value ?? example.context;
|
|
70
|
-
if (!raw)
|
|
71
|
-
continue;
|
|
72
|
-
const trimmed = raw.trim();
|
|
73
|
-
const base = trimmed.length > 0 ? trimmed : raw;
|
|
74
|
-
const { text } = truncateStringTo(base, 160);
|
|
75
|
-
if (!items.includes(text))
|
|
76
|
-
items.push(text);
|
|
77
|
-
}
|
|
78
|
-
return items;
|
|
79
|
-
};
|
|
80
|
-
const searchInResponseSetCookies = async (response, keyword, matches) => {
|
|
81
|
-
const cookies = await response.headerValues('set-cookie').catch(() => []);
|
|
82
|
-
if (!cookies.length)
|
|
83
|
-
return;
|
|
84
|
-
for (const [index, cookie] of cookies.entries()) {
|
|
85
|
-
if (!cookie.toLowerCase().includes(keyword))
|
|
86
|
-
continue;
|
|
87
|
-
matches.push({
|
|
88
|
-
path: `response.headers.set-cookie[${index}]`,
|
|
89
|
-
value: cookie,
|
|
90
|
-
context: highlightMatch(cookie, keyword, 120),
|
|
91
|
-
source: 'responseHeaders',
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
const networkSearch = defineTool({
|
|
96
|
-
capability: 'core',
|
|
97
|
-
schema: {
|
|
98
|
-
name: 'browser_network_search',
|
|
99
|
-
title: 'Search network requests',
|
|
100
|
-
description: 'Search for keywords in network request/response bodies and URLs',
|
|
101
|
-
inputSchema: networkSearchSchema,
|
|
102
|
-
type: 'readOnly',
|
|
103
|
-
},
|
|
104
|
-
handle: async (context, params, response) => {
|
|
105
|
-
await context.ensureTab();
|
|
106
|
-
try {
|
|
107
|
-
const keyword = params.keyword?.toLowerCase();
|
|
108
|
-
if (!keyword) {
|
|
109
|
-
response.addResult('keyword parameter is required');
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
const events = listNetworkEvents(context);
|
|
113
|
-
const keywordParams = parseKeywordParams(params.keyword);
|
|
114
|
-
const searchMatches = [];
|
|
115
|
-
for (const ev of events) {
|
|
116
|
-
const entry = getNetworkEventEntry(context, ev.id);
|
|
117
|
-
if (!entry)
|
|
118
|
-
continue;
|
|
119
|
-
const request = entry.request;
|
|
120
|
-
const resp = entry.response ?? null;
|
|
121
|
-
const matches = [];
|
|
122
|
-
searchInUrls(request, resp, keyword, keywordParams, matches);
|
|
123
|
-
searchInRequestBody(request, keyword, matches);
|
|
124
|
-
if (resp) {
|
|
125
|
-
await searchInResponseBody(resp, keyword, matches);
|
|
126
|
-
await searchInResponseSetCookies(resp, keyword, matches);
|
|
127
|
-
}
|
|
128
|
-
if (matches.length > 0) {
|
|
129
|
-
const groupMap = new Map();
|
|
130
|
-
for (const m of matches) {
|
|
131
|
-
const normalized = normalizePath(m.path);
|
|
132
|
-
let group = groupMap.get(normalized);
|
|
133
|
-
if (!group) {
|
|
134
|
-
group = { normalized, count: 0, examples: [] };
|
|
135
|
-
groupMap.set(normalized, group);
|
|
136
|
-
}
|
|
137
|
-
group.count++;
|
|
138
|
-
if (group.examples.length < 3) {
|
|
139
|
-
const example = { context: m.context, value: m.value, originalPath: m.path !== normalized ? m.path : undefined };
|
|
140
|
-
if (!group.examples.some(e => e.context === example.context))
|
|
141
|
-
group.examples.push(example);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const groups = Array.from(groupMap.values()).sort((a, b) => b.count - a.count || getDepth(b.normalized) - getDepth(a.normalized));
|
|
145
|
-
const totalMatches = matches.length;
|
|
146
|
-
const sourceCounts = accumulateSourceCounts(matches);
|
|
147
|
-
searchMatches.push({
|
|
148
|
-
requestId: ev.id,
|
|
149
|
-
timestamp: ev.timestamp,
|
|
150
|
-
count: 1,
|
|
151
|
-
groups,
|
|
152
|
-
totalMatches,
|
|
153
|
-
sourceCounts,
|
|
154
|
-
request,
|
|
155
|
-
response: resp,
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (searchMatches.length === 0) {
|
|
160
|
-
response.addResult(`No network requests found containing keyword: "${params.keyword}"`);
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
const aggMap = new Map();
|
|
164
|
-
for (const m of searchMatches) {
|
|
165
|
-
const key = `${m.request.method().toUpperCase()} ${normalizeUrlForGrouping(m.request.url())}`;
|
|
166
|
-
const existing = aggMap.get(key);
|
|
167
|
-
const eventScore = computeEventScore(m);
|
|
168
|
-
if (!existing) {
|
|
169
|
-
aggMap.set(key, {
|
|
170
|
-
key,
|
|
171
|
-
request: m.request,
|
|
172
|
-
response: m.response,
|
|
173
|
-
ids: [m.requestId],
|
|
174
|
-
count: m.count,
|
|
175
|
-
totalMatches: m.totalMatches,
|
|
176
|
-
groups: [...m.groups],
|
|
177
|
-
primaryGroups: m.groups,
|
|
178
|
-
primaryEventId: m.requestId,
|
|
179
|
-
primaryTimestamp: m.timestamp,
|
|
180
|
-
primarySourceCounts: cloneSourceCounts(m.sourceCounts),
|
|
181
|
-
primaryMatches: m.totalMatches,
|
|
182
|
-
primaryScore: eventScore,
|
|
183
|
-
});
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
existing.ids.push(m.requestId);
|
|
187
|
-
existing.count += m.count;
|
|
188
|
-
existing.totalMatches += m.totalMatches;
|
|
189
|
-
existing.groups = mergeGroupedMatches(existing.groups, m.groups);
|
|
190
|
-
if (!existing.response && m.response)
|
|
191
|
-
existing.response = m.response;
|
|
192
|
-
const replacePrimary = eventScore > existing.primaryScore || (eventScore === existing.primaryScore && m.timestamp > existing.primaryTimestamp);
|
|
193
|
-
if (!replacePrimary)
|
|
194
|
-
continue;
|
|
195
|
-
existing.request = m.request;
|
|
196
|
-
existing.response = m.response;
|
|
197
|
-
existing.primaryGroups = m.groups;
|
|
198
|
-
existing.primaryEventId = m.requestId;
|
|
199
|
-
existing.primaryTimestamp = m.timestamp;
|
|
200
|
-
existing.primarySourceCounts = cloneSourceCounts(m.sourceCounts);
|
|
201
|
-
existing.primaryMatches = m.totalMatches;
|
|
202
|
-
existing.primaryScore = eventScore;
|
|
203
|
-
}
|
|
204
|
-
const aggregatedMatches = Array.from(aggMap.values()).sort((a, b) => {
|
|
205
|
-
if (a.primaryScore !== b.primaryScore)
|
|
206
|
-
return b.primaryScore - a.primaryScore;
|
|
207
|
-
if (a.primaryMatches !== b.primaryMatches)
|
|
208
|
-
return b.primaryMatches - a.primaryMatches;
|
|
209
|
-
return b.primaryTimestamp - a.primaryTimestamp;
|
|
210
|
-
});
|
|
211
|
-
const topMatches = aggregatedMatches.slice(0, MAX_SEARCH_RESULTS);
|
|
212
|
-
const structured = {
|
|
213
|
-
keyword: params.keyword,
|
|
214
|
-
requests: topMatches.map(m => {
|
|
215
|
-
const method = m.request.method().toUpperCase();
|
|
216
|
-
const url = m.request.url();
|
|
217
|
-
// Normalize URL for grouping only; not returned in output
|
|
218
|
-
const status = m.response ? m.response.status() : undefined;
|
|
219
|
-
const queryKeys = (() => {
|
|
220
|
-
try {
|
|
221
|
-
const u = new URL(url);
|
|
222
|
-
const keys = new Set();
|
|
223
|
-
u.searchParams.forEach((_, k) => {
|
|
224
|
-
if (k)
|
|
225
|
-
keys.add(k);
|
|
226
|
-
});
|
|
227
|
-
return Array.from(keys);
|
|
228
|
-
}
|
|
229
|
-
catch {
|
|
230
|
-
return [];
|
|
231
|
-
}
|
|
232
|
-
})();
|
|
233
|
-
const primaryGroups = m.primaryGroups
|
|
234
|
-
.slice()
|
|
235
|
-
.sort((a, b) => b.count - a.count || getDepth(b.normalized) - getDepth(a.normalized))
|
|
236
|
-
.slice(0, MAX_GROUPS_TO_SHOW);
|
|
237
|
-
const paths = primaryGroups.map(group => ({
|
|
238
|
-
path: formatGroupPath(group),
|
|
239
|
-
examples: extractExamples(group),
|
|
240
|
-
}));
|
|
241
|
-
const sourceCounts = cloneSourceCounts(m.primarySourceCounts);
|
|
242
|
-
const score = Number(m.primaryScore.toFixed(2));
|
|
243
|
-
return {
|
|
244
|
-
summary: formatNetworkSummaryLine({
|
|
245
|
-
method,
|
|
246
|
-
url,
|
|
247
|
-
status: status || 0,
|
|
248
|
-
statusText: m.response?.statusText(),
|
|
249
|
-
postData: m.request.postData(),
|
|
250
|
-
headers: {},
|
|
251
|
-
}),
|
|
252
|
-
primaryEventId: m.primaryEventId,
|
|
253
|
-
eventIds: m.ids.slice(0, 5),
|
|
254
|
-
score,
|
|
255
|
-
sourceCounts,
|
|
256
|
-
params: queryKeys.slice(0, 6),
|
|
257
|
-
paths,
|
|
258
|
-
};
|
|
259
|
-
})
|
|
260
|
-
};
|
|
261
|
-
response.addResult(JSON.stringify(structured));
|
|
262
|
-
}
|
|
263
|
-
catch (error) {
|
|
264
|
-
response.addResult(`Failed to search network requests: ${error.message}`);
|
|
265
|
-
}
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
export default [networkSearch];
|
package/lib/tools/pdf.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
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
|
-
];
|