From 2ac5f693b4877f54c70b9316ccc6da0e16991a41 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sat, 15 May 2021 15:19:00 -0500 Subject: [PATCH] Autoplay videos when allowed --- helpers/lite-youtube.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/helpers/lite-youtube.js b/helpers/lite-youtube.js index 52f5cee..8151b8e 100644 --- a/helpers/lite-youtube.js +++ b/helpers/lite-youtube.js @@ -49,6 +49,18 @@ class LiteYTEmbed extends HTMLElement { this.playerPoster.addEventListener('click', e => this.addIframe()) + // Mounted + + this.detectAutoplay() + .then( ({ willAutoplay }) => { + console.log('willAutoplay', willAutoplay) + + // If we're allowed to autoplay + // then start loading the player + if ( willAutoplay === true ) { + this.startPlayerLoad() + } + }) } // // TODO: Support the the user changing the [videoid] attribute @@ -210,11 +222,11 @@ class LiteYTEmbed extends HTMLElement { async detectAutoplay () { - if ( !process.client ) return { willAutoplay: false } + // if ( !process.client ) return { willAutoplay: false } - const { default: canAutoPlay } = await import('can-autoplay') + // const { default: canAutoPlay } = await import('can-autoplay') - const willAutoplay = await canAutoPlay.video() + const willAutoplay = await canAutoplay.video() // const willAutoplayMuted = await canAutoPlay.video({ muted: true, inline: true }) return {