main.js
 1  // main.js
 2  if (!ScreenCastRecorder.isSupportedBrowser()) {
 3      console.error("Screen Recording not supported in this browser");
 4  }
 5  let recorder;
 6  let outputBlob;
 7  const stopRecording = () => __awaiter(void 0, void 0, void 0, function* () {
 8      let currentState = "RECORDING";
 9      // We should do nothing if the user try to stop recording when it is not started
10      if (currentState === "OFF" || recorder == null) {
11          return;
12      }
13      // if (currentState === "COUNTDOWN") {
14      //     this.setState({
15      //         currentState: "OFF",
16      //     })
17      // }
18      if (currentState === "RECORDING") {
19          if (recorder.getState() === "inactive") {
20              // this.setState({
21              //     currentState: "OFF",
22              // })
23              console.log("Inactive");
24          }
25          else {
26              outputBlob = yield recorder.stop();
27              console.log("Done recording");
28              // this.setState({
29              //   outputBlob,
30              //   currentState: "PREVIEW_FILE",
31              // })
32              window.currentState = "PREVIEW_FILE";
33              const videoSource = URL.createObjectURL(outputBlob);
34              window.videoSource = videoSource;
35              const fileName = "recording";
36              const link = document.createElement("a");
37              link.setAttribute("href", videoSource);
38              link.setAttribute("download", `${fileName}.webm`);
39              link.click();
40          }
41      }
42  });
43  const startRecording = () => __awaiter(void 0, void 0, void 0, function* () {
44      const recordAudio = true;
45      recorder = new ScreenCastRecorder({
46          recordAudio,
47          onErrorOrStop: () => stopRecording(),
48      });
49      try {
50          yield recorder.initialize();
51      }
52      catch (e) {
53          console.warn(`ScreenCastRecorder.initialize error: ${e}`);
54          //   this.setState({ currentState: "UNSUPPORTED" })
55          window.currentState = "UNSUPPORTED";
56          return;
57      }
58      // this.setState({ currentState: "COUNTDOWN" })
59      const hasStarted = recorder.start();
60      if (hasStarted) {
61          // this.setState({
62          //     currentState: "RECORDING",
63          // })
64          console.log("Started recording");
65          window.currentState = "RECORDING";
66      }
67      else {
68          stopRecording().catch(err => console.warn(`withScreencast.stopRecording threw an error: ${err}`));
69      }
70  });
71  
72  // Set global functions to window.
73  window.startRecording = startRecording;
74  window.stopRecording = stopRecording;