@liqi0816/chrome-devtools-mcp-rebrowser 1.0.0
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 +21 -0
- package/README.md +205 -0
- package/launch.mjs +39 -0
- package/package.json +48 -0
- package/patches/chrome-devtools-mcp/DevToolsConnectionAdapter.js.patch +17 -0
- package/postinstall.mjs +192 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Q
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# Chrome DevTools MCP with Rebrowser Anti-Detection Patches
|
|
2
|
+
|
|
3
|
+
This project wraps [chrome-devtools-mcp](https://github.com/ChromeDevTools/chrome-devtools-mcp) with [rebrowser-patches](https://github.com/rebrowser/rebrowser-patches) to make AI agents undetectable by anti-bot systems like Cloudflare and DataDome.
|
|
4
|
+
|
|
5
|
+
## š¤ AI
|
|
6
|
+
|
|
7
|
+
Yes, I used AI. I also committed the full conversations as the real design documents (`.ai-conversations/` - private submodule, ask me for access).
|
|
8
|
+
|
|
9
|
+
They're private because I haven't scrubbed my file paths and trash talk, not because I'm hiding anything.
|
|
10
|
+
|
|
11
|
+
Code is MIT. What license applies to human-AI conversation transcripts is an open question. Literally. Nobody knows yet.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### Prerequisites
|
|
16
|
+
|
|
17
|
+
- **Node.js** ā„ 20.19.0 LTS
|
|
18
|
+
- **Chrome** running with remote debugging enabled (go to `chrome://inspect/#remote-debugging`)
|
|
19
|
+
- **Git** installed (provides `patch.exe` on Windows, needed for applying patches)
|
|
20
|
+
|
|
21
|
+
### Usage via `npx` (recommended)
|
|
22
|
+
|
|
23
|
+
No cloning or manual setup needed. Just configure your MCP client:
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
{
|
|
27
|
+
"mcpServers": {
|
|
28
|
+
"chrome-devtools-mcp-rebrowser": {
|
|
29
|
+
"disabled": false,
|
|
30
|
+
"timeout": 60,
|
|
31
|
+
"type": "stdio",
|
|
32
|
+
"command": "npx",
|
|
33
|
+
"args": [
|
|
34
|
+
"-y",
|
|
35
|
+
"chrome-devtools-mcp-rebrowser@latest",
|
|
36
|
+
"--auto-connect",
|
|
37
|
+
"--performance-crux=false",
|
|
38
|
+
"--usage-statistics=false",
|
|
39
|
+
"--category-emulation=false",
|
|
40
|
+
"--category-performance=false",
|
|
41
|
+
"--category-network=false"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
That's it. The `postinstall` script automatically applies all patches after `npm install`.
|
|
49
|
+
|
|
50
|
+
### Usage from source (development)
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
git clone <repo-url>
|
|
54
|
+
cd chrome-devtools-mcp-rebrowser
|
|
55
|
+
npm install # postinstall applies patches automatically
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Then configure the MCP server to use the local entry point:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"mcpServers": {
|
|
63
|
+
"chrome-devtools-mcp-rebrowser": {
|
|
64
|
+
"disabled": false,
|
|
65
|
+
"timeout": 60,
|
|
66
|
+
"type": "stdio",
|
|
67
|
+
"command": "node",
|
|
68
|
+
"args": [
|
|
69
|
+
"/path/to/launch.mjs",
|
|
70
|
+
"--auto-connect",
|
|
71
|
+
"--performance-crux=false",
|
|
72
|
+
"--usage-statistics=false",
|
|
73
|
+
"--category-emulation=false",
|
|
74
|
+
"--category-performance=false",
|
|
75
|
+
"--category-network=false"
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## How It Works
|
|
83
|
+
|
|
84
|
+
`chrome-devtools-mcp` is Google's official MCP server for Chrome DevTools. It bundles its own copy of Puppeteer (v24.39.1) internally. This project intercepts its `puppeteer.connect()` and `puppeteer.launch()` calls and redirects them through a separately installed, rebrowser-patched version of `puppeteer-core`.
|
|
85
|
+
|
|
86
|
+
### Architecture
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
launch.mjs (entry point)
|
|
90
|
+
ā
|
|
91
|
+
āā Imports patched puppeteer-core (node_modules/puppeteer-core)
|
|
92
|
+
ā āā puppeteer-core@24.39.1 + rebrowser-patches applied
|
|
93
|
+
ā
|
|
94
|
+
āā Imports bundled puppeteer from chrome-devtools-mcp
|
|
95
|
+
ā āā node_modules/chrome-devtools-mcp/build/src/third_party/index.js
|
|
96
|
+
ā
|
|
97
|
+
āā Monkey-patches bundled puppeteer's connect()/launch() methods
|
|
98
|
+
ā to redirect through the patched puppeteer-core
|
|
99
|
+
ā
|
|
100
|
+
āā Delegates to chrome-devtools-mcp's main entry point
|
|
101
|
+
|
|
102
|
+
postinstall.mjs (runs after npm install)
|
|
103
|
+
ā
|
|
104
|
+
āā Applies rebrowser-patches to puppeteer-core (with --fuzz=10)
|
|
105
|
+
ā
|
|
106
|
+
āā Applies DevToolsConnectionAdapter.js patch to chrome-devtools-mcp
|
|
107
|
+
|
|
108
|
+
DevToolsConnectionAdapter.js (patched)
|
|
109
|
+
ā
|
|
110
|
+
āā Blocks Runtime.enable calls from DevTools Universe models
|
|
111
|
+
to prevent CDP detection via the separate CDP session
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Why the Monkey-Patching?
|
|
115
|
+
|
|
116
|
+
- `chrome-devtools-mcp` bundles Puppeteer in a single rollup file (`third_party/index.js`), so you can't simply swap out `puppeteer-core` via `package.json`.
|
|
117
|
+
- `rebrowser-puppeteer` (the drop-in replacement) is only available up to v24.8.1, but `chrome-devtools-mcp@0.20.3` requires Puppeteer v24.39.1 APIs (e.g., `page.emulateFocusedPage()`).
|
|
118
|
+
- Solution: install `puppeteer-core@24.39.1` separately, apply `rebrowser-patches` to it, then redirect the bundled Puppeteer's calls through the patched version.
|
|
119
|
+
|
|
120
|
+
## Flags
|
|
121
|
+
|
|
122
|
+
All flags from `chrome-devtools-mcp` are passed transparently. Key ones:
|
|
123
|
+
|
|
124
|
+
| Flag | Description |
|
|
125
|
+
|------|-------------|
|
|
126
|
+
| `--auto-connect` | Connect to a running Chrome instance (requires remote debugging enabled) |
|
|
127
|
+
| `--browser-url http://127.0.0.1:9222` | Connect via HTTP URL |
|
|
128
|
+
| `--ws-endpoint ws://...` | Connect via WebSocket |
|
|
129
|
+
| `--headless` | Run in headless mode (Warning: more detectable) |
|
|
130
|
+
| `--no-usage-statistics` | Disable telemetry |
|
|
131
|
+
|
|
132
|
+
## Rebrowser Patches Environment Variables
|
|
133
|
+
|
|
134
|
+
You can fine-tune the anti-detection patches:
|
|
135
|
+
|
|
136
|
+
| Variable | Values | Default |
|
|
137
|
+
|----------|--------|---------|
|
|
138
|
+
| `REBROWSER_PATCHES_RUNTIME_FIX_MODE` | `addBinding`, `alwaysIsolated`, `enableDisable`, `0` (disable) | `addBinding` |
|
|
139
|
+
| `REBROWSER_PATCHES_SOURCE_URL` | Any string, or `0` to disable | `app.js` |
|
|
140
|
+
| `REBROWSER_PATCHES_UTILITY_WORLD_NAME` | Any string, or `0` to disable | `util` |
|
|
141
|
+
| `REBROWSER_PATCHES_DEBUG` | `1` to enable debug logging | (disabled) |
|
|
142
|
+
|
|
143
|
+
## What the Patches Fix
|
|
144
|
+
|
|
145
|
+
### rebrowser-patches (applied to puppeteer-core)
|
|
146
|
+
|
|
147
|
+
1. **`Runtime.Enable` leak** ā The primary detection vector. Anti-bot systems detect that `Runtime.Enable` was called (used by Puppeteer/Playwright). The patch avoids calling it and uses alternative methods to manage execution contexts.
|
|
148
|
+
|
|
149
|
+
2. **`sourceURL` fingerprint** ā Puppeteer adds `//# sourceURL=pptr:...` to evaluated scripts. The patch changes it to `//# sourceURL=app.js`.
|
|
150
|
+
|
|
151
|
+
3. **Utility world name** ā The default `__puppeteer_utility_world__` name is changed to `util`.
|
|
152
|
+
|
|
153
|
+
4. **Browser `_connection()` method** ā Exposes the CDP connection for advanced use cases (not detectable by websites).
|
|
154
|
+
|
|
155
|
+
### DevToolsConnectionAdapter.js (applied to chrome-devtools-mcp)
|
|
156
|
+
|
|
157
|
+
5. **DevTools Universe `Runtime.Enable` leak** ā Even with rebrowser-patches applied to puppeteer-core, `chrome-devtools-mcp` creates a *separate* CDP session per page via `page.createCDPSession()` in `DevtoolsUtils.js` and feeds it to a DevTools Universe. The Universe's RuntimeModel then calls `Runtime.enable` on that session ā completely bypassing the patched puppeteer-core. We patch `DevToolsConnectionAdapter.js` to intercept and block `Runtime.enable` calls in the `send()` method, consistent with how rebrowser-patches handles it inside Puppeteer's FrameManager.
|
|
158
|
+
|
|
159
|
+
### What the DevTools Universe CDP session is used for
|
|
160
|
+
|
|
161
|
+
`chrome-devtools-mcp` creates a per-page DevTools Universe (a lightweight instance of Chrome DevTools' SDK) for **source-map-aware stack trace symbolication** and **error introspection**:
|
|
162
|
+
|
|
163
|
+
| Feature | Model | What it does |
|
|
164
|
+
|---------|-------|-------------|
|
|
165
|
+
| Stack trace resolution | `DebuggerModel` + `DebuggerWorkspaceBinding` | Receives `Debugger.scriptParsed` events, downloads source maps, retranslates raw CDP stack frames into original source locations for `get_console_message` output |
|
|
166
|
+
| Exception details | `RuntimeModel` | Calls `Runtime.getExceptionDetails` to resolve full error info from `RemoteObject` references, including recursive `.cause` chain |
|
|
167
|
+
| Skip-all-pauses | `DebuggerModel` | Immediately calls `Debugger.setSkipAllPauses({skip: true})` so the MCP session never interferes with the user's DevTools debugging |
|
|
168
|
+
|
|
169
|
+
### Tradeoff of blocking `Runtime.enable`
|
|
170
|
+
|
|
171
|
+
Blocking `Runtime.enable` on the Universe session means the `RuntimeModel` won't receive execution context events, which degrades `getExceptionDetails()` (error cause chain resolution in `get_console_message`). However:
|
|
172
|
+
|
|
173
|
+
- **Core MCP tools are unaffected** ā `evaluate_script`, page snapshots, navigation, clicking, typing, screenshots all use Puppeteer APIs directly, not the Universe session.
|
|
174
|
+
- **Console message collection is unaffected** ā `PageCollector` listens on Puppeteer's own CDP session (`page._client()`), not the Universe session.
|
|
175
|
+
- **Stack traces from `DebuggerModel` still work** ā `Debugger.enable` is not blocked, so source-map-aware stack traces continue to function.
|
|
176
|
+
- **Only error cause chains degrade** ā The recursive `.cause` lookup via `RuntimeModel.getExceptionDetails()` may fail silently, omitting cause details from console error output.
|
|
177
|
+
|
|
178
|
+
This is an acceptable tradeoff since `Runtime.enable` is the primary CDP detection vector used by anti-bot systems.
|
|
179
|
+
|
|
180
|
+
## Troubleshooting
|
|
181
|
+
|
|
182
|
+
### "Could not connect to Chrome"
|
|
183
|
+
- Make sure Chrome is running
|
|
184
|
+
- Enable remote debugging: go to `chrome://inspect/#remote-debugging`
|
|
185
|
+
- Check that `DevToolsActivePort` file exists in Chrome's user data directory
|
|
186
|
+
|
|
187
|
+
### Patch fails with "FAILED" hunks
|
|
188
|
+
- The `es5-iife` hunk failure is expected and harmless
|
|
189
|
+
- If CJS/ESM hunks fail, the puppeteer-core version may have changed too much. Try adjusting the `--fuzz` value or manually applying the changes
|
|
190
|
+
|
|
191
|
+
### "emulateFocusedPage is not a function" (or similar)
|
|
192
|
+
- Version mismatch: ensure `puppeteer-core` in `package.json` matches the version in `chrome-devtools-mcp`'s `devDependencies` (currently `24.39.1`)
|
|
193
|
+
|
|
194
|
+
### Postinstall fails ā "`patch` command not found"
|
|
195
|
+
- On Linux/macOS: `patch` is usually pre-installed. If not: `sudo apt install patch` or `brew install gpatch`.
|
|
196
|
+
- On Windows: Install [Git for Windows](https://gitforwindows.org/) ā it bundles `patch.exe` at `C:\Program Files\Git\usr\bin\patch.exe`. Make sure it's on your PATH.
|
|
197
|
+
|
|
198
|
+
## Version Compatibility
|
|
199
|
+
|
|
200
|
+
| Package | Version |
|
|
201
|
+
|---------|---------|
|
|
202
|
+
| chrome-devtools-mcp | 0.20.3 |
|
|
203
|
+
| puppeteer-core | 24.39.1 |
|
|
204
|
+
| rebrowser-patches | 1.0.19 |
|
|
205
|
+
| Node.js | ā„ 20.19.0 |
|
package/launch.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Step 1: Import the patched puppeteer-core
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// This is puppeteer-core@24.39.1 with rebrowser-patches applied in-place.
|
|
12
|
+
// The patches disable Runtime.Enable (the primary CDP detection vector),
|
|
13
|
+
// change sourceURL from pptr:... to app.js, and rename the utility world.
|
|
14
|
+
import puppeteerCore from 'puppeteer-core';
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Step 2: Import the bundled puppeteer from chrome-devtools-mcp
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// ESM modules are cached by resolved URL, so this gives us a reference to the
|
|
20
|
+
// SAME puppeteer object that browser.js will use when it calls connect()/launch().
|
|
21
|
+
import { puppeteer as bundledPuppeteer } from 'chrome-devtools-mcp/build/src/third_party/index.js';
|
|
22
|
+
|
|
23
|
+
bundledPuppeteer.connect = function connect() {
|
|
24
|
+
process.stderr.write('[rebrowser-patch] Redirecting puppeteer.connect() through patched puppeteer-core\n');
|
|
25
|
+
return puppeteerCore.connect.apply(puppeteerCore, arguments);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
bundledPuppeteer.launch = function launch() {
|
|
29
|
+
process.stderr.write('[rebrowser-patch] Redirecting puppeteer.launch() through patched puppeteer-core\n');
|
|
30
|
+
return puppeteerCore.launch.apply(puppeteerCore, arguments);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Step 3: Hand off to chrome-devtools-mcp
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// This imports and runs chrome-devtools-mcp's main entry point. When it
|
|
37
|
+
// eventually calls puppeteer.connect() or puppeteer.launch() via
|
|
38
|
+
// browser.js ā third_party/index.js, it will hit our patched methods above.
|
|
39
|
+
await import('chrome-devtools-mcp/build/src/bin/chrome-devtools-mcp.js');
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@liqi0816/chrome-devtools-mcp-rebrowser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Chrome DevTools MCP server with rebrowser anti-detection patches ā undetectable by Cloudflare, DataDome, and other anti-bot systems",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mcp",
|
|
7
|
+
"chrome-devtools",
|
|
8
|
+
"rebrowser",
|
|
9
|
+
"anti-detection",
|
|
10
|
+
"puppeteer",
|
|
11
|
+
"cdp",
|
|
12
|
+
"model-context-protocol"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/liqi0816/chrome-devtools-mcp-rebrowser#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/liqi0816/chrome-devtools-mcp-rebrowser/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/liqi0816/chrome-devtools-mcp-rebrowser.git"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "LI, Qi <goodlq11@gmail.com>",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "launch.mjs",
|
|
26
|
+
"bin": {
|
|
27
|
+
"chrome-devtools-mcp-rebrowser": "./launch.mjs"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"launch.mjs",
|
|
31
|
+
"postinstall.mjs",
|
|
32
|
+
"patches/",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"postinstall": "node postinstall.mjs",
|
|
38
|
+
"start": "node launch.mjs"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"chrome-devtools-mcp": "^0.20.3",
|
|
42
|
+
"puppeteer-core": "^24.39.1",
|
|
43
|
+
"rebrowser-patches": "^1.0.19"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
--- a/build/src/DevToolsConnectionAdapter.js
|
|
2
|
+
+++ b/build/src/DevToolsConnectionAdapter.js
|
|
3
|
+
@@ -27,6 +27,14 @@
|
|
4
|
+
if (!session) {
|
|
5
|
+
throw new Error('Unknown session ' + sessionId);
|
|
6
|
+
}
|
|
7
|
+
+ // rebrowser-patches: block Runtime.enable to prevent CDP detection.
|
|
8
|
+
+ // The DevTools Universe models call Runtime.enable on their own CDP session,
|
|
9
|
+
+ // which leaks as a detectable CDP marker. We suppress it here just like
|
|
10
|
+
+ // rebrowser-patches suppresses it inside Puppeteer's FrameManager.
|
|
11
|
+
+ if (method === 'Runtime.enable' && process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] !== '0') {
|
|
12
|
+
+ process.env['REBROWSER_PATCHES_DEBUG'] && console.error('[rebrowser-patches][DevToolsConnection] Blocked Runtime.enable on session', sessionId);
|
|
13
|
+
+ return Promise.resolve({ result: {} });
|
|
14
|
+
+ }
|
|
15
|
+
// Rolled protocol version between puppeteer and DevTools doesn't necessarily match
|
|
16
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
17
|
+
return session
|
package/postinstall.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Postinstall script ā auto-applies rebrowser-patches and the
|
|
5
|
+
* DevToolsConnectionAdapter patch so that `npx chrome-devtools-mcp-rebrowser`
|
|
6
|
+
* works out of the box with zero manual setup.
|
|
7
|
+
*
|
|
8
|
+
* Runs after `npm install` (via package.json "postinstall" script).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { execFile as execFileCb } from 'node:child_process';
|
|
12
|
+
import { promisify } from 'node:util';
|
|
13
|
+
import { createRequire } from 'node:module';
|
|
14
|
+
import { dirname, join, resolve } from 'node:path';
|
|
15
|
+
import { readFile, writeFile, unlink, mkdtemp } from 'node:fs/promises';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
|
|
18
|
+
const execFile = promisify(execFileCb);
|
|
19
|
+
const require = createRequire(import.meta.url);
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// Helpers
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
/** Resolve the directory of an installed package by its main/package.json */
|
|
26
|
+
function pkgDir(packageName) {
|
|
27
|
+
const entry = require.resolve(`${packageName}/package.json`);
|
|
28
|
+
return dirname(entry);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Find `patch` executable ā on Windows try Git's bundled copy too */
|
|
32
|
+
async function findPatch() {
|
|
33
|
+
for (const base of [
|
|
34
|
+
'',
|
|
35
|
+
'C:\\Program Files\\Git\\usr\\bin',
|
|
36
|
+
'C:\\Program Files (x86)\\Git\\usr\\bin',
|
|
37
|
+
]) {
|
|
38
|
+
const cmd = join(base, 'patch');
|
|
39
|
+
try {
|
|
40
|
+
await execFile(cmd, ['--version']);
|
|
41
|
+
return cmd;
|
|
42
|
+
} catch {
|
|
43
|
+
// not found, try next
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
console.error(
|
|
47
|
+
'ā Could not find the `patch` command.\n' +
|
|
48
|
+
' On Linux/macOS it is usually pre-installed.\n' +
|
|
49
|
+
' On Windows, install Git for Windows ā it bundles patch.exe in\n' +
|
|
50
|
+
' C:\\Program Files\\Git\\usr\\bin\\patch.exe\n' +
|
|
51
|
+
' and make sure it is on your PATH.',
|
|
52
|
+
);
|
|
53
|
+
throw new Error('Could not find `patch` executable');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Strip hunks that target paths matching `filterRe` from a unified diff.
|
|
58
|
+
* Returns the filtered patch as a string.
|
|
59
|
+
*/
|
|
60
|
+
function stripHunksFromPatch(patchContent, filterRe) {
|
|
61
|
+
const lines = patchContent.split('\n');
|
|
62
|
+
const kept = [];
|
|
63
|
+
let skipping = false;
|
|
64
|
+
|
|
65
|
+
for (const line of lines) {
|
|
66
|
+
// Each file section starts with "--- a/..."
|
|
67
|
+
if (line.startsWith('--- a/')) {
|
|
68
|
+
const path = line.slice('--- a/'.length);
|
|
69
|
+
skipping = filterRe.test(path);
|
|
70
|
+
}
|
|
71
|
+
if (!skipping) kept.push(line);
|
|
72
|
+
}
|
|
73
|
+
return kept.join('\n');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Apply a patch file to a target directory.
|
|
78
|
+
*
|
|
79
|
+
* Captures stdout/stderr so we can distinguish "already applied" from real
|
|
80
|
+
* failures. With `--forward`, GNU patch exits 1 both when every hunk was
|
|
81
|
+
* previously applied *and* when some hunks genuinely fail. We tell the two
|
|
82
|
+
* apart by checking the output for FAILED hunks.
|
|
83
|
+
*/
|
|
84
|
+
async function applyPatch(patchBin, targetDir, patchFile, { fuzz } = {}) {
|
|
85
|
+
const args = [
|
|
86
|
+
'--batch',
|
|
87
|
+
'-p1',
|
|
88
|
+
`--input=${patchFile}`,
|
|
89
|
+
'--verbose',
|
|
90
|
+
'--no-backup-if-mismatch',
|
|
91
|
+
'--reject-file=-',
|
|
92
|
+
'--forward',
|
|
93
|
+
];
|
|
94
|
+
if (fuzz != null) args.push(`--fuzz=${fuzz}`);
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const { stdout, stderr } = await execFile(patchBin, args, { cwd: targetDir });
|
|
98
|
+
process.stdout.write(stdout);
|
|
99
|
+
process.stderr.write(stderr);
|
|
100
|
+
console.log(`ā Patch applied successfully in ${targetDir}`);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
// execFile rejects for any non-zero exit code.
|
|
103
|
+
process.stdout.write(err.stdout);
|
|
104
|
+
process.stderr.write(err.stderr);
|
|
105
|
+
|
|
106
|
+
// Check whether every hunk was "Reversed (or previously applied)".
|
|
107
|
+
// If so (and there are zero FAILED hunks), this is a genuine
|
|
108
|
+
// "already applied" situation.
|
|
109
|
+
const hasFailedHunks = /FAILED/i.test(err.stdout);
|
|
110
|
+
const hasReversed = /Reversed \(or previously applied\) patch detected/i.test(err.stdout);
|
|
111
|
+
|
|
112
|
+
if (hasReversed && !hasFailedHunks) {
|
|
113
|
+
console.log(`ā Patch already applied in ${targetDir} (skipped)`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Any other non-zero exit is a real error.
|
|
118
|
+
throw new Error(
|
|
119
|
+
`ā Patch failed in ${targetDir} (exit code ${err.code ?? err.status ?? '?'})`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// Main
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
async function main() {
|
|
129
|
+
console.log('\nš§ chrome-devtools-mcp-rebrowser: postinstall\n');
|
|
130
|
+
|
|
131
|
+
// 1. Locate patch executable
|
|
132
|
+
const patchBin = await findPatch();
|
|
133
|
+
console.log(`Using patch: ${patchBin}\n`);
|
|
134
|
+
|
|
135
|
+
// 2. Resolve package directories
|
|
136
|
+
const puppeteerCoreDir = pkgDir('puppeteer-core');
|
|
137
|
+
const rebrowserPatchesDir = pkgDir('rebrowser-patches');
|
|
138
|
+
const chromeDevtoolsMcpDir = pkgDir('chrome-devtools-mcp');
|
|
139
|
+
const ownDir = resolve(
|
|
140
|
+
dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/i, '$1')),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
console.log(`puppeteer-core: ${puppeteerCoreDir}`);
|
|
144
|
+
console.log(`rebrowser-patches: ${rebrowserPatchesDir}`);
|
|
145
|
+
console.log(`chrome-devtools-mcp: ${chromeDevtoolsMcpDir}`);
|
|
146
|
+
console.log(`own package: ${ownDir}\n`);
|
|
147
|
+
|
|
148
|
+
// 3. Apply rebrowser-patches to puppeteer-core (with fuzz for version tolerance)
|
|
149
|
+
// The upstream patch includes hunks for lib/es5-iife/ which is a bundled
|
|
150
|
+
// build that doesn't exist in every puppeteer-core release and doesn't
|
|
151
|
+
// affect Node-side behaviour. We strip those hunks so they can't cause
|
|
152
|
+
// spurious failures.
|
|
153
|
+
const rebrowserPatchFile = join(
|
|
154
|
+
rebrowserPatchesDir,
|
|
155
|
+
'patches',
|
|
156
|
+
'puppeteer-core',
|
|
157
|
+
'lib.patch',
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const rawPatch = await readFile(rebrowserPatchFile, 'utf8');
|
|
161
|
+
const filteredPatch = stripHunksFromPatch(rawPatch, /^lib\/es5-iife\//);
|
|
162
|
+
|
|
163
|
+
// Write the filtered patch to a temp file
|
|
164
|
+
const tmpDir = await mkdtemp(join(tmpdir(), 'rebrowser-'));
|
|
165
|
+
const filteredPatchFile = join(tmpDir, 'lib-no-es5-iife.patch');
|
|
166
|
+
await writeFile(filteredPatchFile, filteredPatch);
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
console.log('--- Applying rebrowser-patches to puppeteer-core (es5-iife stripped) ---');
|
|
170
|
+
await applyPatch(patchBin, puppeteerCoreDir, filteredPatchFile, { fuzz: 10 });
|
|
171
|
+
} finally {
|
|
172
|
+
// Clean up temp file
|
|
173
|
+
await unlink(filteredPatchFile).catch(() => {});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 4. Apply DevToolsConnectionAdapter patch to chrome-devtools-mcp
|
|
177
|
+
const adapterPatchFile = join(
|
|
178
|
+
ownDir,
|
|
179
|
+
'patches',
|
|
180
|
+
'chrome-devtools-mcp',
|
|
181
|
+
'DevToolsConnectionAdapter.js.patch',
|
|
182
|
+
);
|
|
183
|
+
console.log('\n--- Applying DevToolsConnectionAdapter patch to chrome-devtools-mcp ---');
|
|
184
|
+
await applyPatch(patchBin, chromeDevtoolsMcpDir, adapterPatchFile);
|
|
185
|
+
|
|
186
|
+
console.log('\nā
All patches applied. Ready to use!\n');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
main().catch((err) => {
|
|
190
|
+
console.error(err.message || err);
|
|
191
|
+
process.exit(1);
|
|
192
|
+
});
|