从 Firefox 扩展访问 iFrame 的 dom

发布于 2024-08-12 01:11:14 字数 1313 浏览 6 评论 0原文

我花了很长时间尝试不同的方法来使其发挥作用,但没有任何效果,而且文档也没有多大帮助。

我正在尝试在 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

但这也不起作用。

我正在阅读以下文档:

但要么我不理解它,要么它没有得到正确的解释。

任何人都可以透露一些信息吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仲春光 2024-08-19 01:11:14

您是否尝试过使用 contentWindow 属性?

var doc = iframe.contentWindow.document;

Have you tried using the contentWindow property?

var doc = iframe.contentWindow.document;
温柔嚣张 2024-08-19 01:11:14

尝试 myObject.iframe.contentDocument

类似的内容:

// 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) {
                    ...;
                }
            }

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) {
                    ...;
                }
            }
孤云独去闲 2024-08-19 01:11:14

我传入事件对象,然后传入 event.originalTarget (就像您提到的那样),它工作正常。尽管我必须将 addEventListener 的 useCapture 参数设置为 true 才能调用我的事件。

因此,如果我理解正确,您需要在填充方法中访问文档...

我会将 event.originalTarget 作为参数传递给您的方法
或者
在加载事件中,我将设置例如 myObject.document 属性,然后在 populate 方法中使用它。

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

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);
山有枢 2024-08-19 01:11:14

我几乎遇到了同样的问题。使用以下代码获取 iframe(Firebug 用于调试):

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文