Ilia Dementiev
Ilia Dementiev

Reputation: 17

Set chrome extension cookie value name as var

I'm working on chrome extension and need to set a cookie:

chrome.cookies.set({ url: "https://www.someurl.com/", name: value });

This code works good but "name" should be dynamic var that ajax passed.

var name = response;

Is it posible?

Upvotes: 0

Views: 74

Answers (1)

Xan
Xan

Reputation: 77551

You're using it wrong.

It should be:

chrome.cookies.set({
  url: "https://www.someurl.com/",
  name: someName,
  value: someValue
});

So you can set both as variables.


However, in a general case (say, chrome.storage API that really takes name-value maps), you could do the following:

var data = {};
data[name] = value; // Both are variables
chrome.storage.local.set(data);

When ECMAScript 6 support comes along, you'll be able to use Computed property names:

// Does not work yet
chrome.storage.local.set({[name]: value});

We won't have to wait long though, it's coming in Chrome 44.

Upvotes: 1

Related Questions