Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome Extension : Set persistent cookie in chrome extension?

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 the current browser when I close the browser data was lost so that I go 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 anything I have missed here.

like image 917
Yashwant Kumar Sahu Avatar asked Aug 16 '11 13:08

Yashwant Kumar Sahu


People also ask

Can Chrome Extensions store cookies?

If you want to store cookie in a background script/ popup script, then you can definitely do it. But that cookie will be saved for the domain of your background script which is essentially your chrome extension id.

How do I add cookies to Chrome extensions?

The cookies can be exported by clicking on the 'blue' download button or by clicking on 'Get cookies. txt' in the Chrome context menu on any website.

Can extensions access cookies?

With the Cookies API your extensions have access to capabilities similar to those used by websites to store and read cookies. The API's features give extensions the ability to store information on a site-by-site basis.


1 Answers

It seems that your expiration date is 1 Jan 1970 01:00 (3600 equals 1 hour after UNIX epoch). So of course your cookie will be deleted because it's expiration date is set to the past.

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

To set a cookie relative to the current time you need to add seconds to (new Date().getTime() / 1000) as @pickled suggested.

like image 145
useraged Avatar answered Oct 06 '22 13:10

useraged