我是一个铬扩展开发人员,我的扩展中有一个问题:当我创建一个元素
时child = document.createElement('div');
并将此元素添加到页面主体:
window.document.body.appendChild(child);
对于包含多个iFrame的页面,该孩子出现在页面中的每个iframe中。
我如何强制此元素仅出现在主窗口中,而不是在所有iframes上?
谢谢。
您可以将" all_frames"值转到清单中的false and Check(这意味着只匹配顶部框架。)
"content_scripts": [
{
.....
"all_frames":false
.....
}
],
参考:
表明内容脚本
编辑1:
将您的内容脚本代码分为两个部分
"content_scripts": [
{
"matches": [".."],
"all_frames":true,
"js": ["myscript_iframes.js"]
},
{
"matches": [".."],
"all_frames":false,
"js": ["myscript_noIframes.js"]
}
],
myscript_iframes.js
有代码,您可以在其中读取所有框架中的内容,而myscript_noIframes.js
的代码在其中附加到主框架上。