import { io } from "socket.io-client";
// Create session
const session = await fetch("https://connect.webrun.ai/start/start-session", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
taskDetails: "Go to login page",
startingUrl: "https://example.com/login"
})
}).then(r => r.json());
// Setup video stream
const iframe = document.createElement("iframe");
iframe.src = session.streaming.webViewURL;
iframe.width = 1024;
iframe.height = 600;
document.body.appendChild(iframe);
// Connect WebSocket
const socket = io("https://connect.webrun.ai", {
auth: { sessionId: session.sessionId },
transports: ["websocket"]
});
// Take control when ready
socket.on("connect", () => {
socket.emit("message", {
actionType: "interaction",
action: { type: "takeOverControl" }
});
});
// Setup click handler
const canvas = document.getElementById("clickable-overlay");
canvas.addEventListener("click", (e) => {
const rect = canvas.getBoundingClientRect();
const coords = scaleCoords(
e.clientX - rect.left,
e.clientY - rect.top,
rect.width,
rect.height
);
socket.emit("message", {
actionType: "interaction",
action: { type: "CLICK", x: coords.x, y: coords.y }
});
});
// Release control when done
document.getElementById("done-button").addEventListener("click", () => {
socket.emit("message", {
actionType: "interaction",
action: { type: "releaseControl" }
});
// Continue with next task
socket.emit("message", {
actionType: "newTask",
newState: "start",
taskDetails: "Complete the checkout process"
});
});
function scaleCoords(clientX, clientY, displayWidth, displayHeight) {
return {
x: Math.round((clientX / displayWidth) * 1024),
y: Math.round((clientY / displayHeight) * 600) + 155
};
}