features.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Package devtools implements bounded operations shared by Chromium and WebView2.
  2. package devtools
  3. import (
  4. "encoding/base64"
  5. "encoding/json"
  6. "math"
  7. "clusterflow.dev/cfbrowser/api"
  8. "clusterflow.dev/cfbrowser/internal/capture"
  9. )
  10. type Call func(string, json.RawMessage, func(json.RawMessage, error))
  11. func Capture(call Call, full bool, done func([]byte, error)) {
  12. call("Page.getLayoutMetrics", json.RawMessage(`{}`), func(raw json.RawMessage, err error) {
  13. if err != nil {
  14. done(nil, err)
  15. return
  16. }
  17. var m struct {
  18. Content struct{ Width, Height float64 } `json:"cssContentSize"`
  19. View struct{ PageX, PageY, ClientWidth, ClientHeight float64 } `json:"cssLayoutViewport"`
  20. }
  21. if json.Unmarshal(raw, &m) != nil {
  22. done(nil, api.Err("BROWSER_INVALID_RESULT", "invalid layout metrics"))
  23. return
  24. }
  25. x, y, w, h := m.View.PageX, m.View.PageY, m.View.ClientWidth, m.View.ClientHeight
  26. if full {
  27. x, y, w, h = 0, 0, m.Content.Width, m.Content.Height
  28. }
  29. w, h = math.Ceil(w), math.Ceil(h)
  30. if !finite(x) || !finite(y) || !finite(w) || !finite(h) || w < 1 || h < 1 || w > capture.MaxDimension || h > capture.MaxDimension {
  31. done(nil, api.Err("BROWSER_RESULT_LIMIT", "capture dimensions exceed 4096 x 4096"))
  32. return
  33. }
  34. 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}})
  35. call("Page.captureScreenshot", params, func(raw json.RawMessage, err error) {
  36. if err != nil {
  37. done(nil, err)
  38. return
  39. }
  40. var r struct {
  41. Data string `json:"data"`
  42. }
  43. if json.Unmarshal(raw, &r) != nil || len(r.Data) > base64.StdEncoding.EncodedLen(capture.MaxBytes) {
  44. done(nil, api.Err("BROWSER_RESULT_LIMIT", "capture exceeds 4 MiB"))
  45. return
  46. }
  47. data, err := base64.StdEncoding.DecodeString(r.Data)
  48. if err == nil {
  49. err = capture.Validate(data)
  50. }
  51. done(data, err)
  52. })
  53. })
  54. }
  55. func finite(n float64) bool { return !math.IsNaN(n) && !math.IsInf(n, 0) }
  56. type command struct {
  57. method string
  58. params map[string]any
  59. }
  60. // Input uses browser-generated events. It does not automate the operating system.
  61. func Input(call Call, action, value string, x, y float64, done func(error)) {
  62. if !finite(x) || !finite(y) || x < 0 || y < 0 || x > 4096 || y > 4096 || len(value) > 16384 {
  63. done(api.Err("BROWSER_INVALID_ARGUMENT", "invalid input coordinates or value"))
  64. return
  65. }
  66. cmds := []command{}
  67. mouse := func(kind, button string) command {
  68. return command{"Input.dispatchMouseEvent", map[string]any{"type": kind, "x": x, "y": y, "button": button, "clickCount": 1}}
  69. }
  70. key := func(name, code string, vk, mod int, text string) {
  71. p := map[string]any{"type": "keyDown", "key": name, "code": code, "windowsVirtualKeyCode": vk, "modifiers": mod}
  72. if text != "" {
  73. p["text"] = text
  74. p["unmodifiedText"] = text
  75. }
  76. cmds = append(cmds, command{"Input.dispatchKeyEvent", p}, command{"Input.dispatchKeyEvent", map[string]any{"type": "keyUp", "key": name, "code": code, "windowsVirtualKeyCode": vk, "modifiers": mod}})
  77. }
  78. switch action {
  79. case "click":
  80. cmds = append(cmds, mouse("mousePressed", "left"), mouse("mouseReleased", "left"))
  81. case "hover":
  82. cmds = append(cmds, command{"Input.dispatchMouseEvent", map[string]any{"type": "mouseMoved", "x": x, "y": y}})
  83. case "fill":
  84. key("a", "KeyA", 65, 2, "")
  85. key("Backspace", "Backspace", 8, 0, "")
  86. if value != "" {
  87. cmds = append(cmds, command{"Input.insertText", map[string]any{"text": value}})
  88. }
  89. case "type":
  90. cmds = append(cmds, command{"Input.insertText", map[string]any{"text": value}})
  91. case "press":
  92. keys := map[string]int{"Enter": 13, "Tab": 9, "Escape": 27, "ArrowDown": 40, "ArrowUp": 38, "Backspace": 8}
  93. vk, ok := keys[value]
  94. if !ok {
  95. done(api.Err("BROWSER_INVALID_ARGUMENT", "unsupported key"))
  96. return
  97. }
  98. text := ""
  99. if value == "Enter" {
  100. text = "\r"
  101. }
  102. key(value, value, vk, 0, text)
  103. default:
  104. done(api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "input action is unavailable"))
  105. return
  106. }
  107. var next func(int)
  108. next = func(i int) {
  109. if i == len(cmds) {
  110. done(nil)
  111. return
  112. }
  113. p, _ := json.Marshal(cmds[i].params)
  114. call(cmds[i].method, p, func(_ json.RawMessage, err error) {
  115. if err != nil {
  116. done(err)
  117. return
  118. }
  119. next(i + 1)
  120. })
  121. }
  122. next(0)
  123. }