cdp-tunnel 1.0.7 → 1.0.9

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.
@@ -13,6 +13,17 @@ importScripts('cdp/index.js');
13
13
  importScripts('features/screencast.js');
14
14
  importScripts('features/automation-badge.js');
15
15
 
16
+ // 为字符串添加hashCode方法(用于生成颜色索引)
17
+ String.prototype.hashCode = function() {
18
+ var hash = 0;
19
+ for (var i = 0; i < this.length; i++) {
20
+ var char = this.charCodeAt(i);
21
+ hash = ((hash << 5) - hash) + char;
22
+ hash = hash & hash; // Convert to 32bit integer
23
+ }
24
+ return hash;
25
+ };
26
+
16
27
  (function() {
17
28
  'use strict';
18
29
 
@@ -132,7 +143,7 @@ importScripts('features/automation-badge.js');
132
143
 
133
144
  chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
134
145
  if (changeInfo.status === 'complete' && State.isTabAttached(tabId)) {
135
- AutomationBadge.inject(tabId);
146
+ // 不再注入自动化标识,改为通过标签分组区分
136
147
  }
137
148
  });
138
149
 
@@ -196,7 +207,60 @@ importScripts('features/automation-badge.js');
196
207
  var sessionId = CDPUtils.generateSessionId();
197
208
  State.mapSession(sessionId, tabId, targetId);
198
209
 
199
- AutomationBadge.inject(tabId);
210
+ // 将标签页添加到CDP组(添加延迟等待)
211
+ setTimeout(function() {
212
+ // 获取当前CDP客户端列表
213
+ var cdpClients = State.getCDPClients() || [];
214
+ var groupName;
215
+
216
+ // 如果有CDP客户端,使用第一个客户端的ID作为组名
217
+ if (cdpClients.length > 0) {
218
+ groupName = 'CDP-' + cdpClients[0].id.substring(0, 8);
219
+ } else {
220
+ // 如果没有CDP客户端,使用时间戳作为组名
221
+ groupName = 'CDP-' + Date.now().toString(36);
222
+ }
223
+
224
+ chrome.tabGroups.query({ title: groupName }, function(groups) {
225
+ if (groups.length > 0) {
226
+ // 找到现有的组,将标签页添加到组
227
+ chrome.tabs.group({ tabIds: tabId, groupId: groups[0].id }, function(groupId) {
228
+ if (chrome.runtime.lastError) {
229
+ Logger.error('[TabGroup] Failed to add tab to group:', chrome.runtime.lastError.message);
230
+ } else {
231
+ Logger.info('[TabGroup] Tab added to group:', groupId, 'Group name:', groupName);
232
+ }
233
+ });
234
+ } else {
235
+ // 创建新组并添加标签页
236
+ chrome.tabs.group({ tabIds: tabId }, function(groupId) {
237
+ if (chrome.runtime.lastError) {
238
+ Logger.error('[TabGroup] Failed to create group:', chrome.runtime.lastError.message);
239
+ return;
240
+ }
241
+ // 更新组的标题和颜色
242
+ if (groupId) {
243
+ // 为不同的组使用不同的颜色
244
+ var colors = ['blue', 'red', 'yellow', 'green', 'pink', 'purple', 'cyan', 'orange'];
245
+ var colorIndex = Math.abs(groupName.hashCode ? groupName.hashCode() : 0) % colors.length;
246
+ var groupColor = colors[colorIndex];
247
+
248
+ chrome.tabGroups.update(groupId, {
249
+ title: groupName,
250
+ color: groupColor
251
+ }, function(group) {
252
+ if (chrome.runtime.lastError) {
253
+ Logger.error('[TabGroup] Failed to update group:', chrome.runtime.lastError.message);
254
+ } else {
255
+ Logger.info('[TabGroup] Group created and updated:', group);
256
+ }
257
+ });
258
+ }
259
+ });
260
+ }
261
+ });
262
+ }, 2000); // 等待2秒
263
+
200
264
  Logger.info('[Tabs] Sending Target.attachedToTarget event');
201
265
 
202
266
  EventBuilder.send('Target.attachedToTarget', {
@@ -44,7 +44,7 @@ var SpecialHandler = (function() {
44
44
  var sessionId = CDPUtils.generateSessionId();
45
45
  State.mapSession(sessionId, tabId, targetId);
46
46
 
47
- AutomationBadge.inject(tabId);
47
+ addTabToAutomationGroup(tabId);
48
48
 
49
49
  return { sessionId: sessionId };
50
50
  });
@@ -78,12 +78,17 @@ var SpecialHandler = (function() {
78
78
 
79
79
  return new Promise(function(resolve, reject) {
80
80
  State.addPendingCreatedTabUrl(url);
81
- chrome.tabs.create({ url: url, active: !(params && params.background) }, function(tab) {
81
+ // 默认设置active: false,避免自动切换标签页
82
+ chrome.tabs.create({ url: url, active: false }, function(tab) {
82
83
  if (!tab || !tab.id) {
83
84
  State.removePendingCreatedTabUrl(url);
84
85
  reject(new Error('Failed to create tab'));
85
86
  return;
86
87
  }
88
+
89
+ // 将标签页添加到CDP Automation组
90
+ addTabToAutomationGroup(tab.id);
91
+
87
92
  var targetId = String(tab.id);
88
93
  State.addEmittedTarget(targetId);
89
94
  getTargetIdByTabId(tab.id).then(function(targetId) {
@@ -94,6 +99,86 @@ var SpecialHandler = (function() {
94
99
  });
95
100
  });
96
101
  }
102
+
103
+ function addTabToAutomationGroup(tabId) {
104
+ Logger.info('[TabGroup] Starting addTabToAutomationGroup for tabId:', tabId);
105
+
106
+ // 获取当前CDP客户端列表
107
+ var cdpClients = State.getCDPClients() || [];
108
+ var groupName;
109
+
110
+ // 如果有CDP客户端,使用第一个客户端的ID作为组名
111
+ if (cdpClients.length > 0) {
112
+ groupName = 'CDP-' + cdpClients[0].id.substring(0, 8);
113
+ Logger.info('[TabGroup] Using CDP client ID for group name:', groupName);
114
+ } else {
115
+ // 如果没有CDP客户端,使用固定的组名
116
+ groupName = 'CDP-Automation';
117
+ Logger.info('[TabGroup] No CDP client, using default group name:', groupName);
118
+ }
119
+
120
+ // 添加延迟等待,确保标签页完全加载后再分组
121
+ setTimeout(function() {
122
+ Logger.info('[TabGroup] Executing group operation after delay...');
123
+
124
+ // 查找或创建标签组
125
+ chrome.tabGroups.query({ title: groupName }, function(groups) {
126
+ Logger.info('[TabGroup] Found groups:', groups);
127
+
128
+ if (groups.length > 0) {
129
+ // 找到现有的组,将标签页添加到组
130
+ Logger.info('[TabGroup] Adding tab to existing group:', groups[0].id);
131
+ chrome.tabs.group({ tabIds: tabId, groupId: groups[0].id }, function(groupId) {
132
+ if (chrome.runtime.lastError) {
133
+ Logger.error('[TabGroup] Failed to add tab to group:', chrome.runtime.lastError.message);
134
+ } else {
135
+ Logger.info('[TabGroup] Tab added to group:', groupId, 'Group name:', groupName);
136
+ }
137
+ });
138
+ } else {
139
+ // 创建新组并添加标签页
140
+ Logger.info('[TabGroup] Creating new group...');
141
+ chrome.tabs.group({ tabIds: tabId }, function(groupId) {
142
+ if (chrome.runtime.lastError) {
143
+ Logger.error('[TabGroup] Failed to create group:', chrome.runtime.lastError.message);
144
+ return;
145
+ }
146
+ // 更新组的标题和颜色
147
+ if (groupId) {
148
+ Logger.info('[TabGroup] Group created with ID:', groupId);
149
+ // 为不同的组使用不同的颜色
150
+ var colors = ['blue', 'red', 'yellow', 'green', 'pink', 'purple', 'cyan', 'orange'];
151
+ var colorIndex = Math.abs(groupName.hashCode ? groupName.hashCode() : 0) % colors.length;
152
+ var groupColor = colors[colorIndex];
153
+
154
+ Logger.info('[TabGroup] Updating group with title:', groupName, 'color:', groupColor);
155
+ chrome.tabGroups.update(groupId, {
156
+ title: groupName,
157
+ color: groupColor
158
+ }, function(group) {
159
+ if (chrome.runtime.lastError) {
160
+ Logger.error('[TabGroup] Failed to update group:', chrome.runtime.lastError.message);
161
+ } else {
162
+ Logger.info('[TabGroup] Group created and updated:', group);
163
+ }
164
+ });
165
+ }
166
+ });
167
+ }
168
+ });
169
+ }, 2000); // 等待2秒
170
+ }
171
+
172
+ // 为字符串添加hashCode方法(用于生成颜色索引)
173
+ String.prototype.hashCode = function() {
174
+ var hash = 0;
175
+ for (var i = 0; i < this.length; i++) {
176
+ var char = this.charCodeAt(i);
177
+ hash = ((hash << 5) - hash) + char;
178
+ hash = hash & hash; // Convert to 32bit integer
179
+ }
180
+ return hash;
181
+ };
97
182
 
98
183
  function targetActivateTarget(context) {
99
184
  var params = context.params;
@@ -258,7 +343,7 @@ function checkTabVisibility(tabId) {
258
343
  var sessionId = CDPUtils.generateSessionId();
259
344
  State.mapSession(sessionId, target.tabId, targetId);
260
345
 
261
- AutomationBadge.inject(target.tabId);
346
+ addTabToAutomationGroup(target.tabId);
262
347
 
263
348
  if (config.waitForDebuggerOnStart) {
264
349
  State.addPendingDebuggerTab(target.tabId);
@@ -297,7 +382,7 @@ function checkTabVisibility(tabId) {
297
382
  var sessionId = CDPUtils.generateSessionId();
298
383
  State.mapSession(sessionId, tabId, targetId);
299
384
 
300
- AutomationBadge.inject(tabId);
385
+ addTabToAutomationGroup(tabId);
301
386
 
302
387
  var config = State.getAutoAttachConfig();
303
388
  if (config.waitForDebuggerOnStart) {
@@ -106,7 +106,6 @@ var DebuggerManager = (function() {
106
106
  State.addAttachedTab(tabId);
107
107
  State.setCurrentTabId(tabId);
108
108
  State.persist(tabId, true);
109
- AutomationBadge.inject(tabId);
110
109
 
111
110
  // 注入内部URL拦截脚本
112
111
  return injectInternalUrlBlocker(tabId);
@@ -9,7 +9,8 @@
9
9
  "activeTab",
10
10
  "scripting",
11
11
  "storage",
12
- "downloads"
12
+ "downloads",
13
+ "tabGroups"
13
14
  ],
14
15
  "host_permissions": [
15
16
  "<all_urls>"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdp-tunnel",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Chrome Extension CDP Proxy - 通过 Chrome 扩展将 chrome.debugger API 暴露为 WebSocket 端点",
5
5
  "main": "server/proxy-server.js",
6
6
  "bin": "./cli/index.js",