OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module icon */ | 18 /** @module icon */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); | 22 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
| 23 const info = require("info"); |
23 | 24 |
24 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, | 25 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, |
25 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, | 26 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, |
26 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]; | 27 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]; |
27 const numberOfFrames = frameOpacities.length; | 28 const numberOfFrames = frameOpacities.length; |
28 | 29 |
29 let stopRequested = false; | 30 let stopRequested = false; |
30 let canUpdateIcon = true; | 31 let canUpdateIcon = true; |
31 let notRunning = Promise.resolve(); | 32 let notRunning = Promise.resolve(); |
32 let whitelistedState = new ext.PageMap(); | 33 let whitelistedState = new ext.PageMap(); |
33 | 34 |
| 35 let icons = [null, null]; |
| 36 |
34 function loadImage(url) | 37 function loadImage(url) |
35 { | 38 { |
36 return new Promise((resolve, reject) => | 39 return new Promise((resolve, reject) => |
37 { | 40 { |
38 let image = new Image(); | 41 let image = new Image(); |
39 image.src = url; | 42 image.src = url; |
40 image.addEventListener("load", () => | 43 image.addEventListener("load", () => |
41 { | 44 { |
42 resolve(image); | 45 resolve(image); |
43 }); | 46 }); |
44 image.addEventListener("error", () => | 47 image.addEventListener("error", () => |
45 { | 48 { |
46 reject("Failed to load image " + url); | 49 reject("Failed to load image " + url); |
47 }); | 50 }); |
48 }); | 51 }); |
49 } | 52 } |
50 | 53 |
| 54 function renderIcons() |
| 55 { |
| 56 Promise.all([ |
| 57 loadImage("icons/abp-16.png"), |
| 58 loadImage("icons/abp-16-whitelisted.png"), |
| 59 loadImage("icons/abp-32.png"), |
| 60 loadImage("icons/abp-32-whitelisted.png") |
| 61 ]) |
| 62 .then(images => |
| 63 { |
| 64 let imageMap = { |
| 65 16: [images[0], images[1]], |
| 66 32: [images[2], images[3]] |
| 67 }; |
| 68 |
| 69 let canvas = document.createElement("canvas"); |
| 70 let context = canvas.getContext("2d"); |
| 71 |
| 72 for (let whitelisted = 0; whitelisted <= 1; whitelisted++) |
| 73 { |
| 74 let imageData = {}; |
| 75 let sizes = [16, 32]; |
| 76 |
| 77 for (let size of sizes) |
| 78 { |
| 79 canvas.width = size; |
| 80 canvas.height = size; |
| 81 context.globalAlpha = 1; |
| 82 context.drawImage(imageMap[size][whitelisted], 0, 0); |
| 83 imageData[size] = context.getImageData(0, 0, size, size); |
| 84 } |
| 85 |
| 86 icons[whitelisted] = imageData; |
| 87 } |
| 88 }); |
| 89 } |
| 90 |
51 function setIcon(page, notificationType, opacity, frames) | 91 function setIcon(page, notificationType, opacity, frames) |
52 { | 92 { |
53 opacity = opacity || 0; | 93 opacity = opacity || 0; |
54 let whitelisted = !!whitelistedState.get(page); | 94 let whitelisted = !!whitelistedState.get(page); |
55 | 95 |
56 if (!notificationType || !frames) | 96 if (!notificationType || !frames) |
57 { | 97 { |
58 if (opacity > 0.5) | 98 if (opacity > 0.5) |
59 { | 99 { |
60 page.browserAction.setIcon("/icons/abp-$size-notification-" + | 100 page.browserAction.setIcon("/icons/abp-$size-notification-" + |
61 notificationType + ".png"); | 101 notificationType + ".png"); |
62 } | 102 } |
63 else | 103 else |
64 { | 104 { |
65 page.browserAction.setIcon("/icons/abp-$size" + | 105 page.browserAction.setIcon("/icons/abp-$size" + |
66 (whitelisted ? "-whitelisted" : "") + ".png"); | 106 (whitelisted ? "-whitelisted" : "") + ".png", |
| 107 icons[whitelisted | 0]); |
67 } | 108 } |
68 } | 109 } |
69 else | 110 else |
70 { | 111 { |
71 browser.browserAction.setIcon({ | 112 browser.browserAction.setIcon({ |
72 tabId: page.id, | 113 tabId: page.id, |
73 imageData: frames["" + opacity + whitelisted] | 114 imageData: frames["" + opacity + whitelisted] |
74 }); | 115 }); |
75 } | 116 } |
76 } | 117 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 clearInterval(interval); | 263 clearInterval(interval); |
223 resolve(); | 264 resolve(); |
224 return; | 265 return; |
225 } | 266 } |
226 | 267 |
227 animateIcon(type, frames); | 268 animateIcon(type, frames); |
228 }, 10000); | 269 }, 10000); |
229 }); | 270 }); |
230 }); | 271 }); |
231 }; | 272 }; |
| 273 |
| 274 // Pre-render icons on Chromium (#7253). |
| 275 if (info.platform == "chromium") |
| 276 renderIcons(); |
OLD | NEW |