我正在开发一个 chrome 扩展程序,它可以找到来自哪个 iframe 的请求。现在我可以通过监听 chrome.webRequest 得到一个 frameId所以我可以获得 frameId(请参阅链接中的 frameId 部分,而不是 iframe 标记中的 id 属性)。
我可以使用这个 frameId 找到 iframe 吗?我想要的只是检索 iframe 标签的属性,如名称、宽度和高度。
谢谢
这是我基于 Xan 的工作代码下面的答案。
//frameScript.js
(function() {
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
var width = Math.max( body.scrollWidth, body.offsetWidth,
html.clientHeight, html.scrollWidth, html.offsetWidth );
return {
width: width,
height: height
}
}());
//background.js
chrome.tabs.executeScript(currentTabId, {
allFrames: true,
file: 'frameScript.js'
}, function(data) {
console.log(data);
});
最佳答案
您可以使用框架 ID 将内容脚本注入(inject)到确切的框架中(您需要主机权限,但您已经拥有用于 webRequest 的权限)。
从那里,您可以获得宽度和高度以及 name .碰巧可以从框架本身访问这些特定属性——这不是获取 <iframe>
的通用解决方案。元素本身。
chrome.tabs.executeScript(
tabId,
{frameId: frameId, file: "content.js"},
function(data) { console.log(data[0]); }
);
// content.js
// Evaluate something to return it
{
name: window.name,
height: window.innerHeight
width: window.innerWidth
};
关于javascript - Chrome - 通过 frameId(不是 id 属性)查找 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409010/