mirror of
https://github.com/mastodon/mastodon.git
synced 2025-03-11 09:41:23 +01:00
fix: YouTube iframe being able to start at a defined time
This commit is contained in:
parent
e2d9635074
commit
5ef167b4a8
1 changed files with 10 additions and 8 deletions
|
@ -35,18 +35,20 @@ const getHostname = url => {
|
||||||
|
|
||||||
const domParser = new DOMParser();
|
const domParser = new DOMParser();
|
||||||
|
|
||||||
const addAutoPlay = html => {
|
const handleIframeUrl = (html, url) => {
|
||||||
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');
|
||||||
|
const startTime = new URL(url).searchParams.get('t')
|
||||||
|
|
||||||
if (iframe) {
|
if (iframe) {
|
||||||
if (iframe.src.indexOf('?') !== -1) {
|
const iframeUrl = new URL(iframe.src)
|
||||||
iframe.src += '&';
|
|
||||||
} else {
|
|
||||||
iframe.src += '?';
|
|
||||||
}
|
|
||||||
|
|
||||||
iframe.src += 'autoplay=1&auto_play=1';
|
iframeUrl.searchParams.set('autoplay', 1)
|
||||||
|
iframeUrl.searchParams.set('auto_play', 1)
|
||||||
|
|
||||||
|
if (startTime) iframeUrl.searchParams.set('start', startTime)
|
||||||
|
|
||||||
|
iframe.src = iframeUrl.href
|
||||||
|
|
||||||
// DOM parser creates html/body elements around original HTML fragment,
|
// 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
|
// so we need to get innerHTML out of the body and not the entire document
|
||||||
|
@ -108,7 +110,7 @@ export default class Card extends PureComponent {
|
||||||
|
|
||||||
renderVideo () {
|
renderVideo () {
|
||||||
const { card } = this.props;
|
const { card } = this.props;
|
||||||
const content = { __html: addAutoPlay(card.get('html')) };
|
const content = { __html: handleIframeUrl(card.get('html'), card.get('url')) };
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|
Loading…
Add table
Reference in a new issue