Communicating to a content script that is in an iframe.

3,584 views
Skip to first unread message

Sam

unread,
Jun 3, 2010, 12:09:41 AM6/3/10
to Chromium-extensions
I have a content script that is in an iframe. I know that you can send
a message to a contentscript from the background page like this:

chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"},
function(response) {
console.log(response.farewell);
});
});

chrome.tabs.getSelected gets the tab information of the selected tab.
I then use this tab id to send a request to it's content script.

On the other hand, an iframe isn't a tab, is it? Even if it were a
tab, how do I get the tab id to send a request to it?

Daniel Wagner-Hall

unread,
Jun 3, 2010, 10:59:59 PM6/3/10
to Sam, Chromium-extensions
It will have the same tab details as the page containing the iframe.
Your best bet is to have the content script connect to the background
page using a port connection, rather than a sendRequest, and keep
track of what content scripts have loaded.

> --
> 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.
>
>

Sam

unread,
Jun 4, 2010, 4:18:30 PM6/4/10
to Chromium-extensions
Yea, this is what I figured too. So how do I do this? Should I write
chrome.extensions.connect() in my content script? How does the connect
method work? The documentation on the method is hard for me to
understand and I can't find any examples.

On Jun 3, 7:59 pm, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> It will have the same tab details as the page containing the iframe.
> Your best bet is to have the content script connect to the background
> page using a port connection, rather than a sendRequest, and keep
> track of what content scripts have loaded.
>
> On 3 June 2010 05:09, Sam <sam.lb.hol...@gmail.com> wrote:
>
>
>
> > I have a content script that is in an iframe. I know that you can send
> > a message to a contentscript from the background page like this:
>
> > chrome.tabs.getSelected(null, function(tab) {
> >  chrome.tabs.sendRequest(tab.id, {greeting: "hello"},
> > function(response) {
> >    console.log(response.farewell);
> >  });
> > });
>
> > chrome.tabs.getSelected gets the tab information of the selected tab.
> > I then use this tab id to send a request to it's content script.
>
> > On the other hand, an iframe isn't a tab, is it? Even if it were a
> > tab, how do I get the tab id to send a request to it?
>
> > --
> > You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to chromium-extensions+unsubscr...@chromium.org.

Sam

unread,
Jun 4, 2010, 6:09:14 PM6/4/10
to Chromium-extensions
Okay, I've figured out how the long-lived connections work. It all
works when I have a tab open to the URL with the specific content
script. But, when in the background page, I put an iframe with the URL
and I get this error message:

Unsafe JavaScript attempt to access frame with URL chrome-extension://
dcjnlapaoiolconmenhlbbbcaheehhbk/background.html from frame with URL
http://******.***********.com/********.swf. Domains, protocols and
ports must match.

(I hid the iframe's src URL for privacy purposes.) So why would I be
getting this message when I put an iframe in my background page, but
not when I just open a tab to the URL? As you can see the URL is
pointing to a swf file, so there is no javascript that would be trying
to access the topmost window (the background page), unless a swf file
can do that. But even if it was trying to access window.top, why would
that prevent the content script from running and connecting to the
background page?

I might also point out that I get this error message even when I empty
the content script file. Therefore, the content script isn't causing
this error.

Sam

unread,
Jun 4, 2010, 6:55:54 PM6/4/10
to Chromium-extensions
I think that it's not the error message that's causing my extension
not to work properly. I changed the background page iframe's src URL
to http://www.google.com and didn't get any error messages. However,
the content script still doesn't run within that iframe. I think what
this tells me is: 1) The .swf file was able to TRY to access the
topmost window. 2) The content script is not running within the iframe
that is in the background page.

So, the problem is this: How can I get a content script to run inside
an iframe that is in the background page?

Derek 囧

unread,
Jun 4, 2010, 7:30:02 PM6/4/10
to Chromium-extensions
The parent HTML domain *MUST* be same as the iframe's domain. You can
not change or read data from the iframe if they don't have the same
domain. If you make webpages then you should know that.

Method to communicate between them:
I have a extension that like yours and need to communicate between the
webpage and iframe and I do this:

Simply define a localStorage and make the iframe read the localStorage
per second.
If the script in the iframe detects the localStorage is different than
the one before than it'll do [WHATEVER YOU WANT].

Take a look at mine:
Searching toolbar
https://chrome.google.com/extensions/detail/ajibghijjbbpkccoeabkjhoemiiehnkg

Hope this may help you.


On 6月4日, 下午5時55分, Sam <sam.lb.hol...@gmail.com> wrote:
> I think that it's not the error message that's causing my extension
> not to work properly. I changed the background page iframe's src URL
> tohttp://www.google.comand didn't get any error messages. However,

Sam

unread,
Jun 4, 2010, 7:42:38 PM6/4/10
to Chromium-extensions
Derek, I don't think you understand. I have an iframe within my
background.html extension page. I'm not trying to communicate with it
directly; I'm not trying to access it's DOM from background.html.
Rather, I have a content script that should load into iframe.

I'm trying to communicate between the background and content script
via the chrome.extension.connect() method. I cannot do this because,
for some reason, the content script is not being loaded into the
iframe. When I open the URL directly in a new tab, the content script
is loaded. Why is it that the content script is not being loaded into
the iframe?

On Jun 4, 4:30 pm, Derek 囧 <derek1...@gmail.com> wrote:
> The parent HTML domain *MUST* be same as the iframe's domain. You can
> not change or read data from the iframe if they don't have the same
> domain. If you make webpages then you should know that.
>
> Method to communicate between them:
> I have a extension that like yours and need to communicate between the
> webpage and iframe and I do this:
>
> Simply define a localStorage and make the iframe read the localStorage
> per second.
> If the script in the iframe detects the localStorage is different than
> the one before than it'll do [WHATEVER YOU WANT].
>
> Take a look at mine:
> Searching toolbarhttps://chrome.google.com/extensions/detail/ajibghijjbbpkccoeabkjhoem...
>
> Hope this may help you.
>
> On 6月4日, 下午5時55分, Sam <sam.lb.hol...@gmail.com> wrote:
>
>
>
> > I think that it's not the error message that's causing my extension
> > not to work properly. I changed the background page iframe's src URL
> > tohttp://www.google.comanddidn't get any error messages. However,

PhistucK

unread,
Jun 5, 2010, 4:22:24 AM6/5/10
to Sam, Chromium-extensions
Just for you information, this error message is caused by Flash. Whenever it is loaded in an iFrame, it tries to go to the location.href of the parent document or something, automatically, so it triggers this error message. You should not worry about it.

☆PhistucK


To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Sam

unread,
Jun 5, 2010, 4:25:27 AM6/5/10
to Chromium-extensions
Okay, thanks PhistucK, I didn't know that.

Now the only issue left for me to solve is how do I get my content
script to load in an iframe if the iframe is in the background page?
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .
> > > > > For more options, visit this group athttp://
> > groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .

Sam

unread,
Jun 12, 2010, 5:50:58 AM6/12/10
to Chromium-extensions
I have a content script set for a very specific URL. I placed an
iframe the background page. The content script doesn't load. Is this a
bug?

The content script loads fine when I open the specific URL in a new
tab. I even tried changing the specific URL to http://*.google.com and
I get the same behavior.

Bottom line, content scripts aren't loaded into iframes if the iframe
is within the background page. Why?

On Jun 5, 1:25 am, Sam <sam.lb.hol...@gmail.com> wrote:
> Okay, thanks PhistucK, I didn't know that.
>
> Now the only issue left for me to solve is how do I get my content
> script to load in an iframe if the iframe is in the background page?
>
> On Jun 5, 1:22 am, PhistucK <phist...@gmail.com> wrote:
>
>
>
> > Just for you information, this error message is caused by Flash. Whenever it
> > is loaded in an iFrame, it tries to go to the location.href of the parent
> > document or something, automatically, so it triggers this error message. You
> > should not worry about it.
>
> > ☆PhistucK
>
> > On Sat, Jun 5, 2010 at 01:09,Sam<sam.lb.hol...@gmail.com> wrote:
> > > Okay, I've figured out how the long-lived connections work. It all
> > > works when I have a tab open to the URL with the specific content
> > > script. But, when in the background page, I put an iframe with the URL
> > > and I get this error message:
>
> > > Unsafe JavaScript attempt to access frame with URL chrome-extension://
> > > dcjnlapaoiolconmenhlbbbcaheehhbk/background.html from frame with URL
> > > http://******.***********.com/********.swf. Domains, protocols and
> > > ports must match.
>
> > > (I hid the iframe's src URL for privacy purposes.) So why would I be
> > > getting this message when I put an iframe in my background page, but
> > > not when I just open a tab to the URL? As you can see the URL is
> > > pointing to a swf file, so there is no javascript that would be trying
> > > to access the topmost window (the background page), unless a swf file
> > > can do that. But even if it was trying to access window.top, why would
> > > that prevent the content script from running and connecting to the
> > > background page?
>
> > > I might also point out that I get this error message even when I empty
> > > the content script file. Therefore, the content script isn't causing
> > > this error.
>
> > > On Jun 4, 1:18 pm,Sam<sam.lb.hol...@gmail.com> wrote:
> > > > Yea, this is what I figured too. So how do I do this? Should I write
> > > > chrome.extensions.connect() in my content script? How does the connect
> > > > method work? The documentation on the method is hard for me to
> > > > understand and I can't find any examples.
>
> > > > On Jun 3, 7:59 pm, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
>
> > > > > It will have the same tab details as the page containing the iframe.
> > > > > Your best bet is to have the content script connect to the background
> > > > > page using a port connection, rather than a sendRequest, and keep
> > > > > track of what content scripts have loaded.
>

PhistucK

unread,
Jun 12, 2010, 6:53:53 AM6/12/10
to Sam, Chromium-extensions
Not sure. Will the content script load when it is not in an iFrame?
If so, did you add the "all_frames": true to the content script entry in the manifest?
If so, search crbug.com for an existing issue and star it. If you cannot find one, file a new issue at new.crbug.com.

Thank you.

☆PhistucK


To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
Reply all
Reply to author
Forward
0 new messages