Chrome Extension: chrome.storage undefined

I added the following code to my other Google Chrome Extension ...

var storage = chrome.storage ; console.log("storage is " + storage) ; var bookmarks = chrome.bookmarks ; console.log("bookmarks is " + bookmarks) ; 

After starting, the console says

 storage is undefined bookmarks is [object Object] 

In other words, bookmarks work fine, but storage is not in action. My manifest requested both ...

 { ... "permissions": [ "bookmarks", "tabs", "storage" ], } 

In case it matters, this extension is installed as an external extension in Mac OS X. To make sure it was updated correctly, I copied the above code from the files installed in ~ / Library / Application Support / Google / Chrome / By default / extensions. And of course, I restarted Chrome.

Why can chrome.storage be undefined?

+4
source share
2 answers

LocalStorage is only available in background pages and pop-ups. If you need to access some data, you will need to transfer the message from the current page to the background image. Here is the code:

On the current page:

 chrome.extension.sendMessage({action:"getstorage"}, function(response){ console.log("storage is " + response.myVar); }); 

On the background page:

 chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { if (request.action == "focusWindow"){ sendResponse({myVar: localStorage.myStorage}); } }); 

You can find more examples of Message Passing in the chrome documentation.

+3
source

On the internal tab "Extensions" there is a link "Restart (Ctrl + R)", clicking on it fixes the problem (I spent several hours). If you disable / enable the extension, neither restarting the Chrome browser will fix the problem. Hope this saves you time;)

+2
source

All Articles