Skip to content

Commit 50ccfa1

Browse files
committed
Avoid use of async calls during toolbar handler
The async call can cause the browser to lose context and treat the remainder of the function as not being performed by a user gesture. This can lead to issues when trying to open the toolbar popup.
1 parent 823265d commit 50ccfa1

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

background.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* We need to cache this setting in global state, because looking up the user's preference from storage is async.
3+
* Making the async call in the browserAction click handler can lose "transient activation" state and results in "user gesture" errors.
4+
* This needs to be updated any time the Quick Resize option is changed OR the number of presets changes to/from 0.
5+
*/
6+
var _quickResizePresetID = null;
7+
async function updateCachedQuickResize()
8+
{
9+
const storage = await browser.storage.local.get(["options","presets"]);
10+
_quickResizePresetID = storage.options.useQuickResizeMode && storage.presets.length > 0 ? storage.presets[0].id : null;
11+
console.debug("Updated _quickResizePresetID to " + _quickResizePresetID);
12+
}
13+
114
async function initialize(details)
215
{
316
console.debug("Initializing...");
@@ -58,6 +71,7 @@ async function initialize(details)
5871
}
5972

6073
applyQuickResizeSetting();
74+
updateCachedQuickResize();
6175

6276
console.debug("Initialization complete...");
6377
console.debug(" Storage version: " + storage.version);
@@ -66,10 +80,20 @@ async function initialize(details)
6680
console.debug(" Advanced count: " + Object.keys(storage.advanced).length);
6781
}
6882

83+
function storageChangeHandler(changes, areaName)
84+
{
85+
console.debug("Storage change for " + areaName, changes);
86+
87+
if(areaName === "local" && (changes.options || changes.presets))
88+
{
89+
updateCachedQuickResize();
90+
}
91+
}
92+
6993
// Hotkey handler
7094
async function handleCommand(command)
7195
{
72-
console.debug("handling command: " + command);
96+
console.debug("Handling command: " + command);
7397

7498
if(command.startsWith(PREFIX_PRESET))
7599
{
@@ -84,18 +108,13 @@ async function handleCommand(command)
84108
}
85109
}
86110

87-
async function toolbarClickHandler()
111+
function toolbarClickHandler()
88112
{
89113
console.debug("Toolbar clicked");
90114

91-
const storage = await browser.storage.local.get(["presets","options"]);
92-
const presets = storage.presets;
93-
const options = storage.options;
94-
95-
useQuickResizeMode = options.useQuickResizeMode !== undefined && options.useQuickResizeMode;
96-
if(useQuickResizeMode && presets.length > 0)
115+
if(_quickResizePresetID)
97116
{
98-
applyPreset(presets[0].id);
117+
applyPreset(_quickResizePresetID);
99118
}
100119
else
101120
{
@@ -104,6 +123,7 @@ async function toolbarClickHandler()
104123
}
105124

106125
browser.runtime.onInstalled.addListener(initialize);
126+
browser.storage.onChanged.addListener(storageChangeHandler);
107127
browser.commands.onCommand.addListener(handleCommand);
108128
browser.browserAction.onClicked.addListener(toolbarClickHandler);
109129

0 commit comments

Comments
 (0)