0

I am trying to write a Chrome devtools extension for a JS SDK we have developed. This SDK has an API addEventListener (the events are not DOM events) that I would like to use to be able to display all the events that are being published in the devtools panel I made.

Basically I would like to be able to have the following code in my devtools page script :

chrome.devtools.inspectedWindow.eval(
            "mySDKonTheContentPage", function(result, isException){
                mySDK =result;
                mySDK.addEventListener("myEvent", function(){
                   doSomethingInDevtoolsUI();
               });
            });

Since content scripts don't have access (do they?) to the page's JS objects, I don't really know where to start.

psykhi
  • 2,981
  • 2
  • 16
  • 22
  • Not much clear about your question. However according to the last line, if you want to access page javascript variables through content scripts, see http://stackoverflow.com/questions/3955803/page-variables-in-content-script – Haibara Ai Jul 27 '16 at 09:11
  • Thanks! Sorry if the question is unclear. I thought that writing a devtools extension would give the extension all access to what's going on the page, but apparently not... – psykhi Jul 27 '16 at 09:35

1 Answers1

2

In the script on your page, you can use window.postMessage to send your data to the content script. From there you can set up communication between the content script and the DevTools panel via a background page.

See: Messaging from Injected Scripts to the DevTools Page and Messaging from Content Scripts to the DevTools Page for examples of this in the documentation.

Gideon Pyzer
  • 22,610
  • 7
  • 62
  • 68
  • 1
    Thank you! I was afraid there was no direct way of doing this. I'll try with the injected-script => content-script => background => devtool panel way then :p – psykhi Jul 27 '16 at 09:45
  • If it helps anyone, I made a simple github repo implementing this: https://github.com/psykhi/chrome-devtools-extension – psykhi Aug 01 '16 at 13:44
  • @psykhi That's really useful. Thanks for posting this. – Gideon Pyzer Aug 01 '16 at 13:49