| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // Package devtools implements bounded operations shared by Chromium and WebView2.
- package devtools
- import (
- "encoding/base64"
- "encoding/json"
- "math"
- "clusterflow.dev/cfbrowser/api"
- "clusterflow.dev/cfbrowser/internal/capture"
- )
- type Call func(string, json.RawMessage, func(json.RawMessage, error))
- func Capture(call Call, full bool, done func([]byte, error)) {
- call("Page.getLayoutMetrics", json.RawMessage(`{}`), func(raw json.RawMessage, err error) {
- if err != nil {
- done(nil, err)
- return
- }
- var m struct {
- Content struct{ Width, Height float64 } `json:"cssContentSize"`
- View struct{ PageX, PageY, ClientWidth, ClientHeight float64 } `json:"cssLayoutViewport"`
- }
- if json.Unmarshal(raw, &m) != nil {
- done(nil, api.Err("BROWSER_INVALID_RESULT", "invalid layout metrics"))
- return
- }
- x, y, w, h := m.View.PageX, m.View.PageY, m.View.ClientWidth, m.View.ClientHeight
- if full {
- x, y, w, h = 0, 0, m.Content.Width, m.Content.Height
- }
- w, h = math.Ceil(w), math.Ceil(h)
- if !finite(x) || !finite(y) || !finite(w) || !finite(h) || w < 1 || h < 1 || w > capture.MaxDimension || h > capture.MaxDimension {
- done(nil, api.Err("BROWSER_RESULT_LIMIT", "capture dimensions exceed 4096 x 4096"))
- return
- }
- params, _ := json.Marshal(map[string]any{"format": "png", "fromSurface": true, "captureBeyondViewport": full, "clip": map[string]any{"x": x, "y": y, "width": w, "height": h, "scale": 1}})
- call("Page.captureScreenshot", params, func(raw json.RawMessage, err error) {
- if err != nil {
- done(nil, err)
- return
- }
- var r struct {
- Data string `json:"data"`
- }
- if json.Unmarshal(raw, &r) != nil || len(r.Data) > base64.StdEncoding.EncodedLen(capture.MaxBytes) {
- done(nil, api.Err("BROWSER_RESULT_LIMIT", "capture exceeds 4 MiB"))
- return
- }
- data, err := base64.StdEncoding.DecodeString(r.Data)
- if err == nil {
- err = capture.Validate(data)
- }
- done(data, err)
- })
- })
- }
- func finite(n float64) bool { return !math.IsNaN(n) && !math.IsInf(n, 0) }
- type command struct {
- method string
- params map[string]any
- }
- // Input uses browser-generated events. It does not automate the operating system.
- func Input(call Call, action, value string, x, y float64, done func(error)) {
- if !finite(x) || !finite(y) || x < 0 || y < 0 || x > 4096 || y > 4096 || len(value) > 16384 {
- done(api.Err("BROWSER_INVALID_ARGUMENT", "invalid input coordinates or value"))
- return
- }
- cmds := []command{}
- mouse := func(kind, button string) command {
- return command{"Input.dispatchMouseEvent", map[string]any{"type": kind, "x": x, "y": y, "button": button, "clickCount": 1}}
- }
- key := func(name, code string, vk, mod int, text string) {
- p := map[string]any{"type": "keyDown", "key": name, "code": code, "windowsVirtualKeyCode": vk, "modifiers": mod}
- if text != "" {
- p["text"] = text
- p["unmodifiedText"] = text
- }
- cmds = append(cmds, command{"Input.dispatchKeyEvent", p}, command{"Input.dispatchKeyEvent", map[string]any{"type": "keyUp", "key": name, "code": code, "windowsVirtualKeyCode": vk, "modifiers": mod}})
- }
- switch action {
- case "click":
- cmds = append(cmds, mouse("mousePressed", "left"), mouse("mouseReleased", "left"))
- case "hover":
- cmds = append(cmds, command{"Input.dispatchMouseEvent", map[string]any{"type": "mouseMoved", "x": x, "y": y}})
- case "fill":
- key("a", "KeyA", 65, 2, "")
- key("Backspace", "Backspace", 8, 0, "")
- if value != "" {
- cmds = append(cmds, command{"Input.insertText", map[string]any{"text": value}})
- }
- case "type":
- cmds = append(cmds, command{"Input.insertText", map[string]any{"text": value}})
- case "press":
- keys := map[string]int{"Enter": 13, "Tab": 9, "Escape": 27, "ArrowDown": 40, "ArrowUp": 38, "Backspace": 8}
- vk, ok := keys[value]
- if !ok {
- done(api.Err("BROWSER_INVALID_ARGUMENT", "unsupported key"))
- return
- }
- text := ""
- if value == "Enter" {
- text = "\r"
- }
- key(value, value, vk, 0, text)
- default:
- done(api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "input action is unavailable"))
- return
- }
- var next func(int)
- next = func(i int) {
- if i == len(cmds) {
- done(nil)
- return
- }
- p, _ := json.Marshal(cmds[i].params)
- call(cmds[i].method, p, func(_ json.RawMessage, err error) {
- if err != nil {
- done(err)
- return
- }
- next(i + 1)
- })
- }
- next(0)
- }
|