github.com/MontFerret/ferret@v0.18.0/pkg/drivers/http/page.go (about) 1 package http 2 3 import ( 4 "context" 5 "hash/fnv" 6 7 "github.com/MontFerret/ferret/pkg/runtime/events" 8 9 "github.com/PuerkitoBio/goquery" 10 11 "github.com/MontFerret/ferret/pkg/drivers" 12 "github.com/MontFerret/ferret/pkg/drivers/common" 13 "github.com/MontFerret/ferret/pkg/runtime/core" 14 "github.com/MontFerret/ferret/pkg/runtime/values" 15 ) 16 17 type HTMLPage struct { 18 document *HTMLDocument 19 cookies *drivers.HTTPCookies 20 frames *values.Array 21 response drivers.HTTPResponse 22 } 23 24 func NewHTMLPage( 25 qdoc *goquery.Document, 26 url string, 27 response drivers.HTTPResponse, 28 cookies *drivers.HTTPCookies, 29 ) (*HTMLPage, error) { 30 doc, err := NewRootHTMLDocument(qdoc, url) 31 32 if err != nil { 33 return nil, err 34 } 35 36 p := new(HTMLPage) 37 p.document = doc 38 p.cookies = cookies 39 p.frames = nil 40 p.response = response 41 42 return p, nil 43 } 44 45 func (p *HTMLPage) MarshalJSON() ([]byte, error) { 46 return p.document.MarshalJSON() 47 } 48 49 func (p *HTMLPage) Type() core.Type { 50 return drivers.HTMLPageType 51 } 52 53 func (p *HTMLPage) String() string { 54 return p.document.GetURL().String() 55 } 56 57 func (p *HTMLPage) Compare(other core.Value) int64 { 58 tc := drivers.Compare(p.Type(), other.Type()) 59 60 if tc != 0 { 61 return tc 62 } 63 64 httpPage, ok := other.(*HTMLPage) 65 66 if !ok { 67 return 1 68 } 69 70 return p.document.GetURL().Compare(httpPage.GetURL()) 71 } 72 73 func (p *HTMLPage) Unwrap() interface{} { 74 return p.document 75 } 76 77 func (p *HTMLPage) Hash() uint64 { 78 h := fnv.New64a() 79 80 h.Write([]byte("HTTP")) 81 h.Write([]byte(p.Type().String())) 82 h.Write([]byte(":")) 83 h.Write([]byte(p.document.GetURL())) 84 85 return h.Sum64() 86 } 87 88 func (p *HTMLPage) Copy() core.Value { 89 var cookies *drivers.HTTPCookies 90 91 if p.cookies != nil { 92 cookies = p.cookies.Copy().(*drivers.HTTPCookies) 93 } 94 95 page, err := NewHTMLPage( 96 p.document.doc, 97 p.document.GetURL().String(), 98 p.response, 99 cookies, 100 ) 101 102 if err != nil { 103 return values.None 104 } 105 106 return page 107 } 108 109 func (p *HTMLPage) Iterate(ctx context.Context) (core.Iterator, error) { 110 return p.document.Iterate(ctx) 111 } 112 113 func (p *HTMLPage) GetIn(ctx context.Context, path []core.Value) (core.Value, core.PathError) { 114 return common.GetInPage(ctx, path, p) 115 } 116 117 func (p *HTMLPage) SetIn(ctx context.Context, path []core.Value, value core.Value) core.PathError { 118 return common.SetInPage(ctx, path, p, value) 119 } 120 121 func (p *HTMLPage) Length() values.Int { 122 return p.document.Length() 123 } 124 125 func (p *HTMLPage) Close() error { 126 return nil 127 } 128 129 func (p *HTMLPage) IsClosed() values.Boolean { 130 return values.True 131 } 132 133 func (p *HTMLPage) GetURL() values.String { 134 return p.document.GetURL() 135 } 136 137 func (p *HTMLPage) GetMainFrame() drivers.HTMLDocument { 138 return p.document 139 } 140 141 func (p *HTMLPage) GetFrames(ctx context.Context) (*values.Array, error) { 142 if p.frames == nil { 143 arr := values.NewArray(10) 144 145 err := common.CollectFrames(ctx, arr, p.document) 146 147 if err != nil { 148 return values.NewArray(0), err 149 } 150 151 p.frames = arr 152 } 153 154 return p.frames, nil 155 } 156 157 func (p *HTMLPage) GetFrame(ctx context.Context, idx values.Int) (core.Value, error) { 158 if p.frames == nil { 159 arr := values.NewArray(10) 160 161 err := common.CollectFrames(ctx, arr, p.document) 162 163 if err != nil { 164 return values.None, err 165 } 166 167 p.frames = arr 168 } 169 170 return p.frames.Get(idx), nil 171 } 172 173 func (p *HTMLPage) GetCookies(_ context.Context) (*drivers.HTTPCookies, error) { 174 res := drivers.NewHTTPCookies() 175 176 if p.cookies != nil { 177 p.cookies.ForEach(func(value drivers.HTTPCookie, _ values.String) bool { 178 res.Set(value) 179 180 return true 181 }) 182 } 183 184 return res, nil 185 } 186 187 func (p *HTMLPage) GetResponse(_ context.Context) (drivers.HTTPResponse, error) { 188 return p.response, nil 189 } 190 191 func (p *HTMLPage) SetCookies(_ context.Context, _ *drivers.HTTPCookies) error { 192 return core.ErrNotSupported 193 } 194 195 func (p *HTMLPage) DeleteCookies(_ context.Context, _ *drivers.HTTPCookies) error { 196 return core.ErrNotSupported 197 } 198 199 func (p *HTMLPage) PrintToPDF(_ context.Context, _ drivers.PDFParams) (values.Binary, error) { 200 return nil, core.ErrNotSupported 201 } 202 203 func (p *HTMLPage) CaptureScreenshot(_ context.Context, _ drivers.ScreenshotParams) (values.Binary, error) { 204 return nil, core.ErrNotSupported 205 } 206 207 func (p *HTMLPage) WaitForNavigation(_ context.Context, _ values.String) error { 208 return core.ErrNotSupported 209 } 210 211 func (p *HTMLPage) WaitForFrameNavigation(_ context.Context, _ drivers.HTMLDocument, _ values.String) error { 212 return core.ErrNotSupported 213 } 214 215 func (p *HTMLPage) Navigate(_ context.Context, _ values.String) error { 216 return core.ErrNotSupported 217 } 218 219 func (p *HTMLPage) NavigateBack(_ context.Context, _ values.Int) (values.Boolean, error) { 220 return false, core.ErrNotSupported 221 } 222 223 func (p *HTMLPage) NavigateForward(_ context.Context, _ values.Int) (values.Boolean, error) { 224 return false, core.ErrNotSupported 225 } 226 227 func (p *HTMLPage) Subscribe(_ context.Context, _ events.Subscription) (events.Stream, error) { 228 return nil, core.ErrNotSupported 229 }