在动态创建的iframe中注入内容脚本

时间:2014-05-29 17:40:13

标签: javascript iframe google-chrome-extension content-script

我有一个chrome扩展程序,内容脚本1以编程方式在当前页面上创建iframe(不是同一个域iframe)。我想在这个新的iframe中运行我的chrome扩展程序的内容脚本2(例如,当用户点击iframe中的内容时,会将消息发送到后台脚本)。

在我的manifest.json中,我在内容脚本部分添加了:

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

我认为放置all_frames = true属性就足够了,但是当我使用扩展时,ContentScript2.js会加载到页面加载时加载的每个其他iframe中,但不会加载到动态创建的iframe中。

有人有解决方案吗?

更多信息: 这是我在ContentScript1中用来创建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版本为35.0.1916.114

0 个答案:

没有答案
相关问题