File: domsnapshot.go

package info (click to toggle)
golang-github-chromedp-cdproto 0.0~git20230109.6b041c6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,584 kB
  • sloc: makefile: 2
file content (138 lines) | stat: -rw-r--r-- 6,063 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Package domsnapshot provides the Chrome DevTools Protocol
// commands, types, and events for the DOMSnapshot domain.
//
// This domain facilitates obtaining document snapshots with DOM, layout, and
// style information.
//
// Generated by the cdproto-gen command.
package domsnapshot

// Code generated by cdproto-gen. DO NOT EDIT.

import (
	"context"

	"github.com/chromedp/cdproto/cdp"
)

// DisableParams disables DOM snapshot agent for the given page.
type DisableParams struct{}

// Disable disables DOM snapshot agent for the given page.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/DOMSnapshot#method-disable
func Disable() *DisableParams {
	return &DisableParams{}
}

// Do executes DOMSnapshot.disable against the provided context.
func (p *DisableParams) Do(ctx context.Context) (err error) {
	return cdp.Execute(ctx, CommandDisable, nil, nil)
}

// EnableParams enables DOM snapshot agent for the given page.
type EnableParams struct{}

// Enable enables DOM snapshot agent for the given page.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/DOMSnapshot#method-enable
func Enable() *EnableParams {
	return &EnableParams{}
}

// Do executes DOMSnapshot.enable against the provided context.
func (p *EnableParams) Do(ctx context.Context) (err error) {
	return cdp.Execute(ctx, CommandEnable, nil, nil)
}

// CaptureSnapshotParams returns a document snapshot, including the full DOM
// tree of the root node (including iframes, template contents, and imported
// documents) in a flattened array, as well as layout and white-listed computed
// style information for the nodes. Shadow DOM in the returned DOM tree is
// flattened.
type CaptureSnapshotParams struct {
	ComputedStyles                 []string `json:"computedStyles"`                           // Whitelist of computed styles to return.
	IncludePaintOrder              bool     `json:"includePaintOrder,omitempty"`              // Whether to include layout object paint orders into the snapshot.
	IncludeDOMRects                bool     `json:"includeDOMRects,omitempty"`                // Whether to include DOM rectangles (offsetRects, clientRects, scrollRects) into the snapshot
	IncludeBlendedBackgroundColors bool     `json:"includeBlendedBackgroundColors,omitempty"` // Whether to include blended background colors in the snapshot (default: false). Blended background color is achieved by blending background colors of all elements that overlap with the current element.
	IncludeTextColorOpacities      bool     `json:"includeTextColorOpacities,omitempty"`      // Whether to include text color opacity in the snapshot (default: false). An element might have the opacity property set that affects the text color of the element. The final text color opacity is computed based on the opacity of all overlapping elements.
}

// CaptureSnapshot returns a document snapshot, including the full DOM tree
// of the root node (including iframes, template contents, and imported
// documents) in a flattened array, as well as layout and white-listed computed
// style information for the nodes. Shadow DOM in the returned DOM tree is
// flattened.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/DOMSnapshot#method-captureSnapshot
//
// parameters:
//
//	computedStyles - Whitelist of computed styles to return.
func CaptureSnapshot(computedStyles []string) *CaptureSnapshotParams {
	return &CaptureSnapshotParams{
		ComputedStyles: computedStyles,
	}
}

// WithIncludePaintOrder whether to include layout object paint orders into
// the snapshot.
func (p CaptureSnapshotParams) WithIncludePaintOrder(includePaintOrder bool) *CaptureSnapshotParams {
	p.IncludePaintOrder = includePaintOrder
	return &p
}

// WithIncludeDOMRects whether to include DOM rectangles (offsetRects,
// clientRects, scrollRects) into the snapshot.
func (p CaptureSnapshotParams) WithIncludeDOMRects(includeDOMRects bool) *CaptureSnapshotParams {
	p.IncludeDOMRects = includeDOMRects
	return &p
}

// WithIncludeBlendedBackgroundColors whether to include blended background
// colors in the snapshot (default: false). Blended background color is achieved
// by blending background colors of all elements that overlap with the current
// element.
func (p CaptureSnapshotParams) WithIncludeBlendedBackgroundColors(includeBlendedBackgroundColors bool) *CaptureSnapshotParams {
	p.IncludeBlendedBackgroundColors = includeBlendedBackgroundColors
	return &p
}

// WithIncludeTextColorOpacities whether to include text color opacity in the
// snapshot (default: false). An element might have the opacity property set
// that affects the text color of the element. The final text color opacity is
// computed based on the opacity of all overlapping elements.
func (p CaptureSnapshotParams) WithIncludeTextColorOpacities(includeTextColorOpacities bool) *CaptureSnapshotParams {
	p.IncludeTextColorOpacities = includeTextColorOpacities
	return &p
}

// CaptureSnapshotReturns return values.
type CaptureSnapshotReturns struct {
	Documents []*DocumentSnapshot `json:"documents,omitempty"` // The nodes in the DOM tree. The DOMNode at index 0 corresponds to the root document.
	Strings   []string            `json:"strings,omitempty"`   // Shared string table that all string properties refer to with indexes.
}

// Do executes DOMSnapshot.captureSnapshot against the provided context.
//
// returns:
//
//	documents - The nodes in the DOM tree. The DOMNode at index 0 corresponds to the root document.
//	strings - Shared string table that all string properties refer to with indexes.
func (p *CaptureSnapshotParams) Do(ctx context.Context) (documents []*DocumentSnapshot, strings []string, err error) {
	// execute
	var res CaptureSnapshotReturns
	err = cdp.Execute(ctx, CommandCaptureSnapshot, p, &res)
	if err != nil {
		return nil, nil, err
	}

	return res.Documents, res.Strings, nil
}

// Command names.
const (
	CommandDisable         = "DOMSnapshot.disable"
	CommandEnable          = "DOMSnapshot.enable"
	CommandCaptureSnapshot = "DOMSnapshot.captureSnapshot"
)