Injecting content script in dynamically created iframe

倾然丶 夕夏残阳落幕 提交于 2019-12-07 19:30:22

问题


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

来源:https://stackoverflow.com/questions/23939547/injecting-content-script-in-dynamically-created-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!