I am working on a chrome extension that can find a request comes from which iframe. Now I can get a frameId by listen to chrome.webRequest so I can get frameId(see the frameId section in the link, not the id attribute in iframe tag).
Can I use this frameId to find the iframe? What I want is only retrieve the iframe tag's attribute like name, width and height.
Thanks
Here is my working code base on Xan's answer below.
//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);
});
You can use the frame ID to inject a content script into the exact frame (you'll need host permissions, but then again you already have them for webRequest).
From there, you can get width and height as well as name. It just so happens that those particular properties can be accessed from within the frame itself - it's not a general solution to get the <iframe>
element itself.
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
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With