Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get local storage in chrome extension #23

Closed
sachin-workspace opened this issue May 6, 2020 · 1 comment
Closed

How to get local storage in chrome extension #23

sachin-workspace opened this issue May 6, 2020 · 1 comment
Labels
question Further information is requested

Comments

@sachin-workspace
Copy link

i am trying to access local storage using chrome api but not able to get the values. can u show me how to access it

@satendra02
Copy link
Owner

You must declare the storage permission in the extension manifest to use the storage API.

{  
   "name": "My extension",
   ...
   "permissions": [
      "storage"
    ],
   ...
}

To store user data for your extension, you can use either storage.sync or storage.local

Keep in mind that storing and fetching data in chrome storage is an asynchronous process so you must wait for the callback function to execute your code synchronously.

/*global chrome*/

// Setting data func
function setDataToStorage(data, callback){
   chrome.storage.sync.set(data, callback);
}

// Getting data func
function getDataFromStorage(key, callback){
   chrome.storage.sync.get(key, callback);
}

// Calling set data function .
let data = {"id": "1234", "name": "Jhon", "age": 24};

setDataToStorage(data, function(){
   console.log("Data saved successfully!");

   // Calling get data function.
   getDataFromStorage(["name",  "age"], function(result){
        console.log('Name: ' + result.name, 'Age: ' + result.age);
   })
})

Note: The above example is to just show you how to store data and fetch it.
I am calling getDataFromStorage function inside setDataToStorage callback function to make sure we will get the output. You can use it independently as per your use case.

Make sure to use /*global chrome*/ at top of your file to use any chrome API.

Also, read the official docs for more information.

I hope it helps. Let me know if you face any difficulty using it.

@satendra02 satendra02 added the question Further information is requested label May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants