简体   繁体   中英

Acess iframe content (in firefox plugin javascript)

I'm writing a firefox plugin who open an iframe in webpages. I want to select object in the iframe, but i can't access content:

var iframe = content.document.getElementById("MyBeautifulIframe");
if (iframe)
{
  var mydiv = iframe.contentDocument.getElementById("mydiv");
}

Erreur : TypeError: iframe.contentDocument is undefined

I tryed with iframe.content.document.getElemen... , iframe.document.getElemen... same result.

How access iframe dom ? If i look iframe var type, it's [object XrayWrapper [object XULElement]] , how access dom objects of XULElement object ?

Updated answer:

Took a look at your code and i think i may have found your problem.

You are doing:

var iframe = content.document.getElementById("muzich_iframe_addcontainer");
if (iframe)
{
  if (iframe.contentDocument.getElementById("div"))
  {
    this.close_all();
  }
}

muzich_iframe_addcontainer is a div, not the iframe, so it never has contentDocument. Additionally, i couldn't make it work by creating xul elements. I had to create html div and iframe to make it work.

Here's the code:

var htmlns = "http://www.w3.org/1999/xhtml";
var container = document.createElementNS(htmlns,'div');
container.setAttribute("id", "muzich_iframe_addcontainer");
container.setAttribute("style", styleContainer); //styleContainer is the one you use without the background stuff
window.content.document.body.appendChild(container);

var iframe = document.createElementNS(htmlns,'iframe');
iframe.setAttribute("id", "muzich_iframe");
iframe.setAttribute("style",
    "width: 100%; height: 100%; "
);

iframe.setAttribute("src", "http://www.lifehacker.com"); // Used it as my example url
window.content.document.getElementById("muzich_iframe_addcontainer").appendChild(iframe);

Then, when you want to check to close you do:

var iframe = window.content.document.getElementById("muzich_iframe");
if (iframe)
{
  if (iframe.contentDocument.getElementById("div"))
  {
    this.close_all();
  }
}

Hope this one solves it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM