psykhi
psykhi

Reputation: 2981

How to execute a callback on the devtools page from page's JS?

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.

Upvotes: 0

Views: 898

Answers (1)

Gideon Pyzer
Gideon Pyzer

Reputation: 24018

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.

Upvotes: 2

Related Questions