fancy
fancy

Reputation: 51383

Trigger an action when document.cookie changes?

I need to update localStorage when document.cookie changes. Is there any way to set a listener, overwrite a prototype to act as middleware or some other pattern that would result in the ability to trigger a function on change? I'm trying to avoid something like polling on an interval.

Thanks for any ideas.

Upvotes: 10

Views: 6780

Answers (2)

Matteo Boscolo
Matteo Boscolo

Reputation: 708

Try with cookies.onChanged

browser.cookies.onChanged.addListener(function(changeInfo) {
  console.log('Cookie changed: ' +
              '\n * Cookie: ' + JSON.stringify(changeInfo.cookie) +
              '\n * Cause: ' + changeInfo.cause +
              '\n * Removed: ' + changeInfo.removed);
});

Beware of browser compatibility.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies/onChanged

Upvotes: -2

smassey
smassey

Reputation: 5931

Theres no avoiding it, these events simply dont exist, you'll need to poll.

Upvotes: 3

Related Questions