mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
code review: adding the option buttons at launch time is not critical, this can be deferred
This commit is contained in:
parent
8d41afeb7a
commit
9ce6f9eb23
@ -3372,55 +3372,89 @@ vAPI.contextMenu.remove = function() {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var optionsObserver = {
|
var optionsObserver = (function() {
|
||||||
addonId: 'uBlock0@raymondhill.net',
|
var addonId = 'uBlock0@raymondhill.net';
|
||||||
|
|
||||||
register: function() {
|
var commandHandler = function() {
|
||||||
Services.obs.addObserver(this, 'addon-options-displayed', false);
|
switch ( this.id ) {
|
||||||
cleanupTasks.push(this.unregister.bind(this));
|
case 'showDashboardButton':
|
||||||
|
vAPI.tabs.open({ url: 'dashboard.html', index: -1 });
|
||||||
var browser = tabWatcher.currentBrowser();
|
break;
|
||||||
if ( !browser ) {
|
case 'showNetworkLogButton':
|
||||||
return;
|
vAPI.tabs.open({ url: 'logger-ui.html', index: -1 });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/948
|
var setupOptionsButton = function(doc, id) {
|
||||||
// Older versions of Firefox can throw here when looking up `currentURI`.
|
|
||||||
var currentURI;
|
|
||||||
try {
|
|
||||||
currentURI = browser.currentURI;
|
|
||||||
} catch (ex) {
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( currentURI && currentURI.spec === 'about:addons' ) {
|
|
||||||
this.observe(browser.contentDocument, 'addon-enabled', this.addonId);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
unregister: function() {
|
|
||||||
Services.obs.removeObserver(this, 'addon-options-displayed');
|
|
||||||
},
|
|
||||||
|
|
||||||
setupOptionsButton: function(doc, id, page) {
|
|
||||||
var button = doc.getElementById(id);
|
var button = doc.getElementById(id);
|
||||||
if ( button === null ) {
|
if ( button === null ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
button.addEventListener('command', function() {
|
button.addEventListener('command', commandHandler);
|
||||||
vAPI.tabs.open({ url: page, index: -1 });
|
|
||||||
});
|
|
||||||
button.label = vAPI.i18n(id);
|
button.label = vAPI.i18n(id);
|
||||||
},
|
};
|
||||||
|
|
||||||
observe: function(doc, topic, addonId) {
|
var setupOptionsButtons = function(doc) {
|
||||||
if ( addonId !== this.addonId ) {
|
setupOptionsButton(doc, 'showDashboardButton');
|
||||||
return;
|
setupOptionsButton(doc, 'showNetworkLogButton');
|
||||||
|
};
|
||||||
|
|
||||||
|
var observer = {
|
||||||
|
observe: function(doc, topic, id) {
|
||||||
|
if ( id !== addonId ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setupOptionsButtons(doc);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
this.setupOptionsButton(doc, 'showDashboardButton', 'dashboard.html');
|
// https://github.com/gorhill/uBlock/issues/948
|
||||||
this.setupOptionsButton(doc, 'showNetworkLogButton', 'logger-ui.html');
|
// Older versions of Firefox can throw here when looking up `currentURI`.
|
||||||
}
|
|
||||||
};
|
var canInit = function() {
|
||||||
|
var ok;
|
||||||
|
try {
|
||||||
|
var tabBrowser = tabWatcher.currentBrowser();
|
||||||
|
ok = tabBrowser &&
|
||||||
|
tabBrowser.currentURI &&
|
||||||
|
tabBrowser.contentDocument &&
|
||||||
|
tabBrowser.contentDocument.readyState === 'complete';
|
||||||
|
} catch (ex) {
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Manually add the buttons if the `about:addons` page is already
|
||||||
|
// opened.
|
||||||
|
|
||||||
|
var init = function() {
|
||||||
|
if ( canInit() ) {
|
||||||
|
var tabBrowser = tabWatcher.currentBrowser();
|
||||||
|
if ( tabBrowser.currentURI.spec === 'about:addons' ) {
|
||||||
|
setupOptionsButtons(tabBrowser.contentDocument);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var unregister = function() {
|
||||||
|
Services.obs.removeObserver(observer, 'addon-options-displayed');
|
||||||
|
};
|
||||||
|
|
||||||
|
var register = function() {
|
||||||
|
Services.obs.addObserver(observer, 'addon-options-displayed', false);
|
||||||
|
cleanupTasks.push(unregister);
|
||||||
|
deferUntil(canInit, init);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
register: register,
|
||||||
|
unregister: unregister
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
optionsObserver.register();
|
optionsObserver.register();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user