gpt4 book ai didi

javascript - 如何在 Chrome 扩展程序中控制注入(inject)内容脚本的 (i)frame 深度?

转载 作者:搜寻专家 更新时间:2023-11-01 04:09:54 26 4
gpt4 key购买 nike

在为 Chrome 扩展程序创建 list 文件时,有一个选项 all_frames 允许将内容脚本注入(inject)到顶部框架/iframe 或全部。

我希望此行为在某个特定级别停止(例如,2)。有没有办法指定它,最好是在 list 上?或者至少在实际脚本中进行黑客攻击?

这个问题可以翻译成:“是否有可能知道框架在 HTML 文档中的深度?

最佳答案

您不能在 list 中指定帧深度。
但是,如果 iframe 具有不同的 URL,您可以针对它们运行不同的内容脚本(通过在 content_scripts 数组中有多个条目,在 list 中)。

内容脚本可以使用如下代码以编程方式确定 (i)frame 的深度:

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);
}



您可以针对 this page at jsBin 进行测试,如果您按如下方式创建扩展:

list .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"
}


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);
}



请注意,Chrome 扩展程序目前将 only run on iframes with a src attribute ,并且仅当 URL 满足 list 的 match 和 glob 要求时。

关于javascript - 如何在 Chrome 扩展程序中控制注入(inject)内容脚本的 (i)frame 深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com