2

I am trying to get and set in my extension using background.js but whenever I run the extension I get the error saying that referenceError: localStorage is not defined.

whereas if I use it in my App.js , it is getting and setting in the local storage.

Note: I am using manifest v3

chrome.cookies.get(
      { url: "https://www.xxxx.com/", name: "abc" },
      async (cookie) => {
        const cookieValue: any = cookie?.value;
        oldJwtToken = localStorage.getItem("abc");
        console.log(cookieValue);
        console.log(oldJwtToken);
}
Quick Hotshot
  • 55
  • 1
  • 9
  • 1
    Service workers don't have access to localStorage. Switch to [chrome.storage](https://developer.chrome.com/extensions/storage). – wOxxOm Mar 16 '22 at 14:08
  • I tried with chrome.storage.local but the set value is not fetching. I am getting undefined value. – Quick Hotshot Mar 16 '22 at 14:11
  • Also a help : Can u help me where to see the value stored when using chrome.storage.local.set() ? – Quick Hotshot Mar 16 '22 at 14:17
  • It means you're not using it correctly. This API is asynchronous, see the examples. – wOxxOm Mar 16 '22 at 14:25
  • chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { chrome.storage.local.set({requestValue:request.message},()=>{ console.log("Stored: " + request.message) sendResponse({processStatus:"Success",message:"yay!"}) }); return true; }); – Quick Hotshot Mar 16 '22 at 14:45
  • The code is correct. See [Inspect extension's chrome.storage in devtools](https://stackoverflow.com/q/32435399) – wOxxOm Mar 16 '22 at 14:46
  • This code is valid right? I have used the API docs snippets and coded. But the application->local storage of the service worker is empty. no value is getting stored, but the callback of the "set" function is getting called. – Quick Hotshot Mar 16 '22 at 14:47
  • As I wrote in the comment, see [Inspect extension's chrome.storage in devtools](https://stackoverflow.com/q/32435399). – wOxxOm Mar 16 '22 at 14:48

0 Answers0