code review re. lagacy toolbar button

This commit is contained in:
gorhill 2015-12-03 12:07:46 -05:00
parent 13f42051b2
commit 756887a6cf
1 changed files with 29 additions and 18 deletions

View File

@ -2623,7 +2623,6 @@ vAPI.toolbarButton = {
tbb.id = 'uBlock0-legacy-button'; // NOTE: must match legacy-toolbar-button.css
tbb.viewId = tbb.id + '-panel';
var sss = null;
var styleSheetUri = null;
var createToolbarButton = function(window) {
@ -2651,6 +2650,18 @@ vAPI.toolbarButton = {
};
var addLegacyToolbarButton = function(window) {
// uBO's stylesheet lazily added.
if ( styleSheetUri === null ) {
var sss = Cc['@mozilla.org/content/style-sheet-service;1']
.getService(Ci.nsIStyleSheetService);
styleSheetUri = Services.io.newURI(vAPI.getURL('css/legacy-toolbar-button.css'), null, null);
// Register global so it works in all windows, including palette
if ( !sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) {
sss.loadAndRegisterSheet(styleSheetUri, sss.AUTHOR_SHEET);
}
}
var document = window.document;
// https://github.com/gorhill/uMatrix/issues/357
@ -2723,9 +2734,16 @@ vAPI.toolbarButton = {
var canAddLegacyToolbarButton = function(window) {
var document = window.document;
if (
!document ||
document.readyState !== 'complete' ||
document.getElementById('nav-bar') === null
) {
return false;
}
var toolbox = document.getElementById('navigator-toolbox') ||
document.getElementById('mail-toolbox');
return toolbox !== null && !!toolbox.palette && document.getElementById('nav-bar') !== null;
return toolbox !== null && !!toolbox.palette;
};
var onPopupCloseRequested = function({target}) {
@ -2752,19 +2770,20 @@ vAPI.toolbarButton = {
toolbarButton.parentNode.removeChild(toolbarButton);
}
}
if ( sss === null ) {
return;
}
if ( sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) {
sss.unregisterSheet(styleSheetUri, sss.AUTHOR_SHEET);
}
sss = null;
styleSheetUri = null;
vAPI.messaging.globalMessageManager.removeMessageListener(
location.host + ':closePopup',
onPopupCloseRequested
);
if ( styleSheetUri !== null ) {
var sss = Cc['@mozilla.org/content/style-sheet-service;1']
.getService(Ci.nsIStyleSheetService);
if ( sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) {
sss.unregisterSheet(styleSheetUri, sss.AUTHOR_SHEET);
}
styleSheetUri = null;
}
};
tbb.attachToNewWindow = function(win) {
@ -2780,14 +2799,6 @@ vAPI.toolbarButton = {
onPopupCloseRequested
);
sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService);
styleSheetUri = Services.io.newURI(vAPI.getURL("css/legacy-toolbar-button.css"), null, null);
// Register global so it works in all windows, including palette
if ( !sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) {
sss.loadAndRegisterSheet(styleSheetUri, sss.AUTHOR_SHEET);
}
cleanupTasks.push(shutdown);
};
})();