From b9c464b7a1e26660c58d273c655e747719aa4d80 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 1 Feb 2025 16:03:27 +0530 Subject: [PATCH] work around browser playback restrictions --- content/templates/base.html | 38 ++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/content/templates/base.html b/content/templates/base.html index b31f901..f4fca86 100644 --- a/content/templates/base.html +++ b/content/templates/base.html @@ -9,7 +9,7 @@ CAMP {% endblock %} - + @@ -117,6 +117,42 @@ }); } $(document).ready(loadSlideshow); + var restrictedElements = []; + function removeBehaviorsRestrictions(event) { + if (restrictedElements.length > 0) { + var rElements = restrictedElements; + restrictedElements = []; + rElements.forEach(function(video) { + if (video.autoplay && video.paused) { + video.load() + video.play() + if (video.paused) { + restrictedElements.push(video) + } + } else if (video.readyState < 4) { + video.load() + } + }); + } + } + + if (document.querySelector('video')) { + let video = document.createElement('video'); + video.muted = true + video.play(); + if (video.paused) { + restrictedElements = document.querySelectorAll('video.gif') + window.addEventListener('keydown', removeBehaviorsRestrictions); + window.addEventListener('mousedown', removeBehaviorsRestrictions); + window.addEventListener('touchstart', removeBehaviorsRestrictions); + restrictedElements.forEach(video => { + if (!video.poster) { + video.poster = video.src.replace('.mp4', '.jpg') + video.muted = true + } + }) + } + } {% block end %} {% endblock %}