chrome Extension : Set persistent cookie in chrome

2020-06-12 04:05发布

Here I am working with chrome extension development here I need to set cookie value by my extension.

I have set cookies by:

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

But it available in current browser when I close browser data was lost so that I going with

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

But from this I am not able to see cookie information is any thing I have missed here..

Advance thanks

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-06-12 05:05

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

You need to provide appropriate expirationDate for your cookie. In documentation, expirationDate defined as:

The expiration date of the cookie as the number of seconds since the UNIX epoch

查看更多
混吃等死
3楼-- · 2020-06-12 05:10

If you don't set a value for expirationDate then the cookie will expire when the user closes the browser.

If you do set a value then it must be the current time + how many seconds until it expires. For example:

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

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

You were setting it as 3600 past the base UNIX time, which is the start of 1970, so it immediately expired.

查看更多
登录 后发表回答