-1

I'm trying to access the local storage data in Edge set by my options page, using my popup script. Is there a current, working example of this available?

I was using localStorage, but it would only update the popup if I reloaded the extension after saving changes in my options page. I want to make it easier on the user by allowing the popup to access it immediately after saving, without reloading. I found the browser.storage.local.get(), but documentation conflicts everywhere I look, and I can't find viable working examples.

I have used, per Edge documentation:


browser.storage.local.get("sample");

But it throws an error requiring a callback function. So then I used:

let sample = browser.storage.local.get("example");
sample.then(ifGood, ifBad);

I get an error regarding property "then".

Then I simply tried adding a callback to the action itself:

sample = browser.storage.local.get("example", callbackFunction);

function callbackFunction(data){
    alert(data);
}

The end alert should display a string, but it just displays an empty Object. How do I access the data returned in the callback? I tried callbackFunction(this) as an argument in the get, but it throws an error about the syntax of the get.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • This is an public issue in Edge, and it will not support the callback function in Edge browser. You can check [this thread](https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9420301/): "A Microsoft Edge Extension does not support the Promise model; this is a documented difference between Mozilla and Edge’s platform. Currently, we do not plan to release a fix." – Zhi Lv Apr 04 '19 at 08:19
  • I had seen that, so then my question is what's the best way to address the local storage issue in Edge extensions? Is it just grin and bear it and have the user reload the extension each time they save something in the options? A bit inefficient, but I guess if that's the way it works... – Michael Rudd Apr 04 '19 at 12:54

2 Answers2

0

I found a work-around using browser.runtime.reload() in the Options page when saving the changes. It still reloads the extension, but it does it without requiring the user to do it manually.

0

You should use this syntax:
browser.storage.local.get(propertyName | null, callbackFn)
where
callbackFn = function fn(resultObject) {...}

When you pass null you will get whole storage object.

Look for example 1 or example 2 in my Edge extension.

bam
  • 106
  • 1
  • 5