waitingduck waitingduck - 9 months ago 48
Javascript Question

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.


Here is my working code base on Xan's answer below.

(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

chrome.tabs.executeScript(currentTabId, {
allFrames: true,
file: 'frameScript.js'
}, function(data) {

Xan Xan
Answer Source

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.

  {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