简体   繁体   English

访问iframe内容(在firefox插件javascript中)

[英]Acess iframe content (in firefox plugin javascript)

I'm writing a firefox plugin who open an iframe in webpages. 我正在编写一个firefox插件,该插件可以在网页中打开iframe。 I want to select object in the iframe, but i can't access content: 我想在iframe中选择对象,但无法访问内容:

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

Erreur : TypeError: iframe.contentDocument is undefined Erreur:TypeError:iframe.contentDocument未定义

I tryed with iframe.content.document.getElemen... , iframe.document.getElemen... same result. 我尝试使用iframe.content.document.getElemen ...,iframe.document.getElemen ...相同的结果。

How access iframe dom ? 如何访问iframe dom? If i look iframe var type, it's [object XrayWrapper [object XULElement]] , how access dom objects of XULElement object ? 如果我看iframe var类型,它是[object XrayWrapper [object XULElement]] ,如何访问XULElement对象的dom对象?

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. muzich_iframe_addcontainer是div,而不是iframe,因此它永远不会具有contentDocument。 Additionally, i couldn't make it work by creating xul elements. 另外,我无法通过创建xul元素使其工作。 I had to create html div and iframe to make it work. 我必须创建html div和iframe才能使其正常工作。

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. 希望这个可以为您解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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