hu7sy
hu7sy

Reputation: 961

how can i pass variable to chrome.storage.sync.get()

i want to pass value to chrome.storage.sync.get() function.

chrome.storage.sync.get('privateKey', function(data, e) {
  if (data.privateKey) {
    var decrypt = new JSEncrypt();
    decrypt.setPrivateKey(data.privateKey);
    var uncrypted = decrypt.decrypt(e.detail.encryptedVal)
    alert(uncrypted);
  } else {
    alert("key is not set");
  }
 });

but e.detail.encryptedVal is showing me as undefined.

Upvotes: 0

Views: 1304

Answers (2)

Xan
Xan

Reputation: 77482

The callback of .get() expects exactly 1 parameter.

By passing a function that takes 2 parameters (i.e. function(data, e) {...}), you do the following:

  1. The function is called with one parameter. It is assigned to a callback-local variable data.
  2. The second parameter stays undefined. It is assigned to a callback-local variable e.
  3. If there was a variable e in the outer scope, it is no longer accessible.

I assume the part 3 is exactly your problem. You had a variable e in the scope where you called .get(), but you made it inaccessible.

Generally, due to the concept called closures, you don't actually need to pass e inside the scope - you just use the variable from the outer scope, and it will be retained in memory until the function executes. Think of it as a "locally global" variable, if that makes any sense. If it doesn't, there are better explanations.

With that in mind:

chrome.storage.sync.get('privateKey', function(data) { // just don't mention e here
  /* ... */
    // Just use e inside if it comes from outer scope
    var uncrypted = decrypt.decrypt(e.detail.encryptedVal);
  /* ... */
});

Better yet, let's make that into a function:

function decryptValue(value, callback) {
  chrome.storage.sync.get('privateKey', function(data) {
    var decrypt = new JSEncrypt();
    decrypt.setPrivateKey(data.privateKey);
    var decrypted = decrypt.decrypt(value);
    callback(decrypted);
  }
}

/* ... */
decryptValue(e.detail.encryptedVal, function(decrypted) {
  // Do something
});
/* ... */

Note that callback variable? While you can use decrypted inside the callback of .get(), due to the fact it's asynchronous you can't use it outside. There is a very good overview of the problem, and another one here. Basically, .get() is asynchronous so you HAVE to use a callback.

/* ... */
decryptValue(e.detail.encryptedVal, function(decrypted) {
  // Do something with decrypted
});
// Here, decrypted is not yet computed
/* ... */

Most of Chrome API is asynchronous. I would recommend to read You Don't Know JS book on the topic.

Upvotes: 2

Transcendental
Transcendental

Reputation: 981

So, from the comments bellow, I suppose you'd like to pass the value to get callback in order to user it in the callback. Unfortunately, this can't be done the way you want it to be done. But, what you can do is write a decrypt function that will take the encryptedVal as an argument, and simply use it

function decrypt(encryptedVal) {
    chrome.storage.sync.get('privateKey', function (data) {
        if (data.privateKey) {
            var decrypt = new JSEncrypt();
            decrypt.setPrivateKey(data.privateKey);
            var uncrypted = decrypt.decrypt(encryptedVal)
                alert(uncrypted);
        } else {
            alert("key is not set");
        }
    });
}

Do note that this function uses asynchronous code and you may not return from it as you'd expect.

Upvotes: 1

Related Questions