|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +import type { DriverCommandHandlers } from "./types.js"; |
| 4 | + |
| 5 | +const writeParamsSchema = z.object({ |
| 6 | + text: z.string(), |
| 7 | +}); |
| 8 | + |
| 9 | +const pasteParamsSchema = z.object({ |
| 10 | + shortcut: z.enum(["ControlOrMeta+V", "Meta+V", "Control+V"]).optional(), |
| 11 | +}); |
| 12 | + |
| 13 | +export const clipboardHandlers: DriverCommandHandlers = { |
| 14 | + async "clipboard.read"(manager) { |
| 15 | + const context = await manager.browserContext(); |
| 16 | + const text = await context.clipboard.readText(); |
| 17 | + return { text }; |
| 18 | + }, |
| 19 | + |
| 20 | + async "clipboard.write"(manager, params) { |
| 21 | + const { text } = writeParamsSchema.parse(params); |
| 22 | + const context = await manager.browserContext(); |
| 23 | + await context.clipboard.writeText(text); |
| 24 | + return { ok: true }; |
| 25 | + }, |
| 26 | + |
| 27 | + async "clipboard.clear"(manager) { |
| 28 | + const context = await manager.browserContext(); |
| 29 | + await context.clipboard.clear(); |
| 30 | + return { ok: true }; |
| 31 | + }, |
| 32 | + |
| 33 | + async "clipboard.paste"(manager, params) { |
| 34 | + const { shortcut } = pasteParamsSchema.parse(params ?? {}); |
| 35 | + const context = await manager.browserContext(); |
| 36 | + await context.clipboard.paste(shortcut ? { shortcut } : undefined); |
| 37 | + return { ok: true }; |
| 38 | + }, |
| 39 | + |
| 40 | + async "clipboard.copy"(manager) { |
| 41 | + const context = await manager.browserContext(); |
| 42 | + await context.clipboard.copy(); |
| 43 | + return { ok: true }; |
| 44 | + }, |
| 45 | + |
| 46 | + async "clipboard.cut"(manager) { |
| 47 | + const context = await manager.browserContext(); |
| 48 | + await context.clipboard.cut(); |
| 49 | + return { ok: true }; |
| 50 | + }, |
| 51 | +}; |
0 commit comments