我没有在Firefox附加组件SDK(Tab API)中找到任何可以销毁选项卡或用新选项卡替换它的内容。我可以找到选项卡ID,选项卡URL或其他标识符。有没有办法替换或销毁标签?
答案 0 :(得分:3)
替换是什么意思?如果您只想更改网址,请使用tab.url = url
。
答案 1 :(得分:2)
我不了解SDK,但您可以使用另一个标签销毁并替换一个标签。
这是如何使用非sdk:
var tabIdToClose = 'panel-3-1';
Cu.import('resource://gre/modules/Services.jsm');
var DOMWindows = Services.wm.getEnumerator('navigator:browser');
while (DOMWindows.hasMoreElements()) {
var aDOMWindow = DOMWindows.getNext();
if (aDOMWindow.gBrowser && aDOMWindow.gBrowser.tabContainer) {
var tabs = aDOMWindow.gBrowser.tabContainer.childNodes;
for (var i = 0; i < tabs.length; i++) {
var aTab = tabs[i];
var aTabId = aTab.getAttribute('linkedpanel');
console.log('tab id:', aTabId);
if (aTabId == tabIdToClose) {
aDOMWindow.gBrowser.removeTab(aTab);
}
}
} else {
//this window does not have any tabs
}
}
var tabIdToReplace = 'panel-3-248';
var tabIdToReplaceWith = 'panel-3-246'; //this tab will be closed, but its contents will be in tabIdToReplace
var aTabToReplace;
var aTabToReplaceWith;
Cu.import('resource://gre/modules/Services.jsm');
var DOMWindows = Services.wm.getEnumerator('navigator:browser');
while (DOMWindows.hasMoreElements()) {
var aDOMWindow = DOMWindows.getNext();
if (aDOMWindow.gBrowser && aDOMWindow.gBrowser.tabContainer) {
var tabs = aDOMWindow.gBrowser.tabContainer.childNodes;
for (var i = 0; i < tabs.length; i++) {
var aTab = tabs[i];
var aTabId = aTab.getAttribute('linkedpanel');
console.log('tab id:', aTabId);
if (aTabId == tabIdToReplace) {
aTabToReplace = aTab;
} else if (aTabId == tabIdToReplaceWith) {
aTabToReplaceWith = aTab;
}
if (aTabToReplace && aTabToReplaceWith) {
aTabToReplaceWith.ownerDocument.defaultView.gBrowser.swapBrowsersAndCloseOther(aTabToReplace, aTabToReplaceWith);
break;
}
}
} else {
//this window does not have any tabs
}
}
我还没有完成这个,所以交换并不完美,就像我必须设置iconURL和标签的标题一样:https://gist.github.com/Noitidart/9b335a460f53b0390336