about summary refs log tree commit diff
path: root/app/javascript
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-02-15 23:05:12 +0100
committerGitHub <noreply@github.com>2018-02-15 23:05:12 +0100
commit478ca39e5e8463044a259388459da56d2141e104 (patch)
tree9793bdfd163e8bc596ce941c91e9603ec7a72ac5 /app/javascript
parentf7765acf9d92951a616f41b738d5d23ede58c162 (diff)
After click to embed video, autoplay it (#6480)
Diffstat (limited to 'app/javascript')
-rw-r--r--app/javascript/mastodon/features/status/components/card.js25
1 files changed, 24 insertions, 1 deletions
diff --git a/app/javascript/mastodon/features/status/components/card.js b/app/javascript/mastodon/features/status/components/card.js
index 8a6383471..b52f3c4fa 100644
--- a/app/javascript/mastodon/features/status/components/card.js
+++ b/app/javascript/mastodon/features/status/components/card.js
@@ -30,6 +30,29 @@ const trim = (text, len) => {
   return text.substring(0, cut) + (text.length > len ? '…' : '');
 };
 
+const domParser = new DOMParser();
+
+const addAutoPlay = html => {
+  const document = domParser.parseFromString(html, 'text/html').documentElement;
+  const iframe = document.querySelector('iframe');
+
+  if (iframe) {
+    if (iframe.src.indexOf('?') !== -1) {
+      iframe.src += '&';
+    } else {
+      iframe.src += '?';
+    }
+
+    iframe.src += 'autoplay=1&auto_play=1';
+
+    // 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').innerHTML;
+  }
+
+  return html;
+};
+
 export default class Card extends React.PureComponent {
 
   static propTypes = {
@@ -92,7 +115,7 @@ export default class Card extends React.PureComponent {
 
   renderVideo () {
     const { card }  = this.props;
-    const content   = { __html: card.get('html') };
+    const content   = { __html: addAutoPlay(card.get('html')) };
     const { width } = this.state;
     const ratio     = card.get('width') / card.get('height');
     const height    = card.get('width') > card.get('height') ? (width / ratio) : (width * ratio);