firefox extension, jquery and accessing the docume

2019-07-31 01:08发布

I can easily traverse the current window while I'm inside the pageLoad function in my extension using content.document, but when I try accessing it in another function, called with setInterval, I can't access the content.document.

I tried content.document, document.defaultView, window.content.document and basically every other sensible combination I could think of.

please help. thanks.

2条回答
Melony?
2楼-- · 2019-07-31 01:45

I'm not sure why that wouldn't' work (I'd have to see more code). However, an alternative would be to use gBrowser.selectedBrowser.contentDocument.

查看更多
We Are One
3楼-- · 2019-07-31 01:46

I got this to work by getting the document object out of an event like so:

   e.target.defaultView.document

The event object is available in any event listener function - for e.g. you may be capturing the document loaded event by doing something like this:

   var appcontent = window.document.getElementById('appcontent');
   appcontent.addEventListener('DOMContentLoaded', onPageLoad, false);

In this example, the onPageLoad function gets an event argument that can be used to fetch the document.

To be able to access the document object elsewhere, set a global variable pointing to that. And if using jQuery, pass this as the optional second argument to the $ function (i.e. the context on which jQuery works).

查看更多
登录 后发表回答