When we manipulate cookies, we usedocument.cookie
, or use third-party libraries to perform operations on cookies, such asjs-cookie
Well, why isn’t there an API that can directly fetch or manipulate cookies?
But now you can have oneAPI
To operatecookie
That’s itcookieStore
About cookieStore
CookieStore is an asynchronous API, which is quite convenient for operating cookies. There are few documents for this API, and there is no Chinese document on MDN
To obtain
Get a Cookie
await cookieStore.get({name:"xxx"})
Copy the code
Gets all cookies under the domain
await cookieStore.getAll({ domain: '.baidu.com' })
Copy the code
Set up the
To set cookies we can use cookiestore.set ()
await cookieStore.set({ name: 'YushengSenior'.value: true.domain: 'baidu.com' })
/ / or
await cookieStore.set('haha'.123321)
Copy the code
delete
Before we want to delete a cookie, we need to set its expiration time to invalidate the cookie. Now we can use the following method to delete a specified cookie
await cookieStore.delete('haha')
/ / or
await cookieStore.delete({ name: 'haha' })
Copy the code
Listening for cookie changes
We can listen for a specific cookie and do something when the cookie is changed or deleted
cookieStore.addEventListener('change'.event= > {console.log(event.changed, event.delete)})
Copy the code
Reference Documents:
Chromium API: Developer.chrome.com/docs/extens…
CookieStore: Developer.mozilla.org/en-US/docs/…
Http Cookies: Developer.mozilla.org/zh-CN/docs/…
Cookie Store API: Wicg. Making. IO/cookie – stor…