how to add event listener to cookie so that to notify when a cookie gets updated or expired

Viewed 7663

I want to add an event listener to Cookie. My objective is to call an function, once one of the cookies get expired.

I tried the following given in mozilla:-

 browser.cookies.onChanged.addListener(listener)
 browser.cookies.onChanged.removeListener(listener)
 browser.cookies.onChanged.hasListener(listener)

but its not working in chrome browser.

I seacrhed google but could not get any help

Any help in this regard as how to add event listener to cookie

1 Answers

Right now, there is no API for watching the cookies. This browser.cookies.onChanged that you refer to is only for browser extension https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies/onChanged.

You can use the following npm package https://www.npmjs.com/package/@fcannizzaro/react-use-cookie-watcher.

If you check the implementation, it just adds a setInterval which reads cookies and waits for changes https://github.com/fcannizzaro/react-use-cookie-watcher/blob/master/src/index.js#L7-L18.

So you can do it on your own if you want

Related