如何从firefox插件访问选项卡的内容

时间:2014-03-05 07:57:26

标签: javascript html iframe tabs firefox-addon

在我的插件中,我找到了我想要操作的标签,然后尝试访问它的元素。 目前我正在找到我需要的标签

    var b = this.wm.getMostRecentWindow("navigator:browser");

    // qqDPSWD This allows for correct window targeting.

    var foundW = null;
    var en = this.wm.getEnumerator("navigator:browser");
    while (en.hasMoreElements()) {
      var w = en.getNext();
      if ((w.title && w.title.indexOf(parameters['title_identifier']) != -1) ||
          (w.document && w.document.title.indexOf(parameters['title_identifier']) != -1))
      {
        var doc = w.document;
        var temp2 = doc.getElementById("myframe");
        foundW = temp2.contentWindow;
      }
    }

temp2为null虽然该标签确实有一个id为myframe的iframe。

我将对象doc作为XUL对象,但doc.getElementById(“myframe”)为null。目前我在所需的选项卡中打开了一个html文件,其中所需的iframe位于主选项卡中加载的html页面内。我能够正确识别选项卡,但无法返回iframe窗口。我该怎么做?

我尝试查看文档以便在标签之间进行浏览,但在https://developer.mozilla.org/en/docs/Working_with_windows_in_chrome_code

中找不到正确的答案

节点我正在处理https://github.com/sebuilder/se-builder/blob/master/seleniumbuilder/components/command_processor.js#L10103并希望替换

    foundW = w;

    foundW = w.document.getElementById("myframe").contentWindow

与他想要返回标签窗口的开源项目不同,我想返回他返回的标签内的iframe窗口。

1 个答案:

答案 0 :(得分:2)

您实际上并没有浏览所有标签,只是浏览FIREFOX窗口(称为CHROME窗口)(而不是每个标签内的浏览器及其窗口)。

在你的代码中。 var doc = w.document是FIREFOX窗口的CHROME文档(不是选项卡内的浏览器)。所以FIREFOX窗口的w.title将是当前所选标签的标题(可能后跟' - Mozilla Fireox'你可以为我验证这个吗?我在这里猜测)

temp2为null,因为您的框架位于BLWSER IN TAB窗口中,即HTML文档。因此,如果您的标签当前被选中,您可以像w.gBrowser.selectedTab.linkedBrowser.contentwindow这样获得它,这将是html窗口。 w.selectedTab是您在顶部单击的实际制表符元素,它有一个名为linkedBrowser的属性,其中包含此选项卡内的“HTML”浏览器。 (我把html

以便修复下面的代码:

var b = this.wm.getMostRecentWindow("navigator:browser");

// qqDPSWD This allows for correct window targeting.

var foundW = null;
var en = this.wm.getEnumerator("navigator:browser");
while (en.hasMoreElements()) {
  var w = en.getNext();
  if ((w.title && w.title.indexOf(parameters['title_identifier']) != -1) ||
      (w.document && w.document.title.indexOf(parameters['title_identifier']) != -1))
  {
    var doc = w.gBrowser.selectedTab.linkedBrowser.contentDocument;
    var temp2 = doc.getElementById("myframe");
    foundW = doc.defaultView; //im not sure what you want foundW to be, the chrome window? or the tab html window? if you want html window or you can do doc.defaultView OR w.gBrowser.selectedTab.linkedBrowser.contentWindow BUT if you want the chrome window it would be w
  }
}

但是你的代码有问题,它不会遍历每个窗口中的所有选项卡,只会通过当前选中的选项卡。

这就是你如何为每个窗口中的每个标签做这些,仔细阅读评论,我也拿出你丑陋的if语句lol它让事情变得草率。只需将其替换为/*your if statement*/,以便我在下面做出示例

var b = this.wm.getMostRecentWindow("navigator:browser");

 // qqDPSWD This allows for correct window targeting.

var foundW = null;
var en = this.wm.getEnumerator("navigator:browser");
while (en.hasMoreElements()) {
    var w = en.getNext();
    //we know for sure that all your windows have gBrowser element because you are getting enumerator for 'navigator:browser', but its not necessary for it to have tabContainer, for example a pop up window with no tabs in it
    if (w.gBrowser.tabContainer) {
        for (var i = 0; i < w.gBrowser.tabContainer.childNodes.length; i++) { //this itereates through each tab element in the tab bar (so the thingies you click)
            var tab = w.gBrowser.tabContainer.childNodes[i];
            var tabBrowser = tab.linkedBrowser;
            var tabDoc = tabBrowser.contentDocument;
            var tabWin = tabDoc.defaultView; //OR you can do tabBrowser.contentWindow
            if ( /*if statement here*/ ) {

                var temp2 = tabDoc.getElementById("myframe");
                foundW = tabWin; //im not sure what you want here so i set it to the html window

                w.focus(); //if you want to focus this FIREFOX window which is chrome window do this:
                w.gBrowser.selectedTab = tab[i]; //if you want to select this tab then do this

            }
        }
    } else {
        //it has no tabContainer so its like a popup window with no tabs so our browser elment is just gBrowser, ill use same var names as above to keep things straight for you
        var tabBrowser = w.gBrowser;
        var tabDoc = tabBrowser.contentDocument;
        var tabWin = tabDoc.defaultView; //OR you can do tabBrowser.contentWindow
        if ( /*if statement here*/ ) {
            var temp2 = tabDoc.getElementById("myframe");
            foundW = tabWin; //im not sure what you want here so i set it to the html window

            w.focus(); //if you want to focus this FIREFOX window which is chrome window do this:
            //w.gBrowser.selectedTab = tab[i]; //no tabs in this window so if you do w.focus() on line above it will focus this properly
        }
    }
}