0

I want logged in user's token information in extension. For that i require token of that user stored in localstorage.

content-script.js

chrome.runtime.sendMessage({
      auth: JSON.parse(localStorage.getItem("vuex"))["auth"]
    });

background.js

try {

  self.importScripts('content-script.js');
  chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
 
  if (request.auth) {
   localStorage.setItem("auth", JSON.stringify(request.auth));
  }
}
catch (e) {
 console.error(e);
}

Manifest.json

 {
 "manifest_version": 3,
 "name": "LinkJoy Browser Extension",
 "description": "Shorten long urls in just one click",
 "default_locale": "en",
 "permissions": [
   "activeTab",
   "storage"
 ],
 "host_permissions": ["http://localhost:9000/*"],
 "icons": {
   "16": "icons/linkjoy-logo.png",
   "48": "icons/linkjoy-logo.png",
   "128": "icons/linkjoy-logo.png"
 },
"background": {
   "service_worker": "js/background.js"
},
"content_scripts": [
{
  "matches": [
    "http://localhost:9000/*"
  ],
  "js": [
    "js/content-script.js"
  ]
 }
],
"action": {
  "default_popup": "popup.html",
  "default_title": "__MSG_extName__",
  "default_icon": {
  "19": "icons/linkjoy-logo.png",
  "38": "icons/linkjoy-logo.png"
  }
}

}

Similarly, I have tried using chrome.local.storage api also. But still I am getting error 'localStorage' is not defined.

  • 1
    There's no `localStorage` in the MV3 background script. You can switch to chrome.storage.local. It's a different asynchronous storage, so its usage is different, look for examples. – wOxxOm May 02 '22 at 10:01
  • I tried chrome.storage.local also. For example in above code I have tried something like this in content-script.js chrome.runtime.sendMessage({ auth: JSON.parse(chrome.storage.local.get("vuex"))["auth"] }); background.js - code same as above - chrome.storage.local.set("auth", JSON.stringify(request.auth)); – Krishna Kanabar May 02 '22 at 12:17
  • This is not how this API is used. Look for examples first. – wOxxOm May 02 '22 at 12:18
  • chrome.storage.local.get(['vuex'], function (result) { console.log("local", result.vuex); }); I tried like this also as per given examples. Here in output it is showing null but localstorage data has format like vuex : {"auth" : {"user" : {"first_name":.......}}} – Krishna Kanabar May 02 '22 at 12:33
  • I want to get localstorage data of website in content-script and pass that data in background.js so that I can get localstorage data of website in extension. Get will be required in content-script and Set will be used in background.js – Krishna Kanabar May 02 '22 at 12:49
  • You're not using the examples. The correct usage is chrome.storage.local.set({auth: 'value'}) for writing. And you need to keep `localStorage` in the content script because as you said you want to read the site's data. – wOxxOm May 02 '22 at 12:52
  • @wOxxOm As you said I tried -- content-script.js - chrome.runtime.sendMessage({ auth: JSON.parse(localStorage.getItem("vuex"))["auth"]} and inside backgound.js I wrote ---chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { chrome.storage.local.set({ "auth": JSON.stringify(request.auth) }); }); But I am not getting auth data in backgound.js file....request.auth is coming as null. What am I doing wrong? – Krishna Kanabar May 02 '22 at 15:26
  • Use [devtools for background.js](/a/10258029) and for the [content script](https://stackoverflow.com/q/38913799) to debug your code. – wOxxOm May 02 '22 at 16:01

0 Answers0