Index: ext/background.js |
=================================================================== |
--- a/ext/background.js |
+++ b/ext/background.js |
@@ -312,109 +312,96 @@ |
let BrowserAction = function(tabId) |
{ |
this._tabId = tabId; |
this._changes = null; |
}; |
BrowserAction.prototype = { |
_applyChanges() |
{ |
- if ("iconPath" in this._changes) |
+ return Promise.all(Object.keys(this._changes).map(change => |
{ |
- // Firefox for Android displays the browser action not as an icon but |
- // as a menu item. There is no icon, but such an option may be added in |
- // the future. |
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 |
- if ("setIcon" in browser.browserAction) |
+ if (change == "iconPath") |
Sebastian Noack
2018/06/16 02:02:52
Combine the check here as well?
if (change == "
Jon Sonesen
2018/07/09 21:15:25
Acknowledged.
|
{ |
- let path = { |
- 16: this._changes.iconPath.replace("$size", "16"), |
- 19: this._changes.iconPath.replace("$size", "19"), |
- 20: this._changes.iconPath.replace("$size", "20"), |
- 32: this._changes.iconPath.replace("$size", "32"), |
- 38: this._changes.iconPath.replace("$size", "38"), |
- 40: this._changes.iconPath.replace("$size", "40") |
- }; |
- try |
+ // Firefox for Android displays the browser action not as an icon but |
+ // as a menu item. There is no icon, but such an option may be added |
+ // in the future. |
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 |
+ if ("setIcon" in browser.browserAction) |
{ |
- browser.browserAction.setIcon({tabId: this._tabId, path}); |
- } |
- catch (e) |
- { |
- // Edge throws if passed icon sizes different than 19,20,38,40px. |
- delete path[16]; |
- delete path[32]; |
- browser.browserAction.setIcon({tabId: this._tabId, path}); |
+ let path = { |
+ 16: this._changes.iconPath.replace("$size", "16"), |
+ 19: this._changes.iconPath.replace("$size", "19"), |
+ 20: this._changes.iconPath.replace("$size", "20"), |
+ 32: this._changes.iconPath.replace("$size", "32"), |
+ 38: this._changes.iconPath.replace("$size", "38"), |
+ 40: this._changes.iconPath.replace("$size", "40") |
+ }; |
+ try |
+ { |
+ return browser.browserAction.setIcon({tabId: this._tabId, path}); |
+ } |
+ catch (e) |
+ { |
+ // Edge throws if passed icon sizes different than 19,20,38,40px. |
+ delete path[16]; |
+ delete path[32]; |
+ return browser.browserAction.setIcon({tabId: this._tabId, path}); |
+ } |
} |
} |
- } |
- if ("badgeText" in this._changes) |
- { |
// There is no badge on Firefox for Android; the browser action is |
// simply a menu item. |
- if ("setBadgeText" in browser.browserAction) |
- { |
- browser.browserAction.setBadgeText({ |
+ if (change == "badgeText" && "setBadgeText" in browser.browserAction) |
+ return browser.browserAction.setBadgeText({ |
tabId: this._tabId, |
text: this._changes.badgeText |
}); |
- } |
- } |
- if ("badgeColor" in this._changes) |
- { |
// There is no badge on Firefox for Android; the browser action is |
// simply a menu item. |
- if ("setBadgeBackgroundColor" in browser.browserAction) |
- { |
- browser.browserAction.setBadgeBackgroundColor({ |
+ if (change == "badgeColor" && |
+ "setBadgeBackgroundColor" in browser.browserAction) |
Sebastian Noack
2018/06/16 02:02:52
Nit: Please indent this line so that the condition
Jon Sonesen
2018/07/09 21:15:25
Acknowledged.
|
+ return browser.browserAction.setBadgeBackgroundColor({ |
tabId: this._tabId, |
color: this._changes.badgeColor |
}); |
- } |
- } |
- |
- this._changes = null; |
- }, |
- _queueChanges() |
- { |
- browser.tabs.get(this._tabId, () => |
- { |
- // If the tab is prerendered, browser.tabs.get() sets |
- // browser.runtime.lastError and we have to delay our changes |
- // until the currently visible tab is replaced with the |
- // prerendered tab. Otherwise browser.browserAction.set* fails. |
- if (browser.runtime.lastError) |
- { |
- let onReplaced = (addedTabId, removedTabId) => |
- { |
- if (addedTabId == this._tabId) |
- { |
- browser.tabs.onReplaced.removeListener(onReplaced); |
- this._applyChanges(); |
- } |
- }; |
- browser.tabs.onReplaced.addListener(onReplaced); |
- } |
- else |
- { |
- this._applyChanges(); |
- } |
- }); |
+ })); |
}, |
_addChange(name, value) |
{ |
+ let onReplaced = (addedTabId, removedTabId) => |
Sebastian Noack
2018/06/16 02:02:53
For better code locality, I'd define this listener
Jon Sonesen
2018/07/09 21:15:24
Acknowledged.
|
+ { |
+ if (addedTabId == this._tabId) |
+ { |
+ browser.tabs.onReplaced.removeListener(onReplaced); |
+ this._applyChanges().then(() => |
+ { |
+ this._changes = null; |
+ }); |
+ } |
+ }; |
if (!this._changes) |
- { |
this._changes = {}; |
- this._queueChanges(); |
- } |
this._changes[name] = value; |
+ if (!browser.tabs.onReplaced.hasListener(onReplaced)) |
Sebastian Noack
2018/06/16 02:02:53
Is this check necessary? It might be better to jus
Jon Sonesen
2018/07/09 21:15:24
Acknowledged.
Jon Sonesen
2018/07/09 22:46:00
So perhaps i did this wrong but removing the check
|
+ { |
+ this._applyChanges().then(() => |
+ { |
+ this._changes = null; |
+ }).catch(() => |
Sebastian Noack
2018/06/16 02:02:53
Instead of .then(onSuccess).catch(onFailure),
you
Jon Sonesen
2018/07/09 21:15:25
Yeah, I am aware of that. I thought it was a bit m
|
+ { |
+ // If the tab is prerendered, browser.browserAction.set* fails |
+ // and we have to delay our changes until the currently visible tab |
+ // is replaced with the prerendered tab. |
+ browser.tabs.onReplaced.addListener(onReplaced); |
+ }); |
+ } |
}, |
setIcon(path) |
{ |
this._addChange("iconPath", path); |
}, |
setBadge(badge) |
{ |
if (!badge) |