zab*_*tak 5 javascript iframe frame google-chrome-extension content-script
为Chrome扩展程序创建清单文件时,有一个选项all_frames
允许将内容脚本注入顶部框架/ iframe,或者全部注入.
我希望此行为停止在某个特定级别(例如,2).有没有办法指定它,最好是在清单上?或者至少在实际脚本中有黑客攻击?
问题可能被翻译为:"是否可以知道HTML文档中框架的深度?
您无法在清单中指定帧深度.
但是,如果iframe具有不同的URL,则可以针对它们运行不同的内容脚本(通过content_scripts
在清单中的数组中有多个条目).
内容脚本可以使用以下代码以编程方式确定(i)帧的深度:
getFrameDepth (window.self);
function getFrameDepth (winToID) {
if (winToID === window.top) {
return 0;
}
else if (winToID.parent === window.top) {
return 1;
}
return 1 + getFrameDepth (winToID.parent);
}
Run Code Online (Sandbox Code Playgroud)
如果您按如下方式创建扩展,则可以在jsBin上针对此页面对此进行测试:
manifest.json的:
{
"manifest_version": 2,
"content_scripts": [ {
"all_frames": true,
"js": [ "iframe foo.js" ],
"matches": [ "http://jsbin.com/enivux/*",
"http://jsbin.com/anomic/*",
"http://jsbin.com/ogaxoq/*",
"http://jsbin.com/ihegaq/*"
]
} ],
"description": "Detecting where an iframe is in the hierarchy",
"name": "(i)frame Level identification",
"version": " 1"
}
Run Code Online (Sandbox Code Playgroud)
iframe foo.js:
var frameDepth = getFrameDepth (window.self);
var bodyColors = ["white", "pink", "lime", "cyan"]
document.body.style.background = bodyColors[frameDepth];
function getFrameDepth (winToID) {
if (winToID === window.top) {
return 0;
}
else if (winToID.parent === window.top) {
return 1;
}
return 1 + getFrameDepth (winToID.parent);
}
Run Code Online (Sandbox Code Playgroud)
请注意,Chrome扩展程序目前仅在具有src
属性的iframe上运行,并且仅在URL符合清单的匹配和glob要求时才会运行.
归档时间: |
|
查看次数: |
1472 次 |
最近记录: |