From 756887a6cf7fb1b154b95c7497efa89fdea484ab Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 3 Dec 2015 12:07:46 -0500 Subject: [PATCH] code review re. lagacy toolbar button --- platform/firefox/vapi-background.js | 47 ++++++++++++++++++----------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 30c31f4ad..62cdf4569 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -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); }; })();