Chrome extension: set persistent cookie in chrome extension?

Here I work with the chrome extension here. I need to set a cookie value by my extension.

I set cookies:

chrome.cookies.set({ url: "http://example.com/", name: "CookieVar", value: "123" }); 

But it is available in the current browser, when I close the browser data, it has been lost, so I go with

 chrome.cookies.set({ url: "http://example.com/", name: "CookieVar", value: "123", expirationDate: 3600 }); 

But from this, I cannot see the cookie information - this is what I missed here.

thanks for the help

+8
google-chrome google-chrome-extension
source share
2 answers

It seems that your expiration date is 1 Jan 1970 00:01 (1 means 1 second after the UNIX era). Therefore, of course, your cookie will be deleted.

You need to provide an appropriate expiration time for your cookie. In the documentation , expirationDate is defined as:

Cookie Expiration Date as the number of seconds since the UNIX Epoch

+5
source share

If you do not set a value for expirationDate, then the cookie will expire when the user closes the browser.

If you set the value, it should be the current time + how many seconds until it expires. For example:

 {expirationDate: (new Date().getTime()/1000) + 3600} 

will set it as the current time, plus 3600 seconds, so an hour in the future.

You installed it as 3600 in basic UNIX time, which is the beginning of 1970, so it immediately expired.

+6
source share

All Articles