I'm having a weird issue trying to implement the Chrome keyboard shortcuts API in a Chrome extension I'm developing. Put shortly, I have to press the designated keyboard shortcut 2 times to change the stored value.
Here's my keyboard shortcut listener:
chrome.commands.onCommand.addListener(function(command) {
chrome.storage.sync.get([command], function(result) {
toggle = result[command];
});
toggle = toggle_value(toggle)
chrome.storage.sync.set({[command]: toggle }, function() {
console.log( {[command]: toggle} );
});
});
And my toggle_value()
function:
function toggle_value(value) {
if(value == 0) {
return 1;
} else if(value == 1) {
return 0;
}
}
My keyboard shortcut listener receives a command
as set in the manifest, gets the corresponding value of the same key name from the Chrome synced storage database, toggles the value then it sets it back to the Chrome synced storage using the same key name.
As stated earlier, the value of the command
key is only really toggled after pressing the keyboard shortcut 2 consecutive times. I spent hours debugging this with no luck, please help me.