diff --git a/config/defaults.ts b/config/defaults.ts index 0108db0e9f..fa3a929dbc 100644 --- a/config/defaults.ts +++ b/config/defaults.ts @@ -113,6 +113,9 @@ const defaultConfig = { 'lyric-genius': { romanizedLyrics: false, }, + 'disable-autoplay': { + applyOnce: false, + }, 'discord': { enabled: false, autoReconnect: true, // If enabled, will try to reconnect to discord every 5 seconds after disconnecting or failing to connect diff --git a/plugins/disable-autoplay/front.ts b/plugins/disable-autoplay/front.ts index 176c6bcf8b..0af23a4da8 100644 --- a/plugins/disable-autoplay/front.ts +++ b/plugins/disable-autoplay/front.ts @@ -1,4 +1,6 @@ -export default () => { +import type { ConfigType } from '../../config/dynamic'; + +export default (options: ConfigType<'disable-autoplay'>) => { const timeUpdateListener = (e: Event) => { if (e.target instanceof HTMLVideoElement) { e.target.pause(); @@ -6,13 +8,16 @@ export default () => { }; document.addEventListener('apiLoaded', (apiEvent) => { - apiEvent.detail.addEventListener('videodatachange', (name: string) => { + const eventListener = (name: string) => { + if (options.applyOnce) { + apiEvent.detail.removeEventListener('videodatachange', eventListener); + } + if (name === 'dataloaded') { apiEvent.detail.pauseVideo(); - document.querySelector('video')?.addEventListener('timeupdate', timeUpdateListener); - } else { - document.querySelector('video')?.removeEventListener('timeupdate', timeUpdateListener); + document.querySelector('video')?.addEventListener('timeupdate', timeUpdateListener, { once: true }); } - }); + }; + apiEvent.detail.addEventListener('videodatachange', eventListener); }, { once: true, passive: true }); }; diff --git a/plugins/disable-autoplay/menu.ts b/plugins/disable-autoplay/menu.ts new file mode 100644 index 0000000000..44575929da --- /dev/null +++ b/plugins/disable-autoplay/menu.ts @@ -0,0 +1,20 @@ +import { BrowserWindow } from 'electron'; + +import { setMenuOptions } from '../../config/plugins'; + +import { MenuTemplate } from '../../menu'; + +import type { ConfigType } from '../../config/dynamic'; + +export default (_: BrowserWindow, options: ConfigType<'disable-autoplay'>): MenuTemplate => [ + { + label: 'Applies only on startup', + type: 'checkbox', + checked: options.applyOnce, + click() { + setMenuOptions('disable-autoplay', { + applyOnce: !options.applyOnce, + }); + } + } +];