| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 |
| 34 let icons = [null, null]; | 35 let icons = [null, null]; |
|
Manish Jethani
2019/02/01 14:42:43
Not whitelisted (0) and whitelisted (1).
| |
| 35 | 36 |
| 36 function loadImage(url) | 37 function loadImage(url) |
| 37 { | 38 { |
| 38 return new Promise((resolve, reject) => | 39 return new Promise((resolve, reject) => |
| 39 { | 40 { |
| 40 let image = new Image(); | 41 let image = new Image(); |
| 41 image.src = url; | 42 image.src = url; |
| 42 image.addEventListener("load", () => | 43 image.addEventListener("load", () => |
| 43 { | 44 { |
| 44 resolve(image); | 45 resolve(image); |
| 45 }); | 46 }); |
| 46 image.addEventListener("error", () => | 47 image.addEventListener("error", () => |
| 47 { | 48 { |
| 48 reject("Failed to load image " + url); | 49 reject("Failed to load image " + url); |
| 49 }); | 50 }); |
| 50 }); | 51 }); |
| 51 } | 52 } |
| 52 | 53 |
| 53 function renderIcons() | 54 function renderIcons() |
| 54 { | 55 { |
| 55 Promise.all([ | 56 let paths = [ |
| 56 loadImage("icons/abp-16.png"), | 57 "icons/abp-16.png", "icons/abp-16-whitelisted.png", |
| 57 loadImage("icons/abp-16-whitelisted.png"), | 58 "icons/abp-32.png", "icons/abp-32-whitelisted.png" |
| 58 loadImage("icons/abp-19.png"), | 59 ]; |
| 59 loadImage("icons/abp-19-whitelisted.png"), | 60 |
| 60 loadImage("icons/abp-20.png"), | 61 for (let path of paths) |
| 61 loadImage("icons/abp-20-whitelisted.png"), | 62 { |
| 62 loadImage("icons/abp-32.png"), | 63 loadImage(path).then(image => |
| 63 loadImage("icons/abp-32-whitelisted.png"), | 64 { |
| 64 loadImage("icons/abp-38.png"), | 65 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
| |
| 65 loadImage("icons/abp-38-whitelisted.png"), | 66 |
| 66 loadImage("icons/abp-40.png"), | 67 let canvas = document.createElement("canvas"); |
| 67 loadImage("icons/abp-40-whitelisted.png"), | 68 let context = canvas.getContext("2d"); |
| 68 ]) | 69 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"
| |
| 69 .then(images => | 70 |
| 70 { | 71 canvas.width = size; |
| 71 let imageMap = { | 72 canvas.height = size; |
| 72 16: [images[0], images[1]], | 73 context.globalAlpha = 1; |
| 73 19: [images[2], images[3]], | 74 context.drawImage(image, 0, 0); |
| 74 20: [images[4], images[5]], | 75 imageData[size] = context.getImageData(0, 0, size, size); |
| 75 32: [images[6], images[7]], | 76 |
| 76 38: [images[8], images[9]], | 77 icons[!!whitelisted | 0] = imageData; |
| 77 40: [images[10], images[11]] | 78 }); |
| 78 }; | 79 } |
| 79 | |
| 80 let canvas = document.createElement("canvas"); | |
| 81 let context = canvas.getContext("2d"); | |
| 82 | |
| 83 for (let whitelisted of [false, true]) | |
| 84 { | |
| 85 let imageData = {}; | |
| 86 let sizes = [16, 19, 20, 32, 38, 40]; | |
| 87 | |
| 88 for (let size of sizes) | |
| 89 { | |
| 90 canvas.width = size; | |
| 91 canvas.height = size; | |
| 92 context.globalAlpha = 1; | |
| 93 context.drawImage(imageMap[size][whitelisted | 0], 0, 0); | |
| 94 imageData[size] = context.getImageData(0, 0, size, size); | |
| 95 } | |
| 96 | |
| 97 icons[whitelisted | 0] = imageData; | |
| 98 } | |
| 99 }); | |
| 100 } | 80 } |
| 101 | 81 |
| 102 function setIcon(page, notificationType, opacity, frames) | 82 function setIcon(page, notificationType, opacity, frames) |
| 103 { | 83 { |
| 104 opacity = opacity || 0; | 84 opacity = opacity || 0; |
| 105 let whitelisted = !!whitelistedState.get(page); | 85 let whitelisted = !!whitelistedState.get(page); |
| 106 | 86 |
| 107 if (!notificationType || !frames) | 87 if (!notificationType || !frames) |
| 108 { | 88 { |
| 109 if (opacity > 0.5) | 89 if (opacity > 0.5) |
| 110 { | 90 { |
| 111 page.browserAction.setIcon("/icons/abp-$size-notification-" + | 91 page.browserAction.setIconPath("/icons/abp-$size-notification-" + |
| 112 notificationType + ".png"); | 92 notificationType + ".png"); |
| 93 } | |
| 94 else if (icons[whitelisted | 0]) | |
| 95 { | |
| 96 page.browserAction.setIconImageData(icons[whitelisted | 0]); | |
| 113 } | 97 } |
| 114 else | 98 else |
| 115 { | 99 { |
| 116 page.browserAction.setIcon("/icons/abp-$size" + | 100 page.browserAction.setIconPath("/icons/abp-$size" + |
| 117 (whitelisted ? "-whitelisted" : "") + ".png", | 101 (whitelisted ? "-whitelisted" : "") + ".png "); |
| 118 icons[whitelisted | 0]); | |
| 119 } | 102 } |
| 120 } | 103 } |
| 121 else | 104 else |
| 122 { | 105 { |
| 123 browser.browserAction.setIcon({ | 106 browser.browserAction.setIconPath({ |
| 124 tabId: page.id, | 107 tabId: page.id, |
| 125 imageData: frames["" + opacity + whitelisted] | 108 imageData: frames["" + opacity + whitelisted] |
| 126 }); | 109 }); |
| 127 } | 110 } |
| 128 } | 111 } |
| 129 | 112 |
| 130 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => | 113 filterNotifier.on("page.WhitelistingStateRevalidate", (page, filter) => |
| 131 { | 114 { |
| 132 whitelistedState.set(page, !!filter); | 115 whitelistedState.set(page, !!filter); |
| 133 if (canUpdateIcon) | 116 if (canUpdateIcon) |
| 134 setIcon(page); | 117 setIcon(page); |
| 135 }); | 118 }); |
| 136 | 119 |
| 137 function renderFrames(notificationType) | 120 function renderFrames(notificationType) |
| 138 { | 121 { |
| 139 return Promise.all([ | 122 return Promise.all([ |
| 140 loadImage("icons/abp-16.png"), | 123 loadImage("icons/abp-16.png"), |
| 141 loadImage("icons/abp-16-whitelisted.png"), | 124 loadImage("icons/abp-16-whitelisted.png"), |
| 142 loadImage("icons/abp-16-notification-" + notificationType + ".png"), | 125 loadImage("icons/abp-16-notification-" + notificationType + ".png"), |
| 143 loadImage("icons/abp-19.png"), | |
| 144 loadImage("icons/abp-19-whitelisted.png"), | |
| 145 loadImage("icons/abp-19-notification-" + notificationType + ".png"), | |
| 146 loadImage("icons/abp-20.png"), | 126 loadImage("icons/abp-20.png"), |
| 147 loadImage("icons/abp-20-whitelisted.png"), | 127 loadImage("icons/abp-20-whitelisted.png"), |
| 148 loadImage("icons/abp-20-notification-" + notificationType + ".png"), | 128 loadImage("icons/abp-20-notification-" + notificationType + ".png"), |
| 149 loadImage("icons/abp-32.png"), | 129 loadImage("icons/abp-32.png"), |
| 150 loadImage("icons/abp-32-whitelisted.png"), | 130 loadImage("icons/abp-32-whitelisted.png"), |
| 151 loadImage("icons/abp-32-notification-" + notificationType + ".png"), | 131 loadImage("icons/abp-32-notification-" + notificationType + ".png"), |
| 152 loadImage("icons/abp-38.png"), | |
| 153 loadImage("icons/abp-38-whitelisted.png"), | |
| 154 loadImage("icons/abp-38-notification-" + notificationType + ".png"), | |
| 155 loadImage("icons/abp-40.png"), | 132 loadImage("icons/abp-40.png"), |
| 156 loadImage("icons/abp-40-whitelisted.png"), | 133 loadImage("icons/abp-40-whitelisted.png"), |
| 157 loadImage("icons/abp-40-notification-" + notificationType + ".png") | 134 loadImage("icons/abp-40-notification-" + notificationType + ".png") |
| 158 ]).then(images => | 135 ]).then(images => |
| 159 { | 136 { |
| 160 let imageMap = { | 137 let imageMap = { |
| 161 16: {base: [images[0], images[1]], overlay: images[2]}, | 138 16: {base: [images[0], images[1]], overlay: images[2]}, |
| 162 19: {base: [images[3], images[4]], overlay: images[5]}, | 139 20: {base: [images[3], images[4]], overlay: images[5]}, |
| 163 20: {base: [images[6], images[7]], overlay: images[8]}, | 140 32: {base: [images[6], images[7]], overlay: images[8]}, |
| 164 32: {base: [images[9], images[10]], overlay: images[11]}, | 141 40: {base: [images[9], images[10]], overlay: images[11]} |
| 165 38: {base: [images[12], images[13]], overlay: images[14]}, | |
| 166 40: {base: [images[15], images[16]], overlay: images[17]} | |
| 167 }; | 142 }; |
| 168 | 143 |
| 169 let frames = {}; | 144 let frames = {}; |
| 170 let canvas = document.createElement("canvas"); | 145 let canvas = document.createElement("canvas"); |
| 171 let context = canvas.getContext("2d"); | 146 let context = canvas.getContext("2d"); |
| 172 | 147 |
| 173 for (let whitelisted of [false, true]) | 148 for (let whitelisted of [false, true]) |
| 174 { | 149 { |
| 175 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10) | 150 for (let i = 0, opacity = 0; i <= 10; opacity = ++i / 10) |
| 176 { | 151 { |
| 177 let imageData = {}; | 152 let imageData = {}; |
| 178 let sizes = [16, 19, 20, 32, 38, 40]; | 153 let sizes = [16, 20, 32, 40]; |
| 179 for (let size of sizes) | 154 for (let size of sizes) |
| 180 { | 155 { |
| 181 canvas.width = size; | 156 canvas.width = size; |
| 182 canvas.height = size; | 157 canvas.height = size; |
| 183 context.globalAlpha = 1; | 158 context.globalAlpha = 1; |
| 184 context.drawImage(imageMap[size]["base"][whitelisted | 0], 0, 0); | 159 context.drawImage(imageMap[size]["base"][whitelisted | 0], 0, 0); |
| 185 context.globalAlpha = opacity; | 160 context.globalAlpha = opacity; |
| 186 context.drawImage(imageMap[size]["overlay"], 0, 0); | 161 context.drawImage(imageMap[size]["overlay"], 0, 0); |
| 187 imageData[size] = context.getImageData(0, 0, size, size); | 162 imageData[size] = context.getImageData(0, 0, size, size); |
| 188 } | 163 } |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 resolve(); | 258 resolve(); |
| 284 return; | 259 return; |
| 285 } | 260 } |
| 286 | 261 |
| 287 animateIcon(type, frames); | 262 animateIcon(type, frames); |
| 288 }, 10000); | 263 }, 10000); |
| 289 }); | 264 }); |
| 290 }); | 265 }); |
| 291 }; | 266 }; |
| 292 | 267 |
| 293 renderIcons(); | 268 // Pre-render icons on Chromium (#7253). |
| 269 if (info.platform == "chromium") | |
| 270 renderIcons(); | |
| LEFT | RIGHT |