2

I am developing a chrome extension (content script) to access web page content. I use the DOM to access all elements in page. My code does not work correctly when the web page contains "frameset". In this matter I can count the frame number but I can't access to the frame content. I use this code to count frame objects available on current page :

for frameset :

parent.frames.length

and for iframe :

document.getElementsByTagName("iframe").length

but the following code does not work :

parent.frames[0]

and returns "undefined".

My question: How can I access to frame set elements inside a chrome extension (for both iframe and frameset)?

mobile
  • 21
  • 2

2 Answers2

1

Similar to how Jerinaw approached it, I did the following via jQuery:

// Find the frame
var frame = $(document).find("frame[name='nameOfFrame']")[0];
// Callback once frame's internals are loaded
$(frame).load(function() {
  accessFrame();
});

function accessFrame() {
  // The following line is really important to accessing the frame's content.
  var frameContent = frame.contentDocument;
  // Notice that $(".objectSelectorClass") won't discover the object you're hunting for.  You have to find it within the frame's contentDocument.
  var objectInFrame = $(frameContent).find(".objectSelectorClass");
  var objectInFrame_AlternateSyntax = $(".objectSelectorClass", frameContent)
  // do stuff to objectInFrame
}
Xan
  • 74,770
  • 16
  • 179
  • 206
HeyZiko
  • 1,660
  • 15
  • 28
0

I'm running into the same problem. The frames are not iframes and they are in a frameset. What I had to do was an onload (for some reason addEventListener isn't working) to the frame but through it's contentWindow

for example: From the main document

document.querySelector('#SomeFrameSelector').contentWindow.document.body.onload

Accessing the frame this way got me to the frames document and allowed me to attach the onload. Once fired using the same selector I was able to modify the frames content.

Jerinaw
  • 5,260
  • 7
  • 41
  • 54
  • I did essentially the same thing, but via jquery: $(document).find("frame[name='frameName']")[0].load – HeyZiko Feb 06 '16 at 05:32