adding _onTabDetachedCallback to extension

This commit is contained in:
Tommy D. Rossi
2025-11-15 13:45:42 +01:00
parent 8dab7efa58
commit d92420eee4
2 changed files with 38 additions and 19 deletions
+20 -10
View File
@@ -116,18 +116,28 @@ class SimplifiedExtension {
}); });
debugLog('WebSocket connected successfully, creating RelayConnection instance'); debugLog('WebSocket connected successfully, creating RelayConnection instance');
this._connection = new RelayConnection(socket); this._connection = new RelayConnection({
this._connection.onclose = () => { ws: socket,
debugLog('=== Relay connection onclose callback triggered ==='); onClose: () => {
debugLog('Connected tabs before clearing:', Array.from(this._connectedTabs.keys())); debugLog('=== Relay connection onClose callback triggered ===');
this._connection = undefined; debugLog('Connected tabs before clearing:', Array.from(this._connectedTabs.keys()));
for (const tabId of this._connectedTabs.keys()) { this._connection = undefined;
debugLog('Updating icon to disconnected for tab:', tabId); for (const tabId of this._connectedTabs.keys()) {
debugLog('Updating icon to disconnected for tab:', tabId);
void this._updateIcon(tabId, 'disconnected');
}
this._connectedTabs.clear();
debugLog('All tabs cleared');
},
onTabDetached: (tabId) => {
debugLog('=== Manual tab detachment detected for tab:', tabId, '===');
debugLog('User closed debugger via Chrome automation bar');
this._connectedTabs.delete(tabId);
debugLog('Removed tab from _connectedTabs map');
void this._updateIcon(tabId, 'disconnected'); void this._updateIcon(tabId, 'disconnected');
debugLog('Updated icon to disconnected state');
} }
this._connectedTabs.clear(); });
debugLog('All tabs cleared');
};
} else { } else {
debugLog('Reusing existing connection'); debugLog('Reusing existing connection');
} }
+18 -9
View File
@@ -42,11 +42,17 @@ export class RelayConnection {
private _nextSessionId: number = 1; private _nextSessionId: number = 1;
private _ws: WebSocket; private _ws: WebSocket;
private _closed = false; private _closed = false;
private _onCloseCallback?: () => void;
private _onTabDetachedCallback?: (tabId: number) => void;
onclose?: () => void; constructor({ ws, onClose, onTabDetached }: {
ws: WebSocket;
constructor(ws: WebSocket) { onClose?: () => void;
onTabDetached?: (tabId: number) => void;
}) {
this._ws = ws; this._ws = ws;
this._onCloseCallback = onClose;
this._onTabDetachedCallback = onTabDetached;
this._ws.onmessage = async (event: MessageEvent) => { this._ws.onmessage = async (event: MessageEvent) => {
let message: ExtensionCommandMessage; let message: ExtensionCommandMessage;
try { try {
@@ -164,6 +170,9 @@ export class RelayConnection {
} }
}); });
this._attachedTabs.delete(tabId);
debugLog('Removed tab from _attachedTabs map. Remaining tabs:', this._attachedTabs.size);
chrome.debugger.detach(tab.debuggee) chrome.debugger.detach(tab.debuggee)
.then(() => { .then(() => {
debugLog('Successfully detached debugger from tab:', tabId); debugLog('Successfully detached debugger from tab:', tabId);
@@ -171,9 +180,6 @@ export class RelayConnection {
.catch((err) => { .catch((err) => {
debugLog('Error detaching debugger from tab:', tabId, err.message); debugLog('Error detaching debugger from tab:', tabId, err.message);
}); });
this._attachedTabs.delete(tabId);
debugLog('Removed tab from _attachedTabs map. Remaining tabs:', this._attachedTabs.size);
} }
close(message: string): void { close(message: string): void {
@@ -211,8 +217,8 @@ export class RelayConnection {
this._attachedTabs.clear(); this._attachedTabs.clear();
debugLog('All tabs cleared from map. Chrome automation bar should disappear in a few seconds.'); debugLog('All tabs cleared from map. Chrome automation bar should disappear in a few seconds.');
debugLog('Connection closed, calling onclose callback'); debugLog('Connection closed, calling onClose callback');
this.onclose?.(); this._onCloseCallback?.();
} }
private _onDebuggerEvent = (source: chrome.debugger.DebuggerSession, method: string, params: any): void => { private _onDebuggerEvent = (source: chrome.debugger.DebuggerSession, method: string, params: any): void => {
@@ -256,7 +262,10 @@ export class RelayConnection {
return; return;
} }
debugLog(`Debugger detached from tab ${tabId}: ${reason}`); debugLog(`Manual debugger detachment detected for tab ${tabId}: ${reason}`);
debugLog('User closed debugger via Chrome automation bar, calling onTabDetached callback');
this._onTabDetachedCallback?.(tabId);
this.detachTab(tabId); this.detachTab(tabId);
}; };