从 Firefox 扩展访问 iFrame 的 dom
我花了很长时间尝试不同的方法来使其发挥作用,但没有任何效果,而且文档也没有多大帮助。
我正在尝试在 iframe 内填充表单,并将其动态注入到页面中。为了注入该 iframe,我这样做:
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);
它工作正常并且确实触发了 populate() 方法。我的问题是获取该 iframe 的文档或窗口对象。我已经尝试了以下所有内容:
myObject.iframe.window
myObject.iframe.document
myObject.iframe.content
但这些都是未定义的。
我还尝试将事件对象传递给 iframe 的加载事件侦听器,然后执行以下操作:
event.originalTarget
但这也不起作用。
我正在阅读以下文档:
- https://developer.mozilla.org/En/Working_with_windows_in_chrome_code
- https://developer.mozilla.org/en/Code_snippets/Interaction_ Between_privileged_and_non-privileged_pages
但要么我不理解它,要么它没有得到正确的解释。
任何人都可以透露一些信息吗?
谢谢!
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:
- https://developer.mozilla.org/En/Working_with_windows_in_chrome_code
- https://developer.mozilla.org/en/Code_snippets/Interaction_between_privileged_and_non-privileged_pages
But either I'm not understanding it or it's just not properly explained.
Can anyone shed some light?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过使用 contentWindow 属性?
Have you tried using the contentWindow property?
尝试
myObject.iframe.contentDocument
类似的内容:
Try
myObject.iframe.contentDocument
Something along the lines of this:
我传入事件对象,然后传入 event.originalTarget (就像您提到的那样),它工作正常。尽管我必须将 addEventListener 的 useCapture 参数设置为 true 才能调用我的事件。
因此,如果我理解正确,您需要在填充方法中访问文档...
我会将
event.originalTarget
作为参数传递给您的方法或者
在加载事件中,我将设置例如
myObject.document
属性,然后在 populate 方法中使用它。I'm passing in the event object and then
event.originalTarget
(like you mentioned) and it's working fine. Although I had to setuseCapture
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 methodor
in load event I would set for example
myObject.document
property and then use it in populate method.我几乎遇到了同样的问题。使用以下代码获取 iframe(Firebug 用于调试):
I almost had the same problem. Using the following code gets me the iframe (Firebug is used for debugging):