在我的插件中,我找到了我想要操作的选项卡,然后尝试访问它的元素。 目前我正在通过
找到我需要的选项卡 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;
}
}
虽然该选项卡确实有一个 ID 为 myframe 的 iframe,但 temp2 为 null。
我将对象 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 窗口。
最佳答案
您实际上并没有浏览所有选项卡,您只是浏览 FIREFOX 窗口(称为 CHROME 窗口)(而不是每个选项卡内的浏览器及其窗口)。
在你的代码中。 var doc = w.document
是 FIREFOX 窗口(不是选项卡内的浏览器)的 CHROME 文档。所以w.title
FIREFOX 窗口的标题将是当前所选选项卡的标题(可能后面跟着“- Mozilla Fireox”,您可以帮我验证一下吗?我猜)
temp2 为空,因为您的框架位于 BROWSER 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 语句,哈哈,这让事情变得很草率。把它放回去,我替换为 /*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
}
}
}
关于javascript - 如何从 Firefox 插件访问选项卡的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22191798/