1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-09-29 08:41:11 +13:00

Firefox: determine tabIds via <browser>

This commit is contained in:
Deathamns 2015-03-16 17:30:05 +01:00 committed by gorhill
parent 8f9be95a08
commit 1629accdb8

View file

@ -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; } else if ( target.linkedPanel ) {
}
if ( target.linkedPanel ) {
// target is a tab // target is a tab
return target.linkedPanel; target = target.linkedBrowser;
} }
if ( target.localName !== 'browser' ) {
// target is a browser return vAPI.noTabId;
var i;
var gBrowser = getOwnerWindow(target).gBrowser;
if ( !gBrowser ) {
return -1;
} }
var tabId = this.stack.get(target);
// This should be more efficient from version 35 if ( !tabId ) {
if ( gBrowser.getTabForBrowser ) { tabId = '' + this.stackId++;
i = gBrowser.getTabForBrowser(target); this.stack.set(target, tabId);
return i ? i.linkedPanel : -1;
} }
return tabId;
if ( !gBrowser.browsers ) {
return -1;
}
i = gBrowser.browsers.indexOf(target);
if ( i !== -1 ) {
i = gBrowser.tabs[i].linkedPanel;
}
return i;
}; };
/******************************************************************************/ /******************************************************************************/
@ -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) { vAPI.tabs.getTabsForIds = function(tabIds) {
var tabId;
var tabs = []; var tabs = [];
var singleTab = !Array.isArray(tabIds); var singleTab = !Array.isArray(tabIds);
if ( singleTab ) { if ( singleTab ) {
tabIds = [tabIds]; tabIds = [tabIds];
} }
for ( var tab of this.getAll() ) {
if ( vAPI.fennec ) { var browser = getBrowserForTab(tab);
for ( tabId of tabIds ) { var tabId = this.stack.get(browser);
if ( !tabId ) {
var tab = tabBrowser.tabs.find(tab=>tab.browser.loadContext.DOMWindowID === Number(tabId)); continue;
if ( tab ) {
tabs.push(tab);
}
} }
} else { if ( tabIds.indexOf(tabId) !== -1 ) {
var query = []; tabs.push(tab);
for ( tabId of tabIds ) { }
query.push('tab[linkedpanel="' + tabId + '"]'); 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(); tab = this.getTabsForIds(tabId);
for ( win of windows ) { if ( tab ) {
tab = vAPI.tabs.getTabsForIds(tabId, getTabBrowser(win)); win = getOwnerWindow(tab);
if ( tab ) {
break;
}
} }
} }
@ -555,10 +530,7 @@ vAPI.tabs.get = function(tabId, callback) {
return; return;
} }
if ( !windows ) { var windows = this.getWindows();
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);
tab = this.getTabsForIds(details.tabId, win); if ( tab ) {
if ( tab ) { getBrowserForTab(tab).loadURI(details.url);
getBrowserForTab(tab).loadURI(details.url); return;
return;
}
} }
} }
@ -707,16 +677,12 @@ vAPI.tabs.remove = function(tabIds) {
if ( !Array.isArray(tabIds) ) { if ( !Array.isArray(tabIds) ) {
tabIds = [tabIds]; tabIds = [tabIds];
} }
var tabs = this.getTabsForIds(tabIds);
for ( var win of this.getWindows() ) { if ( tabs.length === 0 ) {
var tabBrowser = getTabBrowser(win); return;
var tabs = this.getTabsForIds(tabIds, tabBrowser); }
if ( !tabs ) { for ( var tab of tabs ) {
continue; this._remove(tab, getTabBrowser(getOwnerWindow(tab)));
}
for ( var tab of tabs ) {
this._remove(tab, tabBrowser);
}
} }
}; };
@ -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 ) {