Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 366
newbieeeeeeee Avatar asked Sep 22 '17 08:09

newbieeeeeeee


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 😉

like image 84
mpospelov Avatar answered Nov 03 '22 23:11

mpospelov