Using chrome.debugger devtools API to attach to a remote target to issue CDP commands

122 views
Skip to first unread message

Abhishek Ghosh

unread,
Jun 5, 2024, 8:18:02 PMJun 5
to chrome-debugging-protocol
Hello everyone! 

I am trying to create a chrome devtools extension where I can debug code in a normal service worker of a website (not extension service worker). To do this, I am using chrome.debugger APIs in my extension to attach to the website's service worker target and executing commands (like Runtime.evaluate etc.).

I also want to be able to do the same for remote debugging targets, i.e. when website is open on Chrome for Android on a phone, which I have connected with my laptop using a USB cable to do remote debugging using desktop chrome's dev tools (a common practice). In this case, I am trying to get the dev tools extension (installed on desktop chrome) to be able to connect to the service worker target running remotely on the android phone's chrome browser, so that I can continue debugging it. But I am not able to find a way to connect using chrome.debugger.* APIs to connect to this remote target. 

I've tried enabling device discovery, remote targets and see such remote targets pop up generally in chrome's own discovery somewhere in the form of <devicename>:chrome_devtools_remote:<targetid> or when explicitly enabling port forwarding, something like localhost:9222:<targetid> but I cant seem to find a way to connect from my extension (it always fails with a "runtime.lastError: No target with given id ..." message, even though in general I can see the browser itself is able to connect to it and inspect (naturally!). I've been snooping around the Protocol Monitor in Chrome quite a bit in the hope of gaining some clue but so far, haven't really been able to crack this mystery :( 

Is this even possible, or am I even going in the right direction? I am fairly new to chrome extensions and CDP, so any guidance, alternate ideas, direction, hacks or workaround are highly appreciated! If this is not the right forum for this question, please also feel free to point me to a better place.

Thank you so much for your time!

- Abhishek

Alex Rudenko

unread,
Jun 7, 2024, 4:51:14 AMJun 7
to chrome-debugging-protocol
Hi Abhishek,

I think accessing remote targets over chrome.debugger is not currently possible but pfaffe@ suggested that chrome.devtools API might work (in particular, script eval via the inspectedWindow). 
Perhaps you could give it a try and see if it works for service workers. If it does not, perhaps it could be a feature request for the DevTools extension API.

Best regards,
Alex

Abhishek Ghosh

unread,
Jun 7, 2024, 6:08:47 PMJun 7
to chrome-debugging-protocol
Thank you Alex. 

I had seen that the inspectedWindow API does seem to work (at least partially) for remote targets, where .eval() worked for some reason although .tabId seems to be null. 

Since I need to find a target one step deeper down (the service worker controlling the inspected window) and attach to it, seems like I'm out of luck at the moment with the chrome.debugger APIs. I was hopeful because it seems the DevTools and Protocol Monitor itself is able to do this pretty well and conveniently (I am able to choose the remote target I think from there from a drop down to send a CDP command to it), so I thought maybe I just need to find the right configurations or APIs to hook on to somehow. 

A feature request to the DevTools extension API sounds like a good idea overall where .getTargets() is able to discover remote targets as well that we can connect to using .attach(), thank you for the suggestion. 

- Abhishek
Reply all
Reply to author
Forward
0 new messages