cdp-tunnel 1.0.7 → 1.0.8

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,8 +44,6 @@ var SpecialHandler = (function() {
44
44
  var sessionId = CDPUtils.generateSessionId();
45
45
  State.mapSession(sessionId, tabId, targetId);
46
46
 
47
- AutomationBadge.inject(tabId);
48
-
49
47
  return { sessionId: sessionId };
50
48
  });
51
49
  });
@@ -78,12 +76,17 @@ var SpecialHandler = (function() {
78
76
 
79
77
  return new Promise(function(resolve, reject) {
80
78
  State.addPendingCreatedTabUrl(url);
81
- chrome.tabs.create({ url: url, active: !(params && params.background) }, function(tab) {
79
+ // 默认设置active: false,避免自动切换标签页
80
+ chrome.tabs.create({ url: url, active: false }, function(tab) {
82
81
  if (!tab || !tab.id) {
83
82
  State.removePendingCreatedTabUrl(url);
84
83
  reject(new Error('Failed to create tab'));
85
84
  return;
86
85
  }
86
+
87
+ // 将标签页添加到CDP Automation组
88
+ addTabToAutomationGroup(tab.id);
89
+
87
90
  var targetId = String(tab.id);
88
91
  State.addEmittedTarget(targetId);
89
92
  getTargetIdByTabId(tab.id).then(function(targetId) {
@@ -94,6 +97,86 @@ var SpecialHandler = (function() {
94
97
  });
95
98
  });
96
99
  }
100
+
101
+ function addTabToAutomationGroup(tabId) {
102
+ Logger.info('[TabGroup] Starting addTabToAutomationGroup for tabId:', tabId);
103
+
104
+ // 获取当前CDP客户端列表
105
+ var cdpClients = State.getCDPClients() || [];
106
+ var groupName;
107
+
108
+ // 如果有CDP客户端,使用第一个客户端的ID作为组名
109
+ if (cdpClients.length > 0) {
110
+ groupName = 'CDP-' + cdpClients[0].id.substring(0, 8);
111
+ Logger.info('[TabGroup] Using CDP client ID for group name:', groupName);
112
+ } else {
113
+ // 如果没有CDP客户端,使用固定的组名
114
+ groupName = 'CDP-Automation';
115
+ Logger.info('[TabGroup] No CDP client, using default group name:', groupName);
116
+ }
117
+
118
+ // 添加延迟等待,确保标签页完全加载后再分组
119
+ setTimeout(function() {
120
+ Logger.info('[TabGroup] Executing group operation after delay...');
121
+
122
+ // 查找或创建标签组
123
+ chrome.tabGroups.query({ title: groupName }, function(groups) {
124
+ Logger.info('[TabGroup] Found groups:', groups);
125
+
126
+ if (groups.length > 0) {
127
+ // 找到现有的组,将标签页添加到组
128
+ Logger.info('[TabGroup] Adding tab to existing group:', groups[0].id);
129
+ chrome.tabs.group({ tabIds: tabId, groupId: groups[0].id }, function(groupId) {
130
+ if (chrome.runtime.lastError) {
131
+ Logger.error('[TabGroup] Failed to add tab to group:', chrome.runtime.lastError.message);
132
+ } else {
133
+ Logger.info('[TabGroup] Tab added to group:', groupId, 'Group name:', groupName);
134
+ }
135
+ });
136
+ } else {
137
+ // 创建新组并添加标签页
138
+ Logger.info('[TabGroup] Creating new group...');
139
+ chrome.tabs.group({ tabIds: tabId }, function(groupId) {
140
+ if (chrome.runtime.lastError) {
141
+ Logger.error('[TabGroup] Failed to create group:', chrome.runtime.lastError.message);
142
+ return;
143
+ }
144
+ // 更新组的标题和颜色
145
+ if (groupId) {
146
+ Logger.info('[TabGroup] Group created with ID:', groupId);
147
+ // 为不同的组使用不同的颜色
148
+ var colors = ['blue', 'red', 'yellow', 'green', 'pink', 'purple', 'cyan', 'orange'];
149
+ var colorIndex = Math.abs(groupName.hashCode ? groupName.hashCode() : 0) % colors.length;
150
+ var groupColor = colors[colorIndex];
151
+
152
+ Logger.info('[TabGroup] Updating group with title:', groupName, 'color:', groupColor);
153
+ chrome.tabGroups.update(groupId, {
154
+ title: groupName,
155
+ color: groupColor
156
+ }, function(group) {
157
+ if (chrome.runtime.lastError) {
158
+ Logger.error('[TabGroup] Failed to update group:', chrome.runtime.lastError.message);
159
+ } else {
160
+ Logger.info('[TabGroup] Group created and updated:', group);
161
+ }
162
+ });
163
+ }
164
+ });
165
+ }
166
+ });
167
+ }, 2000); // 等待2秒
168
+ }
169
+
170
+ // 为字符串添加hashCode方法(用于生成颜色索引)
171
+ String.prototype.hashCode = function() {
172
+ var hash = 0;
173
+ for (var i = 0; i < this.length; i++) {
174
+ var char = this.charCodeAt(i);
175
+ hash = ((hash << 5) - hash) + char;
176
+ hash = hash & hash; // Convert to 32bit integer
177
+ }
178
+ return hash;
179
+ };
97
180
 
98
181
  function targetActivateTarget(context) {
99
182
  var params = context.params;
@@ -297,8 +380,6 @@ function checkTabVisibility(tabId) {
297
380
  var sessionId = CDPUtils.generateSessionId();
298
381
  State.mapSession(sessionId, tabId, targetId);
299
382
 
300
- AutomationBadge.inject(tabId);
301
-
302
383
  var config = State.getAutoAttachConfig();
303
384
  if (config.waitForDebuggerOnStart) {
304
385
  State.addPendingDebuggerTab(tabId);
@@ -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.8",
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",