Browse Source

Firefox: determine tabIds via <browser>

pull/2/head
Deathamns 10 years ago
committed by gorhill
parent
commit
1629accdb8
  1. 118
      platform/firefox/vapi-background.js

118
platform/firefox/vapi-background.js

@ -20,7 +20,7 @@
*/ */
/* jshint esnext: true, bitwise: false */ /* jshint esnext: true, bitwise: false */
/* global self, Components, punycode */
/* global self, Components, punycode, µBlock */
// For background page // For background page
@ -451,45 +451,30 @@ vAPI.tabs.registerListeners = function() {
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.stack = new WeakMap();
vAPI.tabs.stackId = 1;
/******************************************************************************/
vAPI.tabs.getTabId = function(target) { vAPI.tabs.getTabId = function(target) {
if ( vAPI.fennec ) { if ( vAPI.fennec ) {
if ( target.browser ) { if ( target.browser ) {
// target is a tab // target is a tab
target = target.browser; target = target.browser;
} }
return target.loadContext.DOMWindowID;
}
if ( target.linkedPanel ) {
} else if ( target.linkedPanel ) {
// target is a tab // target is a tab
return target.linkedPanel;
}
// target is a browser
var i;
var gBrowser = getOwnerWindow(target).gBrowser;
if ( !gBrowser ) {
return -1;
}
// This should be more efficient from version 35
if ( gBrowser.getTabForBrowser ) {
i = gBrowser.getTabForBrowser(target);
return i ? i.linkedPanel : -1;
target = target.linkedBrowser;
} }
if ( !gBrowser.browsers ) {
return -1;
if ( target.localName !== 'browser' ) {
return vAPI.noTabId;
} }
i = gBrowser.browsers.indexOf(target);
if ( i !== -1 ) {
i = gBrowser.tabs[i].linkedPanel;
var tabId = this.stack.get(target);
if ( !tabId ) {
tabId = '' + this.stackId++;
this.stack.set(target, tabId);
} }
return i;
return tabId;
}; };
/******************************************************************************/ /******************************************************************************/
@ -497,51 +482,41 @@ vAPI.tabs.getTabId = function(target) {
// If tabIds is an array, then an array of tabs will be returned, // If tabIds is an array, then an array of tabs will be returned,
// otherwise a single tab // otherwise a single tab
vAPI.tabs.getTabsForIds = function(tabIds, tabBrowser) {
var tabId;
vAPI.tabs.getTabsForIds = function(tabIds) {
var tabs = []; var tabs = [];
var singleTab = !Array.isArray(tabIds); var singleTab = !Array.isArray(tabIds);
if ( singleTab ) { if ( singleTab ) {
tabIds = [tabIds]; tabIds = [tabIds];
} }
if ( vAPI.fennec ) {
for ( tabId of tabIds ) {
var tab = tabBrowser.tabs.find(tab=>tab.browser.loadContext.DOMWindowID === Number(tabId));
if ( tab ) {
tabs.push(tab);
}
for ( var tab of this.getAll() ) {
var browser = getBrowserForTab(tab);
var tabId = this.stack.get(browser);
if ( !tabId ) {
continue;
} }
} else {
var query = [];
for ( tabId of tabIds ) {
query.push('tab[linkedpanel="' + tabId + '"]');
if ( tabIds.indexOf(tabId) !== -1 ) {
tabs.push(tab);
}
if ( tabs.length >= tabIds.length ) {
break;
} }
query = query.join(',');
tabs = [].slice.call(tabBrowser.tabContainer.querySelectorAll(query));
} }
return singleTab ? tabs[0] || null : tabs; return singleTab ? tabs[0] || null : tabs;
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.get = function(tabId, callback) { vAPI.tabs.get = function(tabId, callback) {
var tab, windows, win;
var tab, win;
if ( tabId === null ) { if ( tabId === null ) {
win = Services.wm.getMostRecentWindow('navigator:browser'); win = Services.wm.getMostRecentWindow('navigator:browser');
tab = getTabBrowser(win).selectedTab; tab = getTabBrowser(win).selectedTab;
tabId = this.getTabId(tab); tabId = this.getTabId(tab);
} else { } else {
windows = this.getWindows();
for ( win of windows ) {
tab = vAPI.tabs.getTabsForIds(tabId, getTabBrowser(win));
if ( tab ) {
break;
}
tab = this.getTabsForIds(tabId);
if ( tab ) {
win = getOwnerWindow(tab);
} }
} }
@ -555,10 +530,7 @@ vAPI.tabs.get = function(tabId, callback) {
return; return;
} }
if ( !windows ) {
windows = this.getWindows();
}
var windows = this.getWindows();
var browser = getBrowserForTab(tab); var browser = getBrowserForTab(tab);
var tabBrowser = getTabBrowser(win); var tabBrowser = getTabBrowser(win);
var tabIndex, tabTitle; var tabIndex, tabTitle;
@ -662,12 +634,10 @@ vAPI.tabs.open = function(details) {
} }
if ( details.tabId ) { if ( details.tabId ) {
for ( win in this.getWindows() ) {
tab = this.getTabsForIds(details.tabId, win);
if ( tab ) {
getBrowserForTab(tab).loadURI(details.url);
return;
}
tab = this.getTabsForIds(details.tabId);
if ( tab ) {
getBrowserForTab(tab).loadURI(details.url);
return;
} }
} }
@ -707,16 +677,12 @@ vAPI.tabs.remove = function(tabIds) {
if ( !Array.isArray(tabIds) ) { if ( !Array.isArray(tabIds) ) {
tabIds = [tabIds]; tabIds = [tabIds];
} }
for ( var win of this.getWindows() ) {
var tabBrowser = getTabBrowser(win);
var tabs = this.getTabsForIds(tabIds, tabBrowser);
if ( !tabs ) {
continue;
}
for ( var tab of tabs ) {
this._remove(tab, tabBrowser);
}
var tabs = this.getTabsForIds(tabIds);
if ( tabs.length === 0 ) {
return;
}
for ( var tab of tabs ) {
this._remove(tab, getTabBrowser(getOwnerWindow(tab)));
} }
}; };
@ -1311,7 +1277,7 @@ vAPI.net.registerListeners = function() {
// Popup candidate // Popup candidate
if ( details.openerURL ) { if ( details.openerURL ) {
for ( var tab of vAPI.tabs.getAll() ) { for ( var tab of vAPI.tabs.getAll() ) {
var URI = tab.linkedBrowser.currentURI;
var URI = getBrowserForTab(tab).currentURI;
// Probably isn't the best method to identify the source tab // Probably isn't the best method to identify the source tab
if ( URI.spec !== details.openerURL ) { if ( URI.spec !== details.openerURL ) {

Loading…
Cancel
Save