Search code examples
google-chrome-extensionlocal-storagesession-storage

Chrome Extension no data is surviving the popup refresh


Pre: I'm in chrome and logged in

  1. I launch (click on) my popup window. It saves some data in all storages (windowlocal, windowsession, google local and google sync)
  2. I right click on it, and choose inspect.
  3. The dev tools window comes up for the popup
  4. I look in Storage Explorer in Dev Tools, I see all my data is there, in all storages.enter image description here
  5. I put a breakpoint at the start of my popup.js code. This breakpoint is before it loads any data or does anything. It's at the beginning of the constructor.
  6. I hit F5 on the Dev Tools window. It flickers and comes back

Expected Result: I see all the data I've saved

Actual Result: No data whatsoever is there, in any of the storages. It's all gone.

What am I doing wrong? Is popup not supposed to be able to save data?


Solution

  • Issues

    1. If you put a breakpoint in the constructor of an extension on dev tools, it's likely that storage explorer won't show anything, because it hasn't loaded yet
    2. In my constructor, I was loading the settings correctly from storage, but then immediately overwriting them with the default values because I was calling load() first, and it had become asynchronous, and further down in my code in my settings property setters were re-saving the default values, so by the time load() resolved itself to execute, it was just reading the (just) saved default values.

    Solution

    I brought the load code out of the constructor into an async function, and called that function right at the end of my constructor. In that function, I used await to make sure the settings were loaded from storage, then continued to set the settings properties.