Passing message from extension to web page

2,961 views
Skip to first unread message

Ben McCann

unread,
Nov 21, 2013, 4:39:32 AM11/21/13
to chromium-...@chromium.org
Hi,

What's the best way to pass a message from an extension to a script in a web page?  chrome.runtime.sendMessage only goes in the other direction, so it's nor clear to me how you do this or why the API doesn't support it.

Thanks,
Ben

Adrian Aichner

unread,
Nov 21, 2013, 6:37:14 AM11/21/13
to Ben McCann, Chromium-extensions


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/6c1963b5-376b-4dd4-bd24-9efbd4ca500f%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

Ben McCann

unread,
Nov 21, 2013, 12:11:08 PM11/21/13
to Adrian Aichner, Chromium-extensions
Yes, I've read that doc. Unless I'm missing something, it doesn't answer my question

Joe Marini

unread,
Nov 21, 2013, 1:31:14 PM11/21/13
to Ben McCann, Adrian Aichner, Chromium-extensions
There's also a tabs API for messaging to content scripts in the page. Take a look at chrome.tabs.sendMessage for example.






--
Joe Marini
Developer Advocate / Chrome

Ben McCann

unread,
Nov 21, 2013, 2:03:20 PM11/21/13
to Joe Marini, Adrian Aichner, Chromium-extensions
I'd like to message a script in the page itself though and not a content script. Contents scripts and page scripts execute in different contexts, so if I send it to the content script, I can't easily get it to the page script as far as I know.

Joe Marini

unread,
Nov 21, 2013, 2:05:27 PM11/21/13
to Ben McCann, anaran, Chromium-extensions

You can use tabs.executeScript() to execute script in the page.

Jeffrey Yasskin

unread,
Nov 21, 2013, 2:08:58 PM11/21/13
to Joe Marini, Ben McCann, anaran, Chromium-extensions

johnjbarton

unread,
Nov 21, 2013, 3:25:46 PM11/21/13
to chromium-...@chromium.org, Joe Marini, Adrian Aichner
From the content script use window.postMessage() to send to the page. The page and the content script are in different JS contexts but they see the same DOM and window.postMessage() events.
jjb 


On Thursday, November 21, 2013 11:03:20 AM UTC-8, Ben McCann wrote:
I'd like to message a script in the page itself though and not a content script. Contents scripts and page scripts execute in different contexts, so if I send it to the content script, I can't easily get it to the page script as far as I know.
On Thu, Nov 21, 2013 at 10:31 AM, Joe Marini <joem...@google.com> wrote:
There's also a tabs API for messaging to content scripts in the page. Take a look at chrome.tabs.sendMessage for example.

On Thu, Nov 21, 2013 at 9:11 AM, Ben McCann <b...@benmccann.com> wrote:
Yes, I've read that doc. Unless I'm missing something, it doesn't answer my question
On Thu, Nov 21, 2013 at 3:37 AM, Adrian Aichner <adrian....@gmail.com> wrote:
On Thu, Nov 21, 2013 at 10:39 AM, Ben McCann <benjamin...@gmail.com> wrote:
Hi,

What's the best way to pass a message from an extension to a script in a web page?  chrome.runtime.sendMessage only goes in the other direction, so it's nor clear to me how you do this or why the API doesn't support it.

Thanks,
Ben

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.



--
Joe Marini
Developer Advocate / Chrome

Reply all
Reply to author
Forward
0 new messages