Disable video autoplay using JavaScript - Fluid Topics - Latest

DITA Connector Reference Guide

Category
Reference Guides
Audience
public
Version
Latest

With this method, users can disable video autoplay for all formats, including DITA. However, it is not possible allow specific videos to play automatically.

Add the following codeblock to the Custom JavaScript menu of a Fluid Topics portal:

document.addEventListener('ft:reader:topicsloaded', cancelVideoAutoplay);

function cancelVideoAutoplay() {
  let videoContainers = document.querySelectorAll('iframe[src$=".mp4"], object[data$=".mp4"]');
  for (let vContainer of videoContainers) {
    const vContainerTag = vContainer.tagName;
    const vContainerAttribute = vContainerTag === 'IFRAME' ? 'src' : 'data';

    const src = vContainer.getAttribute(vContainerAttribute);
    const video = document.createElement('video');
    const videoSource = document.createElement('source');
    const videoSourceType = src.split('.').pop();

    Array.from(vContainer.attributes).forEach(attr => {
      video.setAttribute(attr.name, attr.value);
    });

    videoSource.setAttribute('src', src);
    videoSource.setAttribute('type', `video/${videoSourceType}`);

    video.setAttribute('controls', true);
    video.append(videoSource);

    vContainer.replaceWith(video);
  }
}

This method does not work in a designed Reader page.