问题
This is the code from my chrome extension, as far as i am aware it should return all targets that i can attach a debugger to:
chrome.browserAction.onClicked.addListener(function(tab) {
console.log('launching extention');
chrome.debugger.getTargets(function(result){
console.log('Result!');
console.log("count: "+result.length);
for (index = 0; index < result.length; index++) {
console.log(index+": "+result[index].url);
}
});
});
This is my console output from the above extension:

This shows only tabs open in the chrome browser returned as debuggable targets.
I have my device connected and setup correctly so i can debug android chrome tabs.

Is the method i am using to try to attach to the devices debugger correct?
Related Docs Links; remote-debugging, debugger, debugger-protocol, debugging-clients.
回答1:
Whoops. Seems like this API does not allow to attach to device pages. Worse, I can't find any feature requests to enable it to do so.
Therefore, you will need to go low-level, and use the "legacy workflow" to expose the debugger for raw access on the phone you're connecting.
Essentially, the important part is running ADB command
adb forward tcp:9222 localabstract:chrome_devtools_remote
that exposes the debugging interface of Android Chrome at localhost:9222
, after which you can use XHR / WebSockets to relay raw protocol messages.
I'm afraid that's the only way; if you need the ADB step to be done automatically, you can use a Native Host script. Note that you still can't bypass the "pairing" part that needs to be done on the phone.
来源:https://stackoverflow.com/questions/28024792/how-to-attach-debugger-to-device-chrome-extention