1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-04 08:37:11 +02:00

code review

This commit is contained in:
gorhill 2015-10-08 17:44:21 -04:00
parent 10ecdbf1ea
commit 91ecabf82a

View File

@ -524,12 +524,23 @@ vAPI.storage = (function() {
/******************************************************************************/ /******************************************************************************/
var getTabBrowser = function(win) { var getTabBrowser = (function() {
return vAPI.fennec && win.BrowserApp || if ( vAPI.fennec ) {
vAPI.thunderbird && win.document.getElementById('tabmail') || return function(win) {
win.gBrowser || return win.BrowserApp || null;
null; };
}; }
if ( vAPI.thunderbird ) {
return function(win) {
win.document.getElementById('tabmail');
};
}
return function(win) {
return win.gBrowser || null;
};
})();
/******************************************************************************/ /******************************************************************************/
@ -562,6 +573,16 @@ vAPI.noTabId = '-1';
vAPI.tabs = {}; vAPI.tabs = {};
/******************************************************************************/
vAPI.tabs.mostRecentWindowId = (function() {
if ( vAPI.thunderbird ) {
return 'mail:3pane';
}
return 'navigator:browser';
})();
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.registerListeners = function() { vAPI.tabs.registerListeners = function() {
@ -661,7 +682,7 @@ vAPI.tabs.getAll = function(window) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.getWindows = function() { vAPI.tabs.getWindows = function() {
var winumerator = Services.wm.getEnumerator('navigator:browser'); var winumerator = Services.wm.getEnumerator(this.mostRecentWindowId);
var windows = []; var windows = [];
while ( winumerator.hasMoreElements() ) { while ( winumerator.hasMoreElements() ) {
@ -733,11 +754,13 @@ vAPI.tabs.open = function(details) {
} }
} }
var win = Services.wm.getMostRecentWindow(vAPI.thunderbird && 'mail:3pane' || 'navigator:browser'); var win = Services.wm.getMostRecentWindow(this.mostRecentWindowId);
var tabBrowser = getTabBrowser(win); var tabBrowser = getTabBrowser(win);
if ( vAPI.fennec ) { if ( vAPI.fennec ) {
tabBrowser.addTab(details.url, {selected: details.active !== false}); tabBrowser.addTab(details.url, {
selected: details.active !== false
});
// Note that it's impossible to move tabs on Fennec, so don't bother // Note that it's impossible to move tabs on Fennec, so don't bother
return; return;
} }
@ -755,7 +778,10 @@ vAPI.tabs.open = function(details) {
} }
if ( vAPI.thunderbird ) { if ( vAPI.thunderbird ) {
tabBrowser.openTab('contentTab', { contentPage: details.url, background: !details.active }); tabBrowser.openTab('contentTab', {
contentPage: details.url,
background: !details.active
});
// TODO: Should be possible to move tabs on Thunderbird // TODO: Should be possible to move tabs on Thunderbird
return; return;
} }
@ -791,13 +817,16 @@ vAPI.tabs.replace = function(tabId, url) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabs._remove = function(tab, tabBrowser) { vAPI.tabs._remove = (function() {
if ( vAPI.fennec ) { if ( vAPI.fennec ) {
tabBrowser.closeTab(tab); return function(tab, tabBrowser) {
return; tabBrowser.closeTab(tab);
};
} }
tabBrowser.removeTab(tab); return function(tab, tabBrowser) {
}; tabBrowser.removeTab(tab);
};
})();
/******************************************************************************/ /******************************************************************************/
@ -886,9 +915,10 @@ var tabWatcher = (function() {
var tabIdGenerator = 1; var tabIdGenerator = 1;
var indexFromBrowser = function(browser) { var indexFromBrowser = function(browser) {
if (vAPI.thunderbird) // TODO: Add support for this // TODO: Add support for this
if ( vAPI.thunderbird ) {
return -1; return -1;
}
var win = getOwnerWindow(browser); var win = getOwnerWindow(browser);
if ( !win ) { if ( !win ) {
return -1; return -1;
@ -979,17 +1009,19 @@ var tabWatcher = (function() {
}; };
var currentBrowser = function() { var currentBrowser = function() {
var win = Services.wm.getMostRecentWindow(vAPI.thunderbird && 'mail:3pane' || 'navigator:browser'); var win = Services.wm.getMostRecentWindow(vAPI.tabs.mostRecentWindowId);
// https://github.com/gorhill/uBlock/issues/399 // https://github.com/gorhill/uBlock/issues/399
// getTabBrowser() can return null at browser launch time. // getTabBrowser() can return null at browser launch time.
var tabBrowser = getTabBrowser(win); var tabBrowser = getTabBrowser(win);
if ( tabBrowser === null ) { if ( tabBrowser === null ) {
return null; return null;
} }
if (vAPI.thunderbird) { if ( vAPI.thunderbird ) {
// Directly at startup the first tab may not be initialized // Directly at startup the first tab may not be initialized
if (tabBrowser.tabInfo.length == 0) return null; if ( tabBrowser.tabInfo.length === 0 ) {
return tabBrowser.getBrowserForSelectedTab(); return null;
}
return tabBrowser.getBrowserForSelectedTab() || null;
} }
return browserFromTarget(tabBrowser.selectedTab); return browserFromTarget(tabBrowser.selectedTab);
}; };
@ -1204,7 +1236,7 @@ vAPI.setIcon = function(tabId, iconStatus, badge) {
// If badge is undefined, then setIcon was called from the TabSelect event // If badge is undefined, then setIcon was called from the TabSelect event
var win = badge === undefined var win = badge === undefined
? iconStatus ? iconStatus
: vAPI.thunderbird && Services.wm.getMostRecentWindow('mail:3pane') || Services.wm.getMostRecentWindow('navigator:browser'); : Services.wm.getMostRecentWindow(vAPI.tabs.mostRecentWindowId);
var curTabId = tabWatcher.tabIdFromTarget(getTabBrowser(win).selectedTab); var curTabId = tabWatcher.tabIdFromTarget(getTabBrowser(win).selectedTab);
var tb = vAPI.toolbarButton; var tb = vAPI.toolbarButton;