how can i sendMessage from background.js to devtools.panel's page

1,976 views
Skip to first unread message

zheng...@gmail.com

unread,
Sep 23, 2012, 5:36:37 AM9/23/12
to google-chrome-...@googlegroups.com
in my extension , which have :

{
   "manifest_version" : 2,
  .....
   "background":{
    "scripts":["background.js"]
   },
  "devtools_page": "devtools.html"
  .....
}

in background.js
==================
chrome.webRequest.onBeforeRequest.addListener(function(req){
    chrome.extension.sendMessage({test:123});
});
==================

in devtools.html
==================
chrome.extension.onMessage.addListener(function(Detail){

});

==================


in chrome21 ,  devtools.html page can’t receive the message from background.js
and background.js get an error message : {"message":"Could not establish connection. Receiving end does not exist."}

is anything wrong here?

Andrey Kosyakov

unread,
Sep 26, 2012, 10:50:31 AM9/26/12
to zheng...@gmail.com, google-chrome-...@googlegroups.com
On Sun, Sep 23, 2012 at 1:36 PM, <zheng...@gmail.com> wrote:
 
in background.js
==================
chrome.webRequest.onBeforeRequest.addListener(function(req){
    chrome.extension.sendMessage({test:123});
});

[...] 

in chrome21 ,  devtools.html page can’t receive the message from background.js
and background.js get an error message : {"message":"Could not establish connection. Receiving end does not exist."}

is anything wrong here?

You can't use chrome.extension.sendMessage() to send a message to an extension page running in the context of DevTools (this call is for sending to listeners running in the extension process, i.e. background page, options page etc). The best way to communicate from background page to DevTools is to open a port from the DevTools page using chrome.extension.connect (and a chrome.extension.onConnect listener in the background page) and use port.postMessage() in the background page later.

Best regards,
Andrey.

Reply all
Reply to author
Forward
0 new messages