4

I have a chrome extension, with content script 1 which programmatically creates an iframe (not same domain iframe) on the current page. I want to run a content script 2 of my chrome extension inside this new iframe (e.g. when a user clicks on something inside the iframe, a message is sent to the background script).

In my manifest.json, I added in the content scripts section:

{
    "matches": ["http://url_of_my_new_iframe/*"],
    "css": [],
    "js": ["ContentScript2.js"],
    "run_at": "document_idle",
    "all_frames": true
}

I thought putting the all_frames=true property would be sufficient, but when I use the extension, the ContentScript2.js is loaded in every other iframes loaded at page load, but NOT in my dynamically created iframe.

Does anybody have a solution for this?

More info: This is the code I use in ContentScript1 to create the iframe:

var iFrame = document.createElement('iframe');
iFrame.setAttribute('id', 'some_id');
document.body.insertBefore(iFrame, some_other_dom_element);
iFrame.setAttribute('src', 'my_iframe_url_not_same_domain');

Chrome Version is 35.0.1916.114

jeanpaul62
  • 9,451
  • 13
  • 54
  • 94
  • I'm not sure. Do you want to interact the frame with the parent window? – h_s May 29 '14 at 18:02
  • Could you edit the question and include the code used to create the iframe? And which Chrome version are you using? – Rob W May 29 '14 at 18:06
  • @h_s well actually not with the parent window, but ideally with the background script. For example, when the user clicks on a button in the new iFrame, the background script opens a new tab, or something like that. – jeanpaul62 May 29 '14 at 18:17
  • 1
    @ManiaArmyYurt Are you sure that the URL is matched by `matches`? Is it possible that the URL is redirected to a different domain, e.g. from `http://example.com/` to `http://www.example.com/`? – Rob W May 29 '14 at 19:16
  • @RobW I'm pretty sure yes, actually I even tried putting `"matches": ["http://*/*"]` but same result, i.e. the content script 2 ran inside all the other frames except the dynamically generated one. – jeanpaul62 May 29 '14 at 19:17
  • 1
    @ManiaArmyYurt If you change the manifest file, then you need to reload the extension before seeing the changes. If this does not help, could you mail me a real example to reproduce the error? The code in your question works fine in my browser. – Rob W May 29 '14 at 19:19
  • @ManiaArmyYurt, see this: http://www.onlineaspect.com/2010/01/15/backwards-compatible-postmessage/ You can send a message for the background window. Sorry for delay, you solve the problem? – h_s Jun 06 '14 at 17:51
  • @h_s: yes, I used post messages to solve my problem! – jeanpaul62 Jun 06 '14 at 18:24

0 Answers0