Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.storage.local.set using a variable key name

In a Google Chrome Extension, I want to use chrome.storage.local (as opposed to localStorage) because:

  1. With key-value pairs, the value can be any object (as opposed to string only)
  2. Changes to the data model using setter storage.set can trigger an event listener

Using 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'} }); 
like image 626
user1558225 Avatar asked Jul 27 '12 17:07

user1558225


People also ask

How do I change local storage in Chrome?

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.

How do I find local storage value in Chrome?

# 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.

Can Chrome extensions use local storage?

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.

What is Chrome storage data?

The storage space contains cache, cookies, files, images, etc. from the website that are locally downloaded.


1 Answers

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'} }) 
like image 103
PAEz Avatar answered Oct 04 '22 14:10

PAEz