2

The chrome.cookies.set API receive url as one of its parameter, from which cookie's domain and path is calculated.

For example, if url's value is https://stackoverflow.com, then domain is stackoverflow.com, while path is /.

The issue is, how to set a cookie with stackoverflow's subdomain: .stackoverflow.com. In other words, we want to have a cookie whose domain value is .stackoverflow.com

I've tried two approaches, none of them work.

  1. { url: https//*.stackoverflow.com }
  2. { url: https//.stackoverflow.com }
  • Specify `domain: 'stackoverflow.com', url: 'https://stackoverflow.com'`. – wOxxOm Jun 20 '20 at 07:59
  • Doesn't work @wOxxOm, The current cookie won't be edited, but A new cookie whose domain is `stackoverflow.com` will be inserted. This is not the desired behavior. :( – Cảnh Toàn Nguyễn Jun 20 '20 at 11:58
  • Delete the current cookie and set the new one? – wOxxOm Jun 20 '20 at 12:01
  • Yes I think about this strategy. But somehow can't set `domain` value to be a subdomain `.stackoverflow.com`. Its value is always `stackoverflow.com`. – Cảnh Toàn Nguyễn Jun 20 '20 at 12:09
  • 1
    There's no need for the leading dot when you use `domain`. It already applies to all sub-domains. – wOxxOm Jun 20 '20 at 12:16
  • Yes it does. I found the stackoverflow's related topic here. Posting this for future reference. https://stackoverflow.com/questions/18492576/share-cookie-between-subdomain-and-domain – Cảnh Toàn Nguyễn Jun 20 '20 at 12:21
  • CảnhToànNguyễn have you been able to set the cookie from Chrome extension? `domain` param is not documented in the API. And it does not work for me in Chrome v91 – user1635430 Jul 03 '21 at 15:07

2 Answers2

0

What you mean is actually setting a cookie for all subdomains if you want to set .stackoverflow.com.

From my experience the API method chrome.cookies.set will set cookies for both: domain and all subdomains, when you specify the domain:

chrome.cookies.set({ 
  url: 'https//stackoverflow.com/?sth', 
  domain: 'stackoverflow.com', 
  name: 'some_cookie', 
  value: 'cookie_value' 
})

Will result in two cookies created for the page:

  1. some_cookie:cookie_value:stackoverflow.com
  2. some_cookie:cookie_value:.stackoverflow.com

To set only a cookie for the main domain you should set URL and let Chrome set the domain based on that.

0

You can use { url: "https//stackoverflow.com", domain: ".stackoverflow.com" }

Tested on Google Chrome version 107.0.5304.88

if you omit the domain, then the domain will be host-only like stackoverflow.com, and if you specified the domain, the domain will always be a subdomain like .stackoverflow.com

see https://developer.chrome.com/docs/extensions/reference/cookies/#method-set

We can distinguish between domain and subdomain by the domain field in chrome.cookies.set, but we can't distinguish them in chrome.cookies.remove, because it only accept url name and storageId but no domain field. If we use chrome.cookies.remove, it will remove both domain and subdomain.

So if we want to remove only one of them, we can use chrome.cookies.set like this:

chrome.cookies.set({ ...cookie, domain: cookie.domain.startsWith('.') ? domain : undefined, expirationDate: 0 })
Chosan
  • 539
  • 4
  • 8