JSFiddle - React, Tailwind, and code Playground

by eladalon

HTML

<button id="startButton">Start!</button>
<button id="actionButton" disabled>Action! (monitors-only)</button>
<p id="out"></p>

JavaScript

actionButton.onclick = () => {
  out.innerHTML = "Imagine track.funcX() is now called, and it works well when capturing screens, but throws when capturing windows, and is no-op if capturing tabs. This codepath should only be reachable when capturing a screen. Previously, it was. But with cross-surface-type source injection...";
};

startButton.onclick = async () => {
  startButton.disabled = true;

  let stream;
  try {
    stream = await navigator.mediaDevices.getDisplayMedia();
  } catch (exception) {
    startButton.disabled = false;
    return;
  }

  const [videoTrack] = stream.getVideoTracks();
  videoTrack.onended = () => {
    startButton.disabled = false;
    actionButton.disabled = true;
  };

  // NOTE: This is only executed immediately after gDM resolves.
  if (videoTrack.getSettings().displaySurface == 'monitor') {
    actionButton.disabled = false;
  }
}