vendor/github.com/go-rod/rod/lib/proto/a_patch.go

180 lines · 3698 bytes

  1// Patches to normalize the proto types
  2
  3package proto
  4
  5import (
  6	"time"
  7)
  8
  9// TimeSinceEpoch UTC time in seconds, counted from January 1, 1970.
 10// To convert a time.Time to TimeSinceEpoch, for example:
 11//
 12//	proto.TimeSinceEpoch(time.Now().Unix())
 13//
 14// For session cookie, the value should be -1.
 15type TimeSinceEpoch float64
 16
 17// Time interface.
 18func (t TimeSinceEpoch) Time() time.Time {
 19	return (time.Unix(0, 0)).Add(
 20		time.Duration(t * TimeSinceEpoch(time.Second)),
 21	)
 22}
 23
 24// String interface.
 25func (t TimeSinceEpoch) String() string {
 26	return t.Time().String()
 27}
 28
 29// MonotonicTime Monotonically increasing time in seconds since an arbitrary point in the past.
 30type MonotonicTime float64
 31
 32// Duration interface.
 33func (t MonotonicTime) Duration() time.Duration {
 34	return time.Duration(t * MonotonicTime(time.Second))
 35}
 36
 37// String interface.
 38func (t MonotonicTime) String() string {
 39	return t.Duration().String()
 40}
 41
 42// Point from the origin (0, 0).
 43type Point struct {
 44	X float64 `json:"x"`
 45	Y float64 `json:"y"`
 46}
 47
 48// NewPoint instance.
 49func NewPoint(x, y float64) Point {
 50	return Point{x, y}
 51}
 52
 53// Add v to p and returns a new Point.
 54func (p Point) Add(v Point) Point {
 55	return NewPoint(p.X+v.X, p.Y+v.Y)
 56}
 57
 58// Minus v from p and returns a new Point.
 59func (p Point) Minus(v Point) Point {
 60	return NewPoint(p.X-v.X, p.Y-v.Y)
 61}
 62
 63// Scale p with s and returns a new Point.
 64func (p Point) Scale(s float64) Point {
 65	return NewPoint(p.X*s, p.Y*s)
 66}
 67
 68// Len is the number of vertices.
 69func (q DOMQuad) Len() int {
 70	return len(q) / 2 //nolint: mnd
 71}
 72
 73// Each point.
 74func (q DOMQuad) Each(fn func(pt Point, i int)) {
 75	for i := 0; i < q.Len(); i++ {
 76		fn(Point{q[i*2], q[i*2+1]}, i)
 77	}
 78}
 79
 80// Center of the polygon.
 81func (q DOMQuad) Center() Point {
 82	var x, y float64
 83	q.Each(func(pt Point, _ int) {
 84		x += pt.X
 85		y += pt.Y
 86	})
 87	return Point{x / float64(q.Len()), y / float64(q.Len())}
 88}
 89
 90// Area of the polygon
 91// https://en.wikipedia.org/wiki/Polygon#Area
 92func (q DOMQuad) Area() float64 {
 93	area := 0.0
 94	l := len(q)/2 - 1 //nolint: mnd
 95
 96	for i := 0; i < l; i++ {
 97		area += q[i*2]*q[i*2+3] - q[i*2+2]*q[i*2+1]
 98	}
 99	area += q[l*2]*q[1] - q[0]*q[l*2+1]
100
101	return area / 2 //nolint: mnd
102}
103
104// OnePointInside the shape.
105func (res *DOMGetContentQuadsResult) OnePointInside() *Point {
106	for _, q := range res.Quads {
107		if q.Area() >= 1 {
108			pt := q.Center()
109			return &pt
110		}
111	}
112
113	return nil
114}
115
116// Box returns the smallest leveled rectangle that can cover the whole shape.
117func (res *DOMGetContentQuadsResult) Box() (box *DOMRect) {
118	return Shape(res.Quads).Box()
119}
120
121// Shape is a list of DOMQuad.
122type Shape []DOMQuad
123
124// Box returns the smallest leveled rectangle that can cover the whole shape.
125func (qs Shape) Box() (box *DOMRect) {
126	if len(qs) == 0 {
127		return
128	}
129
130	left := qs[0][0]
131	top := qs[0][1]
132	right := left
133	bottom := top
134
135	for _, q := range qs {
136		q.Each(func(pt Point, _ int) {
137			if pt.X < left {
138				left = pt.X
139			}
140			if pt.Y < top {
141				top = pt.Y
142			}
143			if pt.X > right {
144				right = pt.X
145			}
146			if pt.Y > bottom {
147				bottom = pt.Y
148			}
149		})
150	}
151
152	box = &DOMRect{left, top, right - left, bottom - top}
153
154	return
155}
156
157// MoveTo X and Y to x and y.
158func (p *InputTouchPoint) MoveTo(x, y float64) {
159	p.X = x
160	p.Y = y
161}
162
163// CookiesToParams converts Cookies list to NetworkCookieParam list.
164func CookiesToParams(cookies []*NetworkCookie) []*NetworkCookieParam {
165	list := []*NetworkCookieParam{}
166	for _, c := range cookies {
167		list = append(list, &NetworkCookieParam{
168			Name:     c.Name,
169			Value:    c.Value,
170			Domain:   c.Domain,
171			Path:     c.Path,
172			Secure:   c.Secure,
173			HTTPOnly: c.HTTPOnly,
174			SameSite: c.SameSite,
175			Expires:  c.Expires,
176			Priority: c.Priority,
177		})
178	}
179	return list
180}