In a Google Chrome Extension, I want to use chrome.storage.local
(as opposed to localStorage) because:
storage.set
can trigger an event listenerUsing storage.set
, how can I have a variable key name?
Note: If I don't use the setter, I can do storage[v1]
, but changes to the object won't trigger the event listener.
var storage = chrome.storage.local; var v1 = 'k1'; storage.set({v1:'s1'}); storage.get(v1,function(result){ console.log(v1,result); //console output = k1 {} }); storage.get('v1',function(result){ console.log(result); //console output = {v1:'s1'} });
Just go to the developer tools by pressing F12 , then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there. In Chrome version 65, you can manually modify and add new items.
# View localStorage keys and valuesClick the Application tab to open the Application panel. Expand the Local Storage menu. Click a domain to view its key-value pairs. Click a row of the table to view the value in the viewer below the table.
Therefore, if you're accessing localStorage from your contentscript, it will be the storage from that webpage, not the extension page storage. Now, to let your content script to read your extension storage (where you set them from your options page), you need to use extension message passing. chrome.
The storage space contains cache, cookies, files, images, etc. from the website that are locally downloaded.
Is this what you where looking for?
var storage = chrome.storage.local; var v1 = 'k1'; var obj= {}; obj[v1] = 's1'; storage.set(obj); storage.get(v1,function(result){ console.log(v1,result); //console output = k1 {v1:'s1'} }); storage.get('v1',function(result){ console.log(result); //console output = {v1:'s1'} })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With