github.com/MontFerret/ferret@v0.18.0/pkg/drivers/helpers.go (about) 1 package drivers 2 3 import ( 4 "github.com/MontFerret/ferret/pkg/runtime/core" 5 "github.com/MontFerret/ferret/pkg/runtime/values" 6 "github.com/MontFerret/ferret/pkg/runtime/values/types" 7 ) 8 9 func ToPage(value core.Value) (HTMLPage, error) { 10 err := core.ValidateType(value, HTMLPageType) 11 12 if err != nil { 13 return nil, err 14 } 15 16 return value.(HTMLPage), nil 17 } 18 19 func ToDocument(value core.Value) (HTMLDocument, error) { 20 switch v := value.(type) { 21 case HTMLPage: 22 return v.GetMainFrame(), nil 23 case HTMLDocument: 24 return v, nil 25 default: 26 return nil, core.TypeError( 27 value.Type(), 28 HTMLPageType, 29 HTMLDocumentType, 30 ) 31 } 32 } 33 34 func ToElement(value core.Value) (HTMLElement, error) { 35 switch v := value.(type) { 36 case HTMLPage: 37 return v.GetMainFrame().GetElement(), nil 38 case HTMLDocument: 39 return v.GetElement(), nil 40 case HTMLElement: 41 return v, nil 42 default: 43 return nil, core.TypeError( 44 value.Type(), 45 HTMLPageType, 46 HTMLDocumentType, 47 HTMLElementType, 48 ) 49 } 50 } 51 52 func ToQuerySelector(value core.Value) (QuerySelector, error) { 53 switch v := value.(type) { 54 case QuerySelector: 55 return v, nil 56 case values.String: 57 return NewCSSSelector(v), nil 58 default: 59 return QuerySelector{}, core.TypeError(value.Type(), types.String, QuerySelectorType) 60 } 61 } 62 63 func SetDefaultParams(opts *Options, params Params) Params { 64 if params.Headers == nil && opts.Headers != nil { 65 params.Headers = NewHTTPHeaders() 66 } 67 68 // set default headers 69 if opts.Headers != nil { 70 opts.Headers.ForEach(func(value []string, key string) bool { 71 val := params.Headers.Get(key) 72 73 // do not override user's set values 74 if val == "" { 75 params.Headers.SetArr(key, value) 76 } 77 78 return true 79 }) 80 } 81 82 if params.Cookies == nil && opts.Cookies != nil { 83 params.Cookies = NewHTTPCookies() 84 } 85 86 // set default cookies 87 if opts.Cookies != nil { 88 opts.Cookies.ForEach(func(value HTTPCookie, key values.String) bool { 89 _, exists := params.Cookies.Get(key) 90 91 // do not override user's set values 92 if !exists { 93 params.Cookies.Set(value) 94 } 95 96 return true 97 }) 98 } 99 100 // set default user agent 101 if opts.UserAgent != "" && params.UserAgent == "" { 102 params.UserAgent = opts.UserAgent 103 } 104 105 return params 106 }