Left: | ||
Right: |
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 |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 /* Browser actions */ | 294 /* Browser actions */ |
295 | 295 |
296 let BrowserAction = function(tabId) | 296 let BrowserAction = function(tabId) |
297 { | 297 { |
298 this._tabId = tabId; | 298 this._tabId = tabId; |
299 this._changes = null; | 299 this._changes = null; |
300 }; | 300 }; |
301 BrowserAction.prototype = { | 301 BrowserAction.prototype = { |
302 _applyChanges() | 302 _applyChanges() |
303 { | 303 { |
304 if ("iconPath" in this._changes) | 304 return Promise.all(Object.keys(this._changes).map(change => |
305 { | 305 { |
306 // Firefox for Android displays the browser action not as an icon but | 306 // Firefox for Android displays the browser action not as an icon but |
307 // as a menu item. There is no icon, but such an option may be added in | 307 // as a menu item. There is no icon, but such an option may be added |
308 // the future. | 308 // in the future. |
309 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 | 309 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 |
310 if ("setIcon" in browser.browserAction) | 310 if (change == "iconPath" && "setIcon" in browser.browserAction) |
311 { | 311 { |
312 let path = { | 312 let path = { |
313 16: this._changes.iconPath.replace("$size", "16"), | 313 16: this._changes.iconPath.replace("$size", "16"), |
314 19: this._changes.iconPath.replace("$size", "19"), | 314 19: this._changes.iconPath.replace("$size", "19"), |
315 20: this._changes.iconPath.replace("$size", "20"), | 315 20: this._changes.iconPath.replace("$size", "20"), |
316 32: this._changes.iconPath.replace("$size", "32"), | 316 32: this._changes.iconPath.replace("$size", "32"), |
317 38: this._changes.iconPath.replace("$size", "38"), | 317 38: this._changes.iconPath.replace("$size", "38"), |
318 40: this._changes.iconPath.replace("$size", "40") | 318 40: this._changes.iconPath.replace("$size", "40") |
319 }; | 319 }; |
320 try | 320 try |
321 { | 321 { |
322 browser.browserAction.setIcon({tabId: this._tabId, path}); | 322 return browser.browserAction.setIcon({tabId: this._tabId, path}); |
323 } | 323 } |
324 catch (e) | 324 catch (e) |
325 { | 325 { |
326 // Edge throws if passed icon sizes different than 19,20,38,40px. | 326 // Edge throws if passed icon sizes different than 19,20,38,40px. |
327 delete path[16]; | 327 delete path[16]; |
328 delete path[32]; | 328 delete path[32]; |
329 browser.browserAction.setIcon({tabId: this._tabId, path}); | 329 return browser.browserAction.setIcon({tabId: this._tabId, path}); |
330 } | 330 } |
331 } | 331 } |
332 } | |
333 | 332 |
334 if ("badgeText" in this._changes) | |
335 { | |
336 // There is no badge on Firefox for Android; the browser action is | 333 // There is no badge on Firefox for Android; the browser action is |
337 // simply a menu item. | 334 // simply a menu item. |
338 if ("setBadgeText" in browser.browserAction) | 335 if (change == "badgeText" && "setBadgeText" in browser.browserAction) |
339 { | 336 return browser.browserAction.setBadgeText({ |
340 browser.browserAction.setBadgeText({ | |
341 tabId: this._tabId, | 337 tabId: this._tabId, |
342 text: this._changes.badgeText | 338 text: this._changes.badgeText |
343 }); | 339 }); |
344 } | |
345 } | |
346 | 340 |
347 if ("badgeColor" in this._changes) | |
348 { | |
349 // There is no badge on Firefox for Android; the browser action is | 341 // There is no badge on Firefox for Android; the browser action is |
350 // simply a menu item. | 342 // simply a menu item. |
351 if ("setBadgeBackgroundColor" in browser.browserAction) | 343 if (change == "badgeColor" && |
352 { | 344 "setBadgeBackgroundColor" in browser.browserAction) |
353 browser.browserAction.setBadgeBackgroundColor({ | 345 return browser.browserAction.setBadgeBackgroundColor({ |
354 tabId: this._tabId, | 346 tabId: this._tabId, |
355 color: this._changes.badgeColor | 347 color: this._changes.badgeColor |
356 }); | 348 }); |
357 } | 349 })); |
358 } | |
359 | |
360 this._changes = null; | |
361 }, | |
362 _queueChanges() | |
363 { | |
364 browser.tabs.get(this._tabId, () => | |
365 { | |
366 // If the tab is prerendered, browser.tabs.get() sets | |
367 // browser.runtime.lastError and we have to delay our changes | |
368 // until the currently visible tab is replaced with the | |
369 // prerendered tab. Otherwise browser.browserAction.set* fails. | |
370 if (browser.runtime.lastError) | |
371 { | |
372 let onReplaced = (addedTabId, removedTabId) => | |
373 { | |
374 if (addedTabId == this._tabId) | |
375 { | |
376 browser.tabs.onReplaced.removeListener(onReplaced); | |
377 this._applyChanges(); | |
378 } | |
379 }; | |
380 browser.tabs.onReplaced.addListener(onReplaced); | |
381 } | |
382 else | |
383 { | |
384 this._applyChanges(); | |
385 } | |
386 }); | |
387 }, | 350 }, |
388 _addChange(name, value) | 351 _addChange(name, value) |
389 { | 352 { |
353 let onReplaced = (addedTabId, removedTabId) => | |
Jon Sonesen
2018/07/10 23:01:07
Since I had trouble removing the check below this
| |
354 { | |
355 if (addedTabId == this._tabId) | |
356 { | |
357 browser.tabs.onReplaced.removeListener(onReplaced); | |
358 this._applyChanges().then(() => | |
359 { | |
360 this._changes = null; | |
361 }); | |
362 } | |
363 }; | |
390 if (!this._changes) | 364 if (!this._changes) |
391 { | |
392 this._changes = {}; | 365 this._changes = {}; |
393 this._queueChanges(); | |
394 } | |
395 | 366 |
396 this._changes[name] = value; | 367 this._changes[name] = value; |
368 if (!browser.tabs.onReplaced.hasListener(onReplaced)) | |
Jon Sonesen
2018/07/10 23:01:07
Replacing this check with the applyChanges boolean
| |
369 { | |
370 this._applyChanges().then(() => | |
371 { | |
372 this._changes = null; | |
373 }).catch(() => | |
Jon Sonesen
2018/07/10 23:01:08
I still think this is more readable than the callb
| |
374 { | |
375 // If the tab is prerendered, browser.browserAction.set* fails | |
376 // and we have to delay our changes until the currently visible tab | |
377 // is replaced with the prerendered tab. | |
378 browser.tabs.onReplaced.addListener(onReplaced); | |
379 }); | |
380 } | |
397 }, | 381 }, |
398 setIcon(path) | 382 setIcon(path) |
399 { | 383 { |
400 this._addChange("iconPath", path); | 384 this._addChange("iconPath", path); |
401 }, | 385 }, |
402 setBadge(badge) | 386 setBadge(badge) |
403 { | 387 { |
404 if (!badge) | 388 if (!badge) |
405 { | 389 { |
406 this._addChange("badgeText", ""); | 390 this._addChange("badgeText", ""); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 return frames.get(0) || null; | 550 return frames.get(0) || null; |
567 } | 551 } |
568 }; | 552 }; |
569 } | 553 } |
570 | 554 |
571 return ext.onMessage._dispatch( | 555 return ext.onMessage._dispatch( |
572 message, sender, sendResponse | 556 message, sender, sendResponse |
573 ).includes(true); | 557 ).includes(true); |
574 }); | 558 }); |
575 } | 559 } |
OLD | NEW |