Question about chrome.extension.connect()

3,186 views
Skip to first unread message

Robert Dailey

unread,
May 15, 2011, 9:11:00 PM5/15/11
to Chromium-extensions
Will connections work from scripts that are not content scripts? For example, in my background.html page, I have a script included... common.js. From common.js, I would like to use chrome.extension.connect() to send messages to the script embedded in background.html. Inside of background.html I do this (within script tags):

chrome.extension.onConnect.addListener( function( port )
{
if( port.name == 'action_channel' ) {
port.onMessage.addListener( onRequest );
}
});

If I call chrome.extension.connect right below that, my onConnect callback is not triggered. Nor is it triggered if I connect from common.js. What am I doing wrong?

---------
Robert Dailey

Boris Smus

unread,
May 16, 2011, 3:05:20 PM5/16/11
to Robert Dailey, Chromium-extensions
If you have a chrome.extension.onConnect listener in your background page, You should be running chrome.extension.connect from the content script. There's a sample available that does what you're trying to do: http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/extensions/email_this_page/

- Boris

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

Robert Dailey

unread,
May 16, 2011, 4:58:08 PM5/16/11
to Boris Smus, Chromium-extensions
I'm trying to do my chrome.extension.connect() from a script that is NOT a content script. I was hoping it could be used when my extension has no content pages at all.

Can this be done?

---------
Robert Dailey

Robert Dailey

unread,
May 16, 2011, 8:07:09 PM5/16/11
to Boris Smus, Chromium-extensions
Could I please get a response on this? I'm roadblocked until I can figure out if I need to fix a bug in what I'm doing or possibly find a different approach.

---------
Robert Dailey

Ben

unread,
May 16, 2011, 8:21:05 PM5/16/11
to Robert Dailey, Boris Smus, Chromium-extensions
As far as I know, it's not possible. You can inject a script into a
page, using methods that I haven't explored[0], and may be able to use
connect() from there...but any script running within the context of the
page will not have access to any chrome extension APIs.

[0]http://code.google.com/chrome/extensions/content_scripts.html#pi

Abraham Williams

unread,
May 16, 2011, 8:28:07 PM5/16/11
to Robert Dailey, Chromium-extensions
With the assumption that you you are trying to use chrome.extension.connect from a script loaded in your background.html to trigger an event in your background.html that would typically be triggered by a message from a content_script. You should just call the function directly instead of trying to use chrome.extension.connect. All of the scripts loaded in the background.html are in the same sandbox environment.

Abraham
-------------
Abraham Williams | InboxQ | abrah.am
@abraham | github.com/abraham | blog.abrah.am
This email is: [ ] shareable [x] ask first [ ] private.



--
Reply all
Reply to author
Forward
0 new messages