English 中文(简体)
Accessing an iFrame s dom from a Firefox Extension
原标题:

I ve spent a long time trying different things to get this to work but nothing does and documentation is not helping much.

I m trying to populate a form inside an iframe that I dynamically inject into a page. To inject that iframe I do:

myObject.iframe = document.createElement("iframe");
myObject.iframe.setAttribute("src", data.url);
myObject.iframe.setAttribute("id","extension-iframe");
myObject.window.document.getElementById( publisher ).appendChild(myObject.iframe);
myObject.iframe.addEventListener("load", function(){
    myObject.populate(data);
}, false);

which works fine and DOES trigger the populate() method. My problem is getting the document or window objects for that iframe. I ve tried all of the following:

myObject.iframe.window
myObject.iframe.document
myObject.iframe.content

but these are all undefined.

I also tried passing the event object to that iframe s load event listener and then doing:

event.originalTarget

But that didn t work either.

I am reading the following documentation:

But either I m not understanding it or it s just not properly explained.

Can anyone shed some light?

Thanks!

最佳回答

Try myObject.iframe.contentDocument

Something along the lines of this:

// For accessing browser window from sidebar code.
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);

var gBrowser = mainWindow.gBrowser;

var iframes = gBrowser.contentDocument.getElementsByTagName("iframe");
            for (var i=0; i < iframes.length; i++) {
                var check_elem = iframes[i].contentDocument.getElementById( myid );
                if (check_elem) {
                    ...;
                }
            }
问题回答

Have you tried using the contentWindow property?

var doc = iframe.contentWindow.document;

I m passing in the event object and then event.originalTarget (like you mentioned) and it s working fine. Although I had to set useCapture parameter of addEventListener to true in order to get my event called.

So if I understand correctly you need to access document in populate method...

I would pass event.originalTarget as a parameter to your method or in load event I would set for example myObject.document property and then use it in populate method.

myObject.iframe.addEventListener("load", function(event){
    var doc = event.originalTarget;
    myObject.populate(data,doc);
}, false);

I almost had the same problem. Using the following code gets me the iframe (Firebug is used for debugging):

iframes = window.content.document.getElementsByTagName( iframe )
for (var i = 0; i < iframes.length; i++) {
  var elmInput = iframes[i];
  Firebug.Console.log(elmInput);
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签