Skip to content

Commit

Permalink
disable autoplay on phone
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Nov 7, 2023
1 parent ecdfe8c commit 1718207
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions routers/root.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import os.path
import re
import subprocess
import tempfile
import typing
Expand Down Expand Up @@ -39,6 +40,7 @@

DEFAULT_LOGIN_REDIRECT = "/explore/"
DEFAULT_LOGOUT_REDIRECT = "/"
MOBILE_UA_PATTERN = re.compile(r"(Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini)", re.MULTILINE)


@app.get("/sitemap.xml/")
Expand Down Expand Up @@ -336,3 +338,23 @@ def page_wrapper(request: Request, render_fn: typing.Callable, **kwargs):

st.html(templates.get_template("footer.html").render(**context))
st.html(templates.get_template("login_scripts.html").render(**context))
if is_mobile(request):
render_on_mobile_only()


def render_on_mobile_only() -> str:
# disable video autoplay on mobile
st.html("""
<script>
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll("video").forEach(function(video) {
video.autoplay = false;
});
});
</script>
""")


def is_mobile(request: Request) -> bool:
user_agent = request.headers.get("user-agent", "")
return bool(MOBILE_UA_PATTERN.search(user_agent))

0 comments on commit 1718207

Please sign in to comment.