cdp-tunnel 3.2.3 → 3.2.5
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/package.json
CHANGED
|
@@ -38,6 +38,7 @@ class PortPoolManager {
|
|
|
38
38
|
this.portIndex = portIndex;
|
|
39
39
|
this.port = port;
|
|
40
40
|
this.targetIds = new Set(); // 这个端口创建的所有 targetId
|
|
41
|
+
this.targetUrls = new Map(); // targetId → url(navigate 时更新)
|
|
41
42
|
this.tabIds = new Set(); // Chrome tabId(扩展端返回的)
|
|
42
43
|
this.clients = new Set(); // 连接到这个端口的 client WebSocket
|
|
43
44
|
this.cdpIdCounter = 1; // CDP 命令 id 计数器
|
|
@@ -157,7 +158,7 @@ class PortPoolManager {
|
|
|
157
158
|
targets.push({
|
|
158
159
|
id: targetId,
|
|
159
160
|
type: 'page',
|
|
160
|
-
url: 'about:blank',
|
|
161
|
+
url: session.targetUrls.get(targetId) || 'about:blank',
|
|
161
162
|
title: '',
|
|
162
163
|
webSocketDebuggerUrl: `ws://localhost:${session.port}/devtools/page/${targetId}`
|
|
163
164
|
});
|
|
@@ -208,21 +209,25 @@ class PortPoolManager {
|
|
|
208
209
|
try { msg = JSON.parse(data.toString()); } catch { return; }
|
|
209
210
|
|
|
210
211
|
if (msg.id !== undefined) {
|
|
212
|
+
// createTarget 串行化(避免并发 chrome.tabs.create 回调串)
|
|
213
|
+
if (msg.method === 'Target.createTarget') {
|
|
214
|
+
while (session.createTargetLock) { await session.createTargetLock; }
|
|
215
|
+
session.createTargetLock = new Promise(r => { session._releaseCreateTarget = r; });
|
|
216
|
+
}
|
|
217
|
+
|
|
211
218
|
// 合成输入命令:先 bringToFront + 等待,再发原命令
|
|
212
219
|
if (SYNTHETIC_INPUT.indexOf(msg.method) >= 0 && msg.sessionId) {
|
|
213
220
|
await this._ensureVisible(session, pluginWs, msg.sessionId);
|
|
214
221
|
}
|
|
215
222
|
|
|
216
|
-
if (msg.method === 'Network.enable' || msg.method === 'Page.startScreencast') {
|
|
217
|
-
// 已转发,继续
|
|
218
|
-
}
|
|
219
|
-
|
|
220
223
|
// 命令:分配新 id,记录映射(含 method 名供响应后处理),转发给 plugin
|
|
221
224
|
const newId = `pool${session.portIndex}_${msg.id}`;
|
|
222
225
|
session.pendingRequests.set(newId, {
|
|
223
226
|
originalId: msg.id,
|
|
224
227
|
method: msg.method,
|
|
225
|
-
clientWs: ws
|
|
228
|
+
clientWs: ws,
|
|
229
|
+
params: msg.params,
|
|
230
|
+
sessionId: msg.sessionId
|
|
226
231
|
});
|
|
227
232
|
|
|
228
233
|
const forwarded = { ...msg, id: newId, __portIndex: session.portIndex, __clientId: `pool_${session.port}` };
|
|
@@ -268,11 +273,31 @@ class PortPoolManager {
|
|
|
268
273
|
const pending = session.pendingRequests.get(msg.id);
|
|
269
274
|
session.pendingRequests.delete(msg.id);
|
|
270
275
|
|
|
271
|
-
// 如果是 createTarget 响应,记录 targetId → portIndex 归属
|
|
276
|
+
// 如果是 createTarget 响应,记录 targetId → portIndex 归属 + 初始 url + 释放锁
|
|
272
277
|
if (msg.result && msg.result.targetId) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
278
|
+
const tid = msg.result.targetId;
|
|
279
|
+
session.targetIds.add(tid);
|
|
280
|
+
session.targetUrls.set(tid, pending?.params?.url || 'about:blank');
|
|
281
|
+
this.targetToPort.set(tid, portIndex);
|
|
282
|
+
// 释放 createTarget 锁
|
|
283
|
+
if (session._releaseCreateTarget) {
|
|
284
|
+
session._releaseCreateTarget();
|
|
285
|
+
session._releaseCreateTarget = null;
|
|
286
|
+
session.createTargetLock = null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// 如果是 Page.navigate 响应,更新 targetId 的 url
|
|
291
|
+
if (pending && pending.method === 'Page.navigate' && pending.sessionId && msg.result) {
|
|
292
|
+
// 找 sessionId 对应的 targetId
|
|
293
|
+
for (const [tid, pidx] of this.targetToPort.entries()) {
|
|
294
|
+
if (pidx === portIndex) {
|
|
295
|
+
// 用 pending.params.url 更新
|
|
296
|
+
if (pending.params?.url) {
|
|
297
|
+
session.targetUrls.set(tid, pending.params.url);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
276
301
|
}
|
|
277
302
|
|
|
278
303
|
// 如果是 attachToTarget 响应,记录 sessionId → portIndex
|