0

I need to access the localStorage of a chrome extension in background.js to get values which have been set by the extension popup itself. The key-value pair is existing, I checked that by inspecting the extension popup.

However, reading from the localStorage by using the code below leads to an undefined result.

background.js:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        chrome.storage.local.get(['allowedSites'], function(result) {
            console.log('allowedSites: ', result.allowedSites);
        });
    }
);

The storage permission is set in v3 manifest. Any ideas what's wrong with this code?

pfust75
  • 401
  • 1
  • 5
  • 17
  • chrome.storage.local is not `localStorage`, it's a different storage. See [Inspect extension's chrome.storage](https://stackoverflow.com/q/32435399) – wOxxOm May 08 '22 at 09:19
  • Thanks @wOxxOm! So no possibility to access ```localstorage``` from within ```background.js```? – pfust75 May 08 '22 at 09:35
  • 1
    No. Service worker can't access it. Don't use `localStorage`, switch to chrome.storage.local everywhere. – wOxxOm May 08 '22 at 10:23

0 Answers0