Index: lib/icon.js |
=================================================================== |
--- a/lib/icon.js |
+++ b/lib/icon.js |
@@ -15,27 +15,30 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
/** @module icon */ |
"use strict"; |
const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
+const info = require("info"); |
const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, |
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, |
0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]; |
const numberOfFrames = frameOpacities.length; |
let stopRequested = false; |
let canUpdateIcon = true; |
let notRunning = Promise.resolve(); |
let whitelistedState = new ext.PageMap(); |
+let icons = [null, null]; |
+ |
function loadImage(url) |
{ |
return new Promise((resolve, reject) => |
{ |
let image = new Image(); |
image.src = url; |
image.addEventListener("load", () => |
{ |
@@ -43,37 +46,69 @@ |
}); |
image.addEventListener("error", () => |
{ |
reject("Failed to load image " + url); |
}); |
}); |
} |
+function renderIcons() |
+{ |
+ let paths = [ |
+ "icons/abp-16.png", "icons/abp-16-whitelisted.png", |
+ "icons/abp-32.png", "icons/abp-32-whitelisted.png" |
+ ]; |
+ |
+ for (let path of paths) |
+ { |
+ loadImage(path).then(image => |
+ { |
+ let [, size, whitelisted] = /\/abp-(16|32)(-whitelisted)?\./.exec(path); |
Manish Jethani
2019/02/03 09:47:55
Note: size is a string, not a number, but that's f
|
+ |
+ let canvas = document.createElement("canvas"); |
+ let context = canvas.getContext("2d"); |
+ let imageData = icons[!!whitelisted | 0] || {}; |
Sebastian Noack
2019/02/03 10:07:54
Nit: Isn't coercing to boolean (through !!) redund
Manish Jethani
2019/02/03 10:37:55
`"-whitelisted" | 0` is 0. Because "-whitelisted"
|
+ |
+ canvas.width = size; |
+ canvas.height = size; |
+ context.globalAlpha = 1; |
+ context.drawImage(image, 0, 0); |
+ imageData[size] = context.getImageData(0, 0, size, size); |
+ |
+ icons[!!whitelisted | 0] = imageData; |
+ }); |
+ } |
+} |
+ |
function setIcon(page, notificationType, opacity, frames) |
{ |
opacity = opacity || 0; |
let whitelisted = !!whitelistedState.get(page); |
if (!notificationType || !frames) |
{ |
if (opacity > 0.5) |
{ |
- page.browserAction.setIcon("/icons/abp-$size-notification-" + |
- notificationType + ".png"); |
+ page.browserAction.setIconPath("/icons/abp-$size-notification-" + |
+ notificationType + ".png"); |
+ } |
+ else if (icons[whitelisted | 0]) |
+ { |
+ page.browserAction.setIconImageData(icons[whitelisted | 0]); |
} |
else |
{ |
- page.browserAction.setIcon("/icons/abp-$size" + |
- (whitelisted ? "-whitelisted" : "") + ".png"); |
+ page.browserAction.setIconPath("/icons/abp-$size" + |
+ (whitelisted ? "-whitelisted" : "") + ".png"); |
} |
} |
else |
{ |
- browser.browserAction.setIcon({ |
+ browser.browserAction.setIconPath({ |
tabId: page.id, |
imageData: frames["" + opacity + whitelisted] |
}); |
} |
} |
filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => |
{ |
@@ -224,8 +259,12 @@ |
return; |
} |
animateIcon(type, frames); |
}, 10000); |
}); |
}); |
}; |
+ |
+// Pre-render icons on Chromium (#7253). |
+if (info.platform == "chromium") |
+ renderIcons(); |