Only play notification sounds once over all tabs

pull/9917/head
Benedikt Werner 2021-10-03 08:17:37 +02:00
parent e5806be7e7
commit 734aff8642
No known key found for this signature in database
GPG Key ID: 1DBFF0F8E9E121EB
4 changed files with 19 additions and 6 deletions

View File

@ -105,6 +105,7 @@ interface SoundI {
loadOggOrMp3(name: string, path: string): void;
loadStandard(name: string, soundSet?: string): void;
play(name: string, volume?: number): void;
playOnce(name: string): void;
getVolume(): number;
setVolume(v: number): void;
speech(v?: boolean): boolean;

View File

@ -24,10 +24,10 @@ export default function (opts: ChallengeOpts, data: ChallengeData, redraw: () =>
if (lichess.once('c-' + c.id)) {
if (!lichess.quietMode && data.in.length <= 3) {
opts.show();
lichess.sound.play('newChallenge');
lichess.sound.playOnce('newChallenge');
}
const pushSubsribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
!pushSubsribed && c.challenger && notify(showUser(c.challenger) + ' challenges you!');
const pushSubscribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
if (!pushSubscribed && c.challenger) notify(showUser(c.challenger) + ' challenges you!');
opts.pulse();
}
});

View File

@ -38,10 +38,10 @@ export default function makeCtrl(opts: NotifyOpts, redraw: Redraw): Ctrl {
const notif = data.pager.currentPageResults.find(n => !n.read);
if (!notif) return;
opts.pulse();
if (!lichess.quietMode || notif.content.user.id == 'lichess') lichess.sound.play('newPM');
if (!lichess.quietMode || notif.content.user.id == 'lichess') lichess.sound.playOnce('newPM');
const text = asText(notif, lichess.trans(data.i18n));
const pushSubsribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
if (!pushSubsribed && text) notify(text);
const pushSubscribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
if (!pushSubscribed && text) notify(text);
}
const loadPage = (page: number) =>

View File

@ -71,6 +71,18 @@ const sound: SoundI = new (class {
else doPlay();
}
playOnce(name: string): void {
// increase chances that the first tab can put a local storage lock
const doIt = () => {
const storage = lichess.storage.make('just-played');
if (Date.now() - parseInt(storage.get()!, 10) < 1000) return;
storage.set('' + Date.now());
this.play(name);
};
if (document.hasFocus()) doIt();
else setTimeout(doIt, 10 + Math.random() * 500);
}
setVolume = this.volumeStorage.set;
getVolume = () => {