Handle the debugger detaching in chrome extension

1.2k Views Asked by At

In accordance with the Debugger DevTools API, there is no debugger detach event. Can I catch the event for detaching the debugger when the user clicking "cancel" e.g.?

Sample code for attach:

chrome.debugger.attach(
    {
        tabId: tabId
    },
    '1.3',
    () => {
        chrome.debugger.sendCommand(
            {
                tabId: tabId
            },
            'Debugger.enable',
            {},
            result => {
                // ...
            }
        );
    }
);
2

There are 2 best solutions below

0
On BEST ANSWER

If you're looking to catch your debugger connection being closed (as opposed to an event happening within that connection), there is a chrome.debugger.onDetach event provided by the API.

0
On

I made a workaround that suits me: setInterval with try/catch some debugger command.

const intervalId = setInterval(async () => {
    try {
        // await send 'Debugger.setBreakpointsActive' with active=true
    } catch (error) {
        clearInterval(intervalId);
        onDetach();
    }
}, 500);