我花了很长时间尝试不同的方法来让它工作,但没有任何效果,文档也没有太大帮助。
我试图在动态注入(inject)页面的 iframe 中填充表单。要注入(inject)我做的 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
但要么我不理解它,要么它没有得到正确解释。
任何人都可以解释一下吗?
谢谢!
最佳答案
您是否尝试过使用 contentWindow属性(property)?
var doc = iframe.contentWindow.document;
关于javascript - 从 Firefox 扩展访问 iFrame 的 dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1785185/