最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add event listener to cookie so that to notify when a cookie gets updated or expired - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Sep 22, 2017 at 8:45 newbieeeeeeee asked Sep 22, 2017 at 8:42 newbieeeeeeeenewbieeeeeeee 1311 gold badge1 silver badge4 bronze badges 6
  • 1 developer.mozilla/en-US/Add-ons/WebExtensions/API/cookies/… says this works in Chrome, FF, and Opera. – Barmar Commented Sep 22, 2017 at 8:45
  • 2 browser object itself is not available in chrome console. – newbieeeeeeee Commented Sep 22, 2017 at 8:46
  • 5 I think this is only for use in extensions. The documentation is in the Add-ons section. – Barmar Commented Sep 22, 2017 at 8:49
  • hey any idea? how to add it in plain javascript. down with this issue since last two days – newbieeeeeeee Commented Sep 22, 2017 at 9:02
  • @newbieeeeeeee: there isn't a way to use it in plain JS; write your own hammer-based event launcher. – dandavis Commented Sep 22, 2017 at 9:13
 |  Show 1 more ment

1 Answer 1

Reset to default 12

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/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies/onChanged.

You can use the following npm package https://www.npmjs./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./fcannizzaro/react-use-cookie-watcher/blob/9281ec0a5b6fbcbafd4589f0ea32d2b59bec32b3/src/index.tsx#L31.

So you can do it on your own if you want

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论