/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="dom" />

import { Scrapbox } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.4.2/userscript.ts";
declare const scrapbox: Scrapbox;

let video: HTMLVideoElement | undefined;
scrapbox.PageMenu.addItem({
  title: "recording",
  onClick: async () => {
    if (video) return;
    const stream = await navigator.mediaDevices.getDisplayMedia({
      video: true,
      audio: false,
    });

    video = document.createElement("video");
    video.style.maxWidth = "50%";
    video.style.position = "fixed";
    video.style.bottom = "5%";
    video.style.left = "5%";
    video.controls = true;
    video.autoplay = true;
    video.srcObject = stream;
    video.currentTime = 7*24*60*1000;
    video.onseeked = ()=>{
      alert("after-seeked: "+video.duration);
      video.onseeked = undefined;
    };
    document.body.append(video);
  },
});
scrapbox.PageMenu.addItem({
  title: "stop",
  onClick: () => {
    if (!video) return;
    const stream = video.srcObject;
    if (stream instanceof MediaStream) {
      for (const track of stream.getTracks()) {
        track.stop();
      }
    }
    video.srcObject = null;
    video.remove();
    video = undefined;
  },
});