Autoplay embedded videos after the play button is clicked

This commit is contained in:
Alex Gleason 2023-01-17 13:03:37 -06:00
parent 988d466fcd
commit ed8299892c
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 27 additions and 25 deletions

View file

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
### Fixed ### Fixed
- Posts: don't have to click the play button twice for embedded videos.
### Removed ### Removed
- Admin: single user mode. Now the homepage can be redirected to any URL. - Admin: single user mode. Now the homepage can be redirected to any URL.

View file

@ -5,7 +5,6 @@ import React, { useState, useEffect } from 'react';
import Blurhash from 'soapbox/components/blurhash'; import Blurhash from 'soapbox/components/blurhash';
import Icon from 'soapbox/components/icon'; import Icon from 'soapbox/components/icon';
import { HStack, Stack, Text } from 'soapbox/components/ui'; import { HStack, Stack, Text } from 'soapbox/components/ui';
import { useSettings } from 'soapbox/hooks';
import { normalizeAttachment } from 'soapbox/normalizers'; import { normalizeAttachment } from 'soapbox/normalizers';
import { addAutoPlay } from 'soapbox/utils/media'; import { addAutoPlay } from 'soapbox/utils/media';
@ -42,9 +41,6 @@ const Card: React.FC<ICard> = ({
onOpenMedia, onOpenMedia,
horizontal, horizontal,
}): JSX.Element => { }): JSX.Element => {
const settings = useSettings();
const shouldAutoPlayVideo = settings.get('autoPlayVideo');
const [width, setWidth] = useState(defaultWidth); const [width, setWidth] = useState(defaultWidth);
const [embedded, setEmbedded] = useState(false); const [embedded, setEmbedded] = useState(false);
@ -92,7 +88,7 @@ const Card: React.FC<ICard> = ({
}; };
const renderVideo = () => { const renderVideo = () => {
const content = { __html: shouldAutoPlayVideo ? addAutoPlay(card.html) : card.html }; const content = { __html: addAutoPlay(card.html) };
const ratio = getRatio(card); const ratio = getRatio(card);
const height = width / ratio; const height = width / ratio;

View file

@ -57,7 +57,9 @@ enum VideoProviders {
RUMBLE = 'rumble.com' RUMBLE = 'rumble.com'
} }
/** Try adding autoplay to an iframe embed for platforms such as YouTube. */
const addAutoPlay = (html: string): string => { const addAutoPlay = (html: string): string => {
try {
const document = domParser.parseFromString(html, 'text/html').documentElement; const document = domParser.parseFromString(html, 'text/html').documentElement;
const iframe = document.querySelector('iframe'); const iframe = document.querySelector('iframe');
@ -80,6 +82,9 @@ const addAutoPlay = (html: string): string => {
// so we need to get innerHTML out of the body and not the entire document // so we need to get innerHTML out of the body and not the entire document
return (document.querySelector('body') as HTMLBodyElement).innerHTML; return (document.querySelector('body') as HTMLBodyElement).innerHTML;
} }
} catch (e) {
return html;
}
return html; return html;
}; };