From 0cf45db39df8376e8ec700d34429d56fe203af8c Mon Sep 17 00:00:00 2001 From: Souma <33572696+Souma-Sumire@users.noreply.github.com> Date: Sat, 16 Sep 2023 00:44:58 +0800 Subject: [PATCH] raidboss: prevent duplicate button creation (#5794) created only once, rather than multiple times. --- ui/raidboss/autoplay_helper.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ui/raidboss/autoplay_helper.ts b/ui/raidboss/autoplay_helper.ts index 9a7cff5c1b..920f05da3e 100644 --- a/ui/raidboss/autoplay_helper.ts +++ b/ui/raidboss/autoplay_helper.ts @@ -1,16 +1,27 @@ export default class AutoplayHelper { + private static context?: AudioContext; + private static isButtonCreated = false; + + private static getContext(): AudioContext { + AutoplayHelper.context ??= new AudioContext(); + return AutoplayHelper.context; + } + static CheckIfAlreadyRunning(): boolean { // This check will only ever succeed on running Chromium passing // --autoplay-policy=no-user-gesture-required // as command line argument or configuring CEF the correct way. // Once https://bugs.chromium.org/p/chromium/issues/detail?id=1106380 // is fixed this function will return false on every (up-to-date) browser - const context = new AudioContext(); + const context = AutoplayHelper.getContext(); return context.state === 'running'; } static Prompt(): void { - const context = new AudioContext(); + if (AutoplayHelper.isButtonCreated) { + return; + } + const context = AutoplayHelper.getContext(); const button = document.createElement('button'); button.innerText = 'Click to enable audio'; button.classList.add('autoplay-helper-button'); @@ -21,6 +32,7 @@ export default class AutoplayHelper { button.remove(); }; document.body.appendChild(button); + AutoplayHelper.isButtonCreated = true; } static CheckAndPrompt(): void {