cdp-tunnel 2.5.15 → 2.5.17

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.
@@ -1,10 +1,11 @@
1
1
  var LocalHandler = (function() {
2
2
  function browserGetVersion() {
3
3
  var userAgent = navigator.userAgent || '';
4
- var chromeVersion = CDPUtils.getChromeVersion(userAgent);
4
+ var match = userAgent.match(/Chrome\/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/);
5
+ var product = match ? 'Chrome/' + match[1] : 'Chrome';
5
6
  return {
6
7
  protocolVersion: '1.3',
7
- product: chromeVersion ? 'Chrome/' + chromeVersion : 'Chrome',
8
+ product: product,
8
9
  revision: '',
9
10
  userAgent: userAgent,
10
11
  jsVersion: ''
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "CDP Bridge",
4
- "version": "2.5.15",
4
+ "version": "2.5.17",
5
5
  "description": "Chrome DevTools Protocol Bridge for Playwright/Puppeteer automation",
6
6
  "permissions": [
7
7
  "debugger",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdp-tunnel",
3
- "version": "2.5.15",
3
+ "version": "2.5.17",
4
4
  "description": "Bridge Chrome's debugger API to WebSocket — control your existing browser with Playwright/Puppeteer via CDP",
5
5
  "main": "server/proxy-server.js",
6
6
  "bin": "./cli/index.js",
@@ -53,7 +53,7 @@
53
53
  "LICENSE"
54
54
  ],
55
55
  "dependencies": {
56
- "cdp-tunnel": "^2.5.14",
56
+ "cdp-tunnel": "^2.5.15",
57
57
  "commander": "^12.0.0",
58
58
  "ws": "^8.16.0"
59
59
  },
@@ -159,6 +159,7 @@ const { version: PKG_VERSION } = require('../package.json');
159
159
 
160
160
  let cachedTargets = [];
161
161
  let lastTargetsUpdate = 0;
162
+ let cachedBrowserVersion = null;
162
163
 
163
164
  console.log('='.repeat(60));
164
165
  console.log(` WebSocket CDP Proxy Server v${PKG_VERSION}`);
@@ -187,6 +188,41 @@ function buildTargetWebSocketUrl(req, targetId) {
187
188
  return `ws://${getHost(req)}/devtools/page/${targetId}`;
188
189
  }
189
190
 
191
+ function invalidateTargetsCache() {
192
+ lastTargetsUpdate = 0;
193
+ }
194
+
195
+ async function requestVersionFromPlugin() {
196
+ if (cachedBrowserVersion) return cachedBrowserVersion;
197
+
198
+ const plugin = pluginConnections.values().next().value;
199
+ if (!plugin || plugin.readyState !== WebSocket.OPEN) {
200
+ return null;
201
+ }
202
+
203
+ return new Promise((resolve) => {
204
+ const requestId = `version_${Date.now()}`;
205
+ const timeout = setTimeout(() => resolve(null), 2000);
206
+
207
+ const handler = (data) => {
208
+ try {
209
+ const msg = JSON.parse(data.toString());
210
+ if (msg.id === requestId && msg.result) {
211
+ clearTimeout(timeout);
212
+ plugin.off('message', handler);
213
+ if (msg.result.product || msg.result.userAgent) {
214
+ cachedBrowserVersion = msg.result;
215
+ }
216
+ resolve(cachedBrowserVersion || msg.result);
217
+ }
218
+ } catch (e) {}
219
+ };
220
+
221
+ plugin.on('message', handler);
222
+ plugin.send(JSON.stringify({ id: requestId, method: 'Browser.getVersion' }));
223
+ });
224
+ }
225
+
190
226
  async function requestTargetsFromPlugin() {
191
227
  const now = Date.now();
192
228
  if (now - lastTargetsUpdate < CONFIG.TARGETS_CACHE_TTL && cachedTargets.length > 0) {
@@ -229,11 +265,14 @@ async function handleHttpRequest(req, res) {
229
265
  const url = new URL(req.url, `http://localhost:${PORT}`);
230
266
 
231
267
  if (url.pathname === '/json/version' || url.pathname === '/json/version/') {
268
+ const ver = await requestVersionFromPlugin();
269
+ const userAgent = ver?.userAgent || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.86 Safari/537.36';
270
+ const product = ver?.product || 'Chrome/131.0.6778.86';
232
271
  const payload = {
233
- Browser: `Chrome/131.0.6778.86`,
234
- 'Protocol-Version': '1.3',
235
- 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.86 Safari/537.36',
236
- 'V8-Version': '13.1.201.8',
272
+ Browser: product,
273
+ 'Protocol-Version': ver?.protocolVersion || '1.3',
274
+ 'User-Agent': userAgent,
275
+ 'V8-Version': ver?.jsVersion || '',
237
276
  'WebKit-Version': '537.36',
238
277
  webSocketDebuggerUrl: buildWebSocketDebuggerUrl(req)
239
278
  };
@@ -587,6 +626,8 @@ function handlePluginConnection(ws, clientInfo) {
587
626
  if (!match) {
588
627
  console.log(` ↳ Run "cdp-tunnel update" or reload the extension to sync versions`);
589
628
  }
629
+ cachedBrowserVersion = null;
630
+ requestVersionFromPlugin();
590
631
  return;
591
632
  }
592
633
 
@@ -745,6 +786,16 @@ function handlePluginConnection(ws, clientInfo) {
745
786
  clientWs.send(msgStr);
746
787
  console.log(`[ATTACHED EVENT] Sent cached event to client: ${mapping.clientId}`);
747
788
  }
789
+ const newTargetInfo = cachedCreated?.parsed?.params?.targetInfo
790
+ || cachedEvent?.parsed?.params?.targetInfo;
791
+ if (newTargetInfo) {
792
+ const exists = cachedTargets.some(t => t.targetId === targetId);
793
+ if (!exists) {
794
+ cachedTargets.push(newTargetInfo);
795
+ }
796
+ } else {
797
+ invalidateTargetsCache();
798
+ }
748
799
  }
749
800
  // 过滤 Target.getTargets 响应,只返回该客户端拥有的 target
750
801
  if (mapping.isGetTargets && parsed.result && parsed.result.targetInfos) {
@@ -762,6 +813,7 @@ function handlePluginConnection(ws, clientInfo) {
762
813
  targetIdToClientId.delete(mapping.closeTargetId);
763
814
  console.log(`[CLOSE TARGET CLEANUP] removed targetId=${mapping.closeTargetId?.substring(0,8)} from mapping`);
764
815
  }
816
+ invalidateTargetsCache();
765
817
  }
766
818
 
767
819
  // 然后发送响应给客户端