File size: 1,173 Bytes
e442c7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
export async function captureScreen(): Promise<string> {
	let stream: MediaStream | undefined;
	try {
		// This will show the native browser dialog for screen capture
		stream = await navigator.mediaDevices.getDisplayMedia({
			video: true,
			audio: false,
		});

		// Create a canvas element to capture the screenshot
		const canvas = document.createElement("canvas");
		const video = document.createElement("video");

		// Wait for the video to load metadata
		await new Promise((resolve) => {
			video.onloadedmetadata = () => {
				canvas.width = video.videoWidth;
				canvas.height = video.videoHeight;
				video.play();
				resolve(null);
			};
			if (stream) {
				video.srcObject = stream;
			} else {
				throw Error("No stream available");
			}
		});

		// Draw the video frame to canvas
		const context = canvas.getContext("2d");
		context?.drawImage(video, 0, 0, canvas.width, canvas.height);
		// Convert to base64
		return canvas.toDataURL("image/png");
	} catch (error) {
		console.error("Error capturing screenshot:", error);
		throw error;
	} finally {
		// Stop all tracks
		if (stream) {
			stream.getTracks().forEach((track) => track.stop());
		}
	}
}