Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome - find iframe by frameId(not id attribute)

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);
                });
like image 753
waitingduck Avatar asked Nov 09 '22 06:11

waitingduck


1 Answers

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
};
like image 179
Xan Avatar answered Nov 15 '22 05:11

Xan