|
| 1 | +import 'dotenv/config' |
| 2 | +import { app, BrowserWindow } from 'electron' |
| 3 | +import { Sandbox } from '@e2b/desktop' |
| 4 | + |
| 5 | +// Additional px to take into the account the window border at the top |
| 6 | +const windowFrameHeight = 29 |
| 7 | + |
| 8 | +app.on('window-all-closed', () => { |
| 9 | + app.quit() |
| 10 | +}) |
| 11 | + |
| 12 | +function wait(ms) { |
| 13 | + return new Promise(resolve => setTimeout(resolve, ms)) |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {string} streamUrl - The URL of the stream to load |
| 18 | + * @param {number} width - The width of the window |
| 19 | + * @param {number} height - The height of the window |
| 20 | + */ |
| 21 | +async function createWindow(streamUrl, width, height) { |
| 22 | + const win = new BrowserWindow({ |
| 23 | + title: 'E2B Desktop', |
| 24 | + width, |
| 25 | + height: height + windowFrameHeight, |
| 26 | + webPreferences: { |
| 27 | + nodeIntegration: false, |
| 28 | + contextIsolation: true, |
| 29 | + webviewTag: true, |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
| 33 | + console.log('> Loading stream URL...') |
| 34 | + await win.loadURL(streamUrl) |
| 35 | + console.log(' - Stream URL loaded') |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @param {import('@e2b/desktop').Sandbox} desktop - E2B desktop sandbox |
| 40 | + * @param {number} width - The width of the window |
| 41 | + * @param {number} height - The height of the window |
| 42 | + */ |
| 43 | +async function moveAround(desktop, width, height) { |
| 44 | + console.log('\n> Randomly moving mouse and right clicking 5 times...') |
| 45 | + for (let i = 0; i < 5; i++) { |
| 46 | + const x = Math.floor(Math.random() * width) |
| 47 | + const y = Math.floor(Math.random() * height) |
| 48 | + await desktop.moveMouse(x, y) |
| 49 | + console.log(` - Moved mouse to ${x}, ${y}`) |
| 50 | + await desktop.rightClick() |
| 51 | + console.log(` - Right clicked ${i}`) |
| 52 | + console.log(' - Waiting 2 seconds...\n') |
| 53 | + await wait(2000) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function main() { |
| 58 | + console.log('> Waiting for electron app to be ready...') |
| 59 | + await app.whenReady() |
| 60 | + console.log(' - Electron app is ready') |
| 61 | + |
| 62 | + console.log('\n> Starting desktop sandbox...') |
| 63 | + const desktop = await Sandbox.create() |
| 64 | + console.log(' - Desktop sandbox started, ID:', desktop.sandboxId) |
| 65 | + |
| 66 | + const size = await desktop.getScreenSize() |
| 67 | + console.log(' - Desktop sandbox screen size:', size) |
| 68 | + |
| 69 | + console.log('\n> Starting desktop stream...') |
| 70 | + await desktop.stream.start() |
| 71 | + |
| 72 | + console.log('\n> Waiting 5 seconds for the stream to load...') |
| 73 | + for (let i = 5; i > 0; i--) { |
| 74 | + console.log(` - ${i} seconds remaining until the next step...`) |
| 75 | + await wait(1000) |
| 76 | + } |
| 77 | + |
| 78 | + const url = desktop.stream.getUrl() |
| 79 | + console.log(' - Stream URL:', url) |
| 80 | + |
| 81 | + console.log('\n> Creating browser window...') |
| 82 | + createWindow(url, size.width, size.height) |
| 83 | + console.log(' - Browser window created') |
| 84 | + |
| 85 | + await moveAround(desktop, size.width, size.height) |
| 86 | + |
| 87 | + console.log('\nPress enter to kill the sandbox and close the window...') |
| 88 | + process.stdin.once('data', async () => { |
| 89 | + console.log('\n> Stopping desktop stream...') |
| 90 | + await desktop.stream.stop() |
| 91 | + console.log(' - Desktop stream stopped') |
| 92 | + |
| 93 | + console.log('\n> Closing browser window...') |
| 94 | + console.log(' - Browser window closed') |
| 95 | + |
| 96 | + console.log('\n> Killing desktop sandbox...') |
| 97 | + await desktop.kill() |
| 98 | + console.log(' - Desktop sandbox killed') |
| 99 | + |
| 100 | + app.quit() |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +main().then().catch(console.error) |
0 commit comments