Hi, everyone! In November I created https://sv.wikipedia.org/wiki/MediaWiki:Gadget-LiveNotifications.js which shows the number of unread notifications in parens in the title, just like on Phabricator. Unfortunately, I quickly had to remove this (https://sv.wikipedia.org/w/index.php?title=MediaWiki:Gadgets-definition&diff=42079218) because I realized that if someone has 20 Wikipedia tabs open, they will send 20 API calls. How do I prevent this?
Send only one API call regardless of number of tabs


Using a shared web worker, for example.

You could set a flag that a request was made in a specific interval. This flag must be set on a location that is available from all tabs. The localStoreage[1] as well as cookies should be shared between tabs
[1] https://developer.mozilla.org/de/docs/Web/API/Window/localStorage

For cases like this, SessionStorage is probably preferred over LocalStorage, as you don’t need the persistence of LocalStorage. When either of those are used, please do remember to wrap all calls to them in try/catch, as they might not always be available, for a variety of reasons.
A shared web worker can of course work too, but browser support for that is a bit more limited.

Actually, SessionStorage is scoped to the current tab/window, so it won’t work as you think. If you have several tabs open, each one will have their own SessionStorage instance, without a way to see data from each other, even if they’re on the same domain.

Oh you are absolutely right. I forgot they added that restriction to session storage.