Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reach variables in a chrome extension popup localStorage from content scripts?

I have stored some options in my chrome extension. Using localStorage.setItem().

But when I want to use them on a page, it's not working. I know it's because they are totally in different domains, but how can I reach the settings in my chrome extension domain from any other domain (in my page scripts).

Thanks,

like image 244
AGamePlayer Avatar asked Jan 29 '23 14:01

AGamePlayer


1 Answers

You could use the chrome.storage API.

To save something locally:

chrome.storage.sync.set({"key":"value"});

To retrieve a value:

chrome.storage.sync.get("key",function(res) {
console.log(res[key])
});

If you don't want the storage to sync across the user's account, you can use chrome.storage.local instead of chrome.storage.sync

Documentation

like image 199
Michael Avatar answered Feb 16 '23 01:02

Michael