cdp-tunnel 1.3.0 → 1.4.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/cli/index.js +104 -0
- package/extension-new/cdp/handler/local.js +35 -1
- package/extension-new/cdp/index.js +2 -0
- package/package.json +1 -1
package/cli/index.js
CHANGED
|
@@ -595,6 +595,109 @@ program
|
|
|
595
595
|
}
|
|
596
596
|
});
|
|
597
597
|
|
|
598
|
+
program
|
|
599
|
+
.command('diagnose')
|
|
600
|
+
.description('诊断 CDP Tunnel 连接问题')
|
|
601
|
+
.option('-p, --port <port>', '指定端口', parseInt)
|
|
602
|
+
.action(async (options) => {
|
|
603
|
+
const config = getConfig();
|
|
604
|
+
const port = options.port || config.port || 9221;
|
|
605
|
+
const http = require('http');
|
|
606
|
+
|
|
607
|
+
console.log('');
|
|
608
|
+
log('bold', '🔍 CDP Tunnel 诊断');
|
|
609
|
+
console.log('');
|
|
610
|
+
|
|
611
|
+
const running = isServerRunning();
|
|
612
|
+
log(running ? 'green' : 'red', ` 1. Proxy Server: ${running ? '运行中' : '❌ 未运行'}`);
|
|
613
|
+
if (!running) {
|
|
614
|
+
log('yellow', ' → 运行 cdp-tunnel start 启动服务器');
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
let httpOk = false;
|
|
618
|
+
try {
|
|
619
|
+
const result = await new Promise((resolve, reject) => {
|
|
620
|
+
http.get(`http://localhost:${port}/json/version`, res => {
|
|
621
|
+
let data = '';
|
|
622
|
+
res.on('data', chunk => data += chunk);
|
|
623
|
+
res.on('end', () => resolve(JSON.parse(data)));
|
|
624
|
+
}).on('error', reject);
|
|
625
|
+
});
|
|
626
|
+
httpOk = true;
|
|
627
|
+
log('green', ` 2. HTTP 端点: 正常 (Browser: ${result.Browser || 'unknown'})`);
|
|
628
|
+
} catch (err) {
|
|
629
|
+
log('red', ` 2. HTTP 端点: ❌ ${err.message}`);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const extStatus = checkChromeExtension();
|
|
633
|
+
log(extStatus.connected ? 'green' : 'red', ` 3. Chrome 扩展: ${extStatus.connected ? '已连接' : '❌ 未连接'}`);
|
|
634
|
+
if (!extStatus.connected) {
|
|
635
|
+
log('yellow', ' → 点击浏览器工具栏上的 CDP Bridge 图标');
|
|
636
|
+
log('yellow', ' → 或运行 cdp-tunnel extension 安装扩展');
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (httpOk) {
|
|
640
|
+
try {
|
|
641
|
+
const targets = await new Promise((resolve, reject) => {
|
|
642
|
+
http.get(`http://localhost:${port}/json/list`, res => {
|
|
643
|
+
let data = '';
|
|
644
|
+
res.on('data', chunk => data += chunk);
|
|
645
|
+
res.on('end', () => resolve(JSON.parse(data)));
|
|
646
|
+
}).on('error', reject);
|
|
647
|
+
});
|
|
648
|
+
log('green', ` 4. 可用 Targets: ${targets.length} 个`);
|
|
649
|
+
targets.forEach((t, i) => {
|
|
650
|
+
log('gray', ` ${i + 1}. ${t.title || t.url || 'unknown'} (${t.type})`);
|
|
651
|
+
});
|
|
652
|
+
} catch (err) {
|
|
653
|
+
log('red', ` 4. Targets: ❌ ${err.message}`);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const { isChromeRunning, findChromePath } = require('./chrome-manager');
|
|
658
|
+
const chromeRunning = isChromeRunning();
|
|
659
|
+
log(chromeRunning ? 'green' : 'yellow', ` 5. Chrome 进程: ${chromeRunning ? '运行中' : '未运行'}`);
|
|
660
|
+
|
|
661
|
+
if (chromeRunning) {
|
|
662
|
+
const chromePath = findChromePath();
|
|
663
|
+
if (chromePath) {
|
|
664
|
+
log('gray', ` 路径: ${chromePath}`);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
if (httpOk && extStatus.connected) {
|
|
669
|
+
log('cyan', ' 6. Playwright 连接测试...');
|
|
670
|
+
try {
|
|
671
|
+
const { chromium } = require('playwright');
|
|
672
|
+
const browser = await chromium.connectOverCDP(`http://localhost:${port}`, {
|
|
673
|
+
timeout: 10000
|
|
674
|
+
});
|
|
675
|
+
const contexts = browser.contexts();
|
|
676
|
+
log('green', ` ✅ 连接成功! ${contexts.length} 个上下文, 共 ${contexts.reduce((sum, ctx) => sum + ctx.pages().length, 0)} 个页面`);
|
|
677
|
+
await browser.close();
|
|
678
|
+
} catch (err) {
|
|
679
|
+
if (err.code === 'MODULE_NOT_FOUND') {
|
|
680
|
+
log('gray', ' ⏭ 跳过 (playwright 未安装)');
|
|
681
|
+
} else {
|
|
682
|
+
log('red', ` ❌ ${err.message}`);
|
|
683
|
+
log('yellow', ' → 检查 Chrome 是否有多个实例在运行');
|
|
684
|
+
log('yellow', ' → 尝试关闭所有 Chrome 后重新运行 cdp-tunnel start');
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
console.log('');
|
|
690
|
+
if (running && httpOk && extStatus.connected) {
|
|
691
|
+
log('green', '✅ 一切正常! 连接 ws://localhost:' + port + '/devtools/browser/...');
|
|
692
|
+
log('cyan', ' Playwright: chromium.connectOverCDP("http://localhost:' + port + '")');
|
|
693
|
+
} else {
|
|
694
|
+
log('yellow', '⚠️ 有问题需要修复,请根据上面的提示操作');
|
|
695
|
+
}
|
|
696
|
+
console.log('');
|
|
697
|
+
|
|
698
|
+
process.exit(0);
|
|
699
|
+
});
|
|
700
|
+
|
|
598
701
|
program.addHelpText('after', `
|
|
599
702
|
|
|
600
703
|
常用命令:
|
|
@@ -603,6 +706,7 @@ program.addHelpText('after', `
|
|
|
603
706
|
$ cdp-tunnel start --watchdog 服务崩溃时自动重启
|
|
604
707
|
$ cdp-tunnel status 查看状态
|
|
605
708
|
$ cdp-tunnel update 检查并更新
|
|
709
|
+
$ cdp-tunnel diagnose 诊断连接问题
|
|
606
710
|
$ cdp-tunnel extension 安装 Chrome 扩展
|
|
607
711
|
|
|
608
712
|
快速开始:
|
|
@@ -135,6 +135,39 @@ var LocalHandler = (function() {
|
|
|
135
135
|
return {};
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
function tabGetMuteStatus(params) {
|
|
139
|
+
var cdpOnly = params && params.cdpOnly;
|
|
140
|
+
var attachedTabIds = State.getAttachedTabIds();
|
|
141
|
+
|
|
142
|
+
return new Promise(function(resolve) {
|
|
143
|
+
chrome.tabs.query({}, function(tabs) {
|
|
144
|
+
if (chrome.runtime.lastError) {
|
|
145
|
+
resolve({ tabs: [] });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
var result = tabs;
|
|
150
|
+
if (cdpOnly) {
|
|
151
|
+
result = tabs.filter(function(t) {
|
|
152
|
+
return attachedTabIds.indexOf(t.id) !== -1;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
resolve({
|
|
157
|
+
tabs: result.map(function(tab) {
|
|
158
|
+
return {
|
|
159
|
+
id: tab.id,
|
|
160
|
+
url: tab.url || '',
|
|
161
|
+
title: tab.title || '',
|
|
162
|
+
muted: tab.mutedInfo ? tab.mutedInfo.muted : false,
|
|
163
|
+
mutedReason: tab.mutedInfo ? (tab.mutedInfo.reason || '') : ''
|
|
164
|
+
};
|
|
165
|
+
})
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
138
171
|
function getTargetInfos() {
|
|
139
172
|
return chrome.debugger.getTargets().then(function(targets) {
|
|
140
173
|
// 为每个有 tabId 的 target 查询 openerTabId
|
|
@@ -274,6 +307,7 @@ var LocalHandler = (function() {
|
|
|
274
307
|
emptyObject: emptyObject,
|
|
275
308
|
getTargetInfos: getTargetInfos,
|
|
276
309
|
getTargetInfoById: getTargetInfoById,
|
|
277
|
-
mapToTargetInfo: mapToTargetInfo
|
|
310
|
+
mapToTargetInfo: mapToTargetInfo,
|
|
311
|
+
tabGetMuteStatus: tabGetMuteStatus
|
|
278
312
|
};
|
|
279
313
|
})();
|
|
@@ -22,6 +22,8 @@ var CDP_HANDLERS = {
|
|
|
22
22
|
'Target.getBrowserContexts': { type: 'LOCAL', handler: LocalHandler.targetGetBrowserContexts },
|
|
23
23
|
'Target.attachToBrowserTarget': { type: 'LOCAL', handler: LocalHandler.targetAttachToBrowserTarget },
|
|
24
24
|
|
|
25
|
+
'Tab.getMuteStatus': { type: 'LOCAL', handler: LocalHandler.tabGetMuteStatus },
|
|
26
|
+
|
|
25
27
|
'SystemInfo.getInfo': { type: 'LOCAL', handler: LocalHandler.systemInfoGetInfo },
|
|
26
28
|
'SystemInfo.getProcessInfo': { type: 'LOCAL', handler: LocalHandler.systemInfoGetProcessInfo },
|
|
27
29
|
|