I'm working on a chrome extension that uses an Event Page and I'm trying to completely clear it's local storage every time Chrome starts up.
This is what I have right now in eventPage.js:
chrome.runtime.onStartup.addListener(function() {
chrome.storage.local.clear(function() {
var error = chrome.runtime.lastError;
if (error)
console.error(error);
});
});
// The below code retrieves the object of interest 'storage_var'
// and does stuff with it
chrome.storage.local.get(init);
function init(storage_var) {
// doing stuff, includes a bunch of listeners and helpers that
// sometimes use chrome.storage.local.set to affect local storage
}
This does not seem to be working. I understand the asynchronous nature of these functions, so please spare me any long lectures about them. This isn't an attempt to somehow make the clear
function behave synchronously; I'm not just not sure how I can use the clear
function properly so it does as intended.
I was thinking about putting everything below the chrome.runtime.onStartup.addListener
call in the callback function of clear
so that it only gets called after the clear has finished, but this would mean putting essentially the whole program in the chrome startup listener, which doesn't seem like the right approach. I'm just not sure I'm thinking about this the right way.
Any help and wisdom about asynchronous functions would be appreciated. Please no rants though, I've already read plenty :) Thank you!