Uncaught TypeError: cannot read 'local' property from undefined in chrome extension

I wrote a Chrome extension. I cannot use localStorage.setItem and localStorage.getItem to save and retrieve, because the background and browser actions are executed in a different environment [ as shown here ].

So, I decided to use the Chrome Storage API:

 var storage = chrome.storage.local; var myTestVar = 'somevar'; var obj = {}; obj[myTestVar] = $("#somevar").val(); storage.set(obj); 

which caused the following error:

Uncaught TypeError: Cannot read the 'local' property from undefined

What am I doing wrong?

+4
source share
1 answer

Ensure that all necessary permissions have been declared in the manifest file. "storage" , in your case.

In general, the following steps should fix the seemingly undefined Chrome API problem:

  • Read the API documentation that you are using and familiarize yourself with the prerequisites, usually show permissions ( e.g. chrome.storage # manifest ).
  • Check if your (custom) version of the Chrome API supports by looking at What's New .
  • Check if the script is working in the right context . Most APIs are available only for the extension process. (The chrome.storage API chrome.storage also be used in script content)
  • Otherwise, resort to your usual debugging skills: Typos, variable shadow, ...
+8
source

All Articles