nicolium: allow youtube embed start time
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -15,7 +15,6 @@ import Icon from '@/components/ui/icon';
|
||||
import Stack from '@/components/ui/stack';
|
||||
import Text from '@/components/ui/text';
|
||||
import Emojify from '@/features/emoji/emojify';
|
||||
import { addAutoPlay } from '@/utils/media';
|
||||
import { getTextDirection } from '@/utils/rtl';
|
||||
|
||||
import HoverAccountWrapper from './hover-account-wrapper';
|
||||
@ -33,6 +32,34 @@ interface IPreviewCard {
|
||||
horizontal?: boolean;
|
||||
}
|
||||
|
||||
const domParser = new DOMParser();
|
||||
|
||||
const handleIframeUrl = (html: string, url: string, providerName: string) => {
|
||||
const document = domParser.parseFromString(html, 'text/html').documentElement;
|
||||
const iframe = document.querySelector('iframe');
|
||||
const startTime = new URL(url).searchParams.get('t');
|
||||
|
||||
if (iframe) {
|
||||
const iframeUrl = new URL(iframe.src);
|
||||
|
||||
iframeUrl.searchParams.set('autoplay', '1');
|
||||
iframeUrl.searchParams.set('auto_play', '1');
|
||||
|
||||
if (providerName === 'YouTube') {
|
||||
iframeUrl.searchParams.set('start', startTime ?? '');
|
||||
iframe.referrerPolicy = 'strict-origin-when-cross-origin';
|
||||
}
|
||||
|
||||
iframe.allow = 'autoplay';
|
||||
|
||||
iframe.src = iframeUrl.href;
|
||||
|
||||
return iframe.outerHTML;
|
||||
}
|
||||
|
||||
return '';
|
||||
};
|
||||
|
||||
/** Displays a Mastodon link preview. Similar to OEmbed. */
|
||||
const PreviewCard: React.FC<IPreviewCard> = ({
|
||||
card,
|
||||
@ -93,7 +120,7 @@ const PreviewCard: React.FC<IPreviewCard> = ({
|
||||
};
|
||||
|
||||
const renderVideo = () => {
|
||||
const content = { __html: addAutoPlay(card.html) };
|
||||
const content = { __html: handleIframeUrl(card.html, card.url, card.provider_name) };
|
||||
const ratio = getRatio(card);
|
||||
const height = width / ratio;
|
||||
|
||||
|
||||
@ -304,7 +304,7 @@ const EditEvent: React.FC<IEditEvent> = ({ statusId }) => {
|
||||
<Text tag='span' theme='muted'>
|
||||
<FormattedMessage
|
||||
id='compose_event.fields.has_end_time'
|
||||
defaultMessage='The event has an end date'
|
||||
defaultMessage='This event has an end date'
|
||||
/>
|
||||
</Text>
|
||||
</HStack>
|
||||
|
||||
@ -52,32 +52,4 @@ const getVideoDuration = (file: File): Promise<number> => {
|
||||
return promise;
|
||||
};
|
||||
|
||||
const domParser = new DOMParser();
|
||||
|
||||
/** Try adding autoplay to an iframe embed for platforms such as YouTube. */
|
||||
const addAutoPlay = (html: string): string => {
|
||||
try {
|
||||
const document = domParser.parseFromString(html, 'text/html').documentElement;
|
||||
const iframe = document.querySelector('iframe');
|
||||
|
||||
if (iframe) {
|
||||
const url = new URL(iframe.src);
|
||||
|
||||
url.searchParams.append('autoplay', '1');
|
||||
url.searchParams.append('auto_play', '1');
|
||||
iframe.allow = 'autoplay';
|
||||
|
||||
iframe.src = url.toString();
|
||||
|
||||
// DOM parser creates html/body elements around original HTML fragment,
|
||||
// so we need to get innerHTML out of the body and not the entire document
|
||||
return (document.querySelector('body') as HTMLBodyElement).innerHTML;
|
||||
}
|
||||
} catch (e) {
|
||||
return html;
|
||||
}
|
||||
|
||||
return html;
|
||||
};
|
||||
|
||||
export { getVideoDuration, formatBytes, truncateFilename, addAutoPlay };
|
||||
export { getVideoDuration, formatBytes, truncateFilename };
|
||||
|
||||
Reference in New Issue
Block a user