From 06756baed96f03b0a1bf3cd6b6a56e2e4bb5ee4b Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 27 May 2018 11:13:25 -0400 Subject: [PATCH] improve use of imageData in setIcon - Generate imageData according to existing path information - Pass a copy as argument (see https://github.com/NanoAdblocker/NanoCore/commit/06768dcadb098d021f0529fb014df6136f6e10e6) --- platform/chromium/vapi-background.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index d0a610ef9..fc204f1c8 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -649,11 +649,9 @@ vAPI.setIcon = (function() { ' ({badge})'; let icons = [ { - tabId: 0, path: { '16': 'img/icon_16-off.png', '32': 'img/icon_32-off.png' } }, { - tabId: 0, path: { '16': 'img/icon_16.png', '32': 'img/icon_32.png' } } ]; @@ -679,10 +677,14 @@ vAPI.setIcon = (function() { // https://searchfox.org/mozilla-central/rev/5ff2d7683078c96e4b11b8a13674daded935aa44/browser/components/extensions/parent/ext-browserAction.js#631 if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; } - let imgs = [ - { i: 0, p: '16' }, { i: 0, p: '32' }, - { i: 1, p: '16' }, { i: 1, p: '32' }, - ]; + let imgs = []; + for ( let i = 0; i < icons.length; i++ ) { + let path = icons[i].path; + for ( let key in path ) { + if ( path.hasOwnProperty(key) === false ) { continue; } + imgs.push({ i: i, p: key }); + } + } let onLoaded = function() { for ( let img of imgs ) { if ( img.r.complete === false ) { return; } @@ -708,8 +710,11 @@ vAPI.setIcon = (function() { } iconData[img.i][img.p] = imgData; } - icons[0] = { tabId: 0, imageData: iconData[0] }; - icons[1] = { tabId: 0, imageData: iconData[1] }; + for ( let i = 0; i < iconData.length; i++ ) { + if ( iconData[i] ) { + icons[i] = { imageData: iconData[i] }; + } + } }; for ( let img of imgs ) { img.r = new Image(); @@ -723,8 +728,9 @@ vAPI.setIcon = (function() { if ( browserAction.setIcon !== undefined ) { if ( parts === undefined || (parts & 0x01) !== 0 ) { - icons[state].tabId = tab.id; - browserAction.setIcon(icons[state]); + browserAction.setIcon( + Object.assign({ tabId: tab.id }, icons[state]) + ); } browserAction.setBadgeText({ tabId: tab.id, text: badge }); }