Documentation ¶
Index ¶
- Constants
- Variables
- func DownloadElement(asset DownloadableElement, out io.Writer) (int64, error)
- func DownloadElementAsync(asset DownloadableElement, out io.Writer, c AsyncDownloadChannel)
- type AnchorElement
- type AsyncDownloadChannel
- type AsyncDownloadResult
- type BaseDownloadableElement
- type BaseElement
- type Browser
- func (b *Browser) Back() bool
- func (b *Browser) BookmarkOpen(name string) error
- func (b *Browser) BookmarkSave(name string) error
- func (b *Browser) History() jar.History
- func (b *Browser) Reload() error
- func (b *Browser) SavePage(dir string, perm os.FileMode) (saveFile string, errs []error)
- func (b *Browser) SendFormGET(url string, data gourl.Values) error
- func (b *Browser) SendFormPOST(url string, data gourl.Values) error
- func (b *Browser) SendGET(url string) error
- func (b *Browser) SendHEAD(url string) error
- func (b *Browser) SendMultipartPOST(u string, fields gourl.Values, files FileSet) error
- func (b *Browser) SendPOST(url string, contentType string, body io.Reader) error
- type Document
- func (doc *Document) Anchors() []*AnchorElement
- func (doc *Document) Body() *goquery.Selection
- func (doc *Document) CharacterSet() string
- func (doc *Document) Click(expr string) error
- func (doc *Document) ContentType() string
- func (doc *Document) Cookie() []*http.Cookie
- func (doc *Document) Form(expr string) (Submittable, error)
- func (doc *Document) Forms() []Submittable
- func (doc *Document) GetElementByID(id string) *goquery.Selection
- func (doc *Document) GetElementsByClassName(className string) *goquery.Selection
- func (doc *Document) GetElementsByName(name string) *goquery.Selection
- func (doc *Document) GetElementsByTagName(name string) *goquery.Selection
- func (doc *Document) Head() *goquery.Selection
- func (doc *Document) Images() []*ImageElement
- func (doc *Document) InnerHTML() string
- func (doc *Document) QuerySelector(selector string) *goquery.Selection
- func (doc *Document) QuerySelectorAll(selector string) *goquery.Selection
- func (doc *Document) Scripts() []*ScriptElement
- func (doc *Document) Stylesheets() []*StylesheetElement
- func (doc *Document) Title() string
- func (doc *Document) Write(w io.Writer) (int64, error)
- type DownloadableElement
- type Element
- type ElementType
- type Event
- type EventArgValues
- type EventArgs
- func (a *EventArgs) GetBool(key string) bool
- func (a *EventArgs) GetFloat64(key string) float64
- func (a *EventArgs) GetInt(key string) int
- func (a *EventArgs) GetInt64(key string) int64
- func (a *EventArgs) GetString(key string) string
- func (a *EventArgs) IsDefaultPrevented() bool
- func (a *EventArgs) IsStopped() bool
- func (a *EventArgs) PreventDefault()
- func (a *EventArgs) StopPropagation()
- type EventListenerFunc
- type EventTarget
- type File
- type FileSet
- type Form
- func (f *Form) Action() string
- func (f *Form) Click(button string) error
- func (f *Form) ClickByValue(name, value string) error
- func (f *Form) Dom() *goquery.Selection
- func (f *Form) File(name string, fileName string, data io.Reader) error
- func (f *Form) Input(name, value string) error
- func (f *Form) Method() string
- func (f *Form) Set(name, value string) error
- func (f *Form) SetFile(name string, fileName string, data io.Reader)
- func (f *Form) Submit() error
- type ImageElement
- type Navigator
- type ScriptElement
- type StylesheetElement
- type Submittable
Constants ¶
const ( OnError = "error" OnLoad = "load" OnUnload = "unload" OnRequest = "request" OnResponse = "response" )
const ( Name = "Surf" Version = "2.0" )
const ( DefaultSendReferer = true DefaultMetaRefreshHandling = true DefaultFollowRedirects = true DefaultNumDownloadWorkers = 4 )
const ( HeaderUserAgent = "User-Agent" HeaderHost = "Host" HeaderReferer = "Referer" HeaderContentType = "Content-Type" )
const ( MethodGET = "GET" MethodPOST = "POST" MethodHEAD = "HEAD" )
const ( ContentTypeTextPlain = "text/plain" ContentTypeTextHtml = "text/html" )
Variables ¶
var ( // Debugging turns debugging messages on and off. Debugging bool = false // UserAgent is the User-Agent header value sent with requests. UserAgent string = agent.Create(Name, Version) // JarState is the current browser state. JarState *jar.State = &jar.State{} // JarCookies stores cookies for every site visited by the browser. JarCookies http.CookieJar = jar.NewMemoryCookies() // JarBookmarks stores the saved bookmarks. JarBookmarks jar.BookmarksJar = jar.NewMemoryBookmarks() // JarHistory stores the visited pages. JarHistory jar.History = jar.NewMemoryHistory() // transport specifies the mechanism by which individual HTTP // requests are made. Transport http.RoundTripper // RequestHeaders are additional headers to send with each request. RequestHeaders http.Header = jar.NewMemoryHeaders() // NumDownloadWorkers is the number of workers to download page assets. NumDownloadWorkers int = DefaultNumDownloadWorkers // SendReferer instructs a Browser to send the Referer header. SendReferer bool = DefaultSendReferer // MetaRefreshHandling instructs a Browser to handle the refresh meta tag. MetaRefreshHandling bool = DefaultMetaRefreshHandling // FollowRedirects instructs a Browser to follow Location headers. FollowRedirects bool = DefaultFollowRedirects )
Functions ¶
func DownloadElement ¶
func DownloadElement(asset DownloadableElement, out io.Writer) (int64, error)
DownloadElement copies a remote file to the given writer.
func DownloadElementAsync ¶
func DownloadElementAsync(asset DownloadableElement, out io.Writer, c AsyncDownloadChannel)
DownloadElementAsync downloads an element asynchronously and notifies the given channel when the download is complete.
Types ¶
type AnchorElement ¶
type AnchorElement struct { BaseElement // Text is the text appearing between the opening and closing anchor tag. Text string }
AnchorElement stores the properties of a page link.
func NewAnchorElement ¶
func NewAnchorElement(u *gourl.URL, id, text string) *AnchorElement
NewAnchorElement creates and returns a new *AnchorElement instance.
type AsyncDownloadChannel ¶
type AsyncDownloadChannel chan *AsyncDownloadResult
AsyncDownloadChannel is a channel upon which the results of an async download are passed.
type AsyncDownloadResult ¶
type AsyncDownloadResult struct { // Element is a pointer to the Downloadable asset that was downloaded. Element DownloadableElement // Writer where the asset data was written. Writer io.Writer // Size is the number of bytes written to the io.Writer. Size int64 // Error contains any error that occurred during the download or nil. Error error }
AsyncDownloadResult has the results of an asynchronous download.
type BaseDownloadableElement ¶
type BaseDownloadableElement struct {
BaseElement
}
BaseDownloadableElement is an element that may be downloaded.
func (*BaseDownloadableElement) Download ¶
func (at *BaseDownloadableElement) Download(out io.Writer) (int64, error)
Download writes the element to the given io.Writer type.
func (*BaseDownloadableElement) DownloadAsync ¶
func (at *BaseDownloadableElement) DownloadAsync(out io.Writer, ch AsyncDownloadChannel)
DownloadAsync downloads the element asynchronously.
type BaseElement ¶
type BaseElement struct {
// contains filtered or unexported fields
}
BaseElement implements Element.
func (*BaseElement) ID ¶
func (at *BaseElement) ID() string
ID returns the asset ID or an empty string when not available.
type Browser ¶
type Browser struct { *EventTarget Document *Document Location *gourl.URL Headers http.Header Response *http.Response // contains filtered or unexported fields }
Browser...
func (*Browser) BookmarkOpen ¶
BookmarkOpen calls SendGET() with the URL for the bookmark with the given name.
func (*Browser) BookmarkSave ¶
BookmarkSave saves the page URL in the bookmarks with the given name.
func (*Browser) History ¶
History returns the browser history. See https://developer.mozilla.org/en-US/docs/Web/API/Window/history
func (*Browser) SendFormGET ¶
SendFormGET appends the data values to the given URL and sends a GET request.
func (*Browser) SendFormPOST ¶
SendFormPOST requests the given URL using the POST method with the given data.
func (*Browser) SendMultipartPOST ¶
SendMultipartPOST requests the given URL using the POST method with the given data using multipart/form-data format.
type Document ¶
type Document struct { *EventTarget // Location stores the current url. Location *gourl.URL // contains filtered or unexported fields }
Document stores the details of the current browser document.
func (*Document) Anchors ¶
func (doc *Document) Anchors() []*AnchorElement
Anchors returns an array of every anchor tag found in the page. See https://developer.mozilla.org/en-US/docs/Web/API/Document/anchors
func (*Document) Body ¶
Body returns the page body as a string of html. See https://developer.mozilla.org/en-US/docs/Web/API/Document/body
func (*Document) CharacterSet ¶
CharacterSet returns the document character set, e.g. "utf-8". See https://developer.mozilla.org/en-US/docs/Web/API/Document/characterSet
func (*Document) ContentType ¶
Content type returns the document content type, e.g. "text/html". See https://developer.mozilla.org/en-US/docs/Web/API/Document/contentType
func (*Document) Form ¶
func (doc *Document) Form(expr string) (Submittable, error)
Form returns the form in the current page that matches the given expr.
func (*Document) Forms ¶
func (doc *Document) Forms() []Submittable
Forms returns an array of every form in the page. See https://developer.mozilla.org/en-US/docs/Web/API/Document/forms
func (*Document) GetElementByID ¶
GetElementById returns a reference to the element by its ID. See https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
func (*Document) GetElementsByClassName ¶
GetElementsByClassName returns every element with the given class. See https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
func (*Document) GetElementsByName ¶
GetElementsByName returns the elements with the given name. See https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName
func (*Document) GetElementsByTagName ¶
GetElementsByTagName returns the elements with the given tag. See https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName
func (*Document) Head ¶
See https://developer.mozilla.org/en-US/docs/Web/API/Document/head
func (*Document) Images ¶
func (doc *Document) Images() []*ImageElement
Images returns an array of every image found in the page. See https://developer.mozilla.org/en-US/docs/Web/API/Document/images
func (*Document) QuerySelector ¶
QuerySelector returns the first element matching the given selector. See https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
func (*Document) QuerySelectorAll ¶
QuerySelectorAll returns all of the elements matching the given selector. See https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
func (*Document) Scripts ¶
func (doc *Document) Scripts() []*ScriptElement
Scripts returns an array of every script linked to the document. See https://developer.mozilla.org/en-US/docs/Web/API/Document/scripts
func (*Document) Stylesheets ¶
func (doc *Document) Stylesheets() []*StylesheetElement
Stylesheets returns an array of every stylesheet linked to the document. See https://developer.mozilla.org/en-US/docs/Web/API/Document/styleSheets
func (*Document) Title ¶
Title returns the page title. See https://developer.mozilla.org/en-US/docs/Web/API/Document/title
type DownloadableElement ¶
type DownloadableElement interface { Element // Download writes the contents of the element to the given writer. // // Returns the number of bytes written. Download(out io.Writer) (int64, error) // DownloadAsync downloads the contents of the element asynchronously. // // An instance of AsyncDownloadResult will be sent down the given channel // when the download is complete. DownloadAsync(out io.Writer, ch AsyncDownloadChannel) }
DownloadableElement represents an element that may be downloaded.
type Element ¶
type Element interface { // URL returns the asset URL. URL() *gourl.URL // ID returns the asset ID or an empty string when not available. ID() string // Type returns the type of element. Type() ElementType }
Element represents a page element, such as an image or stylesheet.
type ElementType ¶
type ElementType uint16
ElementType describes a type of page element, such as an image or stylesheet.
const ( // ElementTypeLink describes a *Link element. ElementTypeLink ElementType = iota // ElementTypeImage describes an *Image element. ElementTypeImage // ElementTypeStylesheet describes a *Stylesheet element. ElementTypeStylesheet // ElementTypeScript describes a *Script element. ElementTypeScript )
type EventArgValues ¶
type EventArgValues map[string]interface{}
type EventArgs ¶
type EventArgs struct { Values EventArgValues Error error // contains filtered or unexported fields }
EventArgs stores arguments to an event.
func NewEventArgs ¶
func NewEventArgs(values EventArgValues) *EventArgs
NewEventArgs returns a new *EventArgs instance.
func (*EventArgs) GetFloat64 ¶
GetFloat64 returns the value at key as a float64.
func (*EventArgs) IsDefaultPrevented ¶
IsDefaultPrevented returns true when PreventDefault() has been called.
func (*EventArgs) PreventDefault ¶
func (a *EventArgs) PreventDefault()
Cancels the event if it is cancelable, without stopping further dispatching of the event.
func (*EventArgs) StopPropagation ¶
func (a *EventArgs) StopPropagation()
StopPropagation prevents further dispatching of the event.
type EventListenerFunc ¶
type EventListenerFunc func(e *Event)
type EventTarget ¶
type EventTarget struct {
// contains filtered or unexported fields
}
EventTarget represents an object which dispatches events.
func NewEventTarget ¶
func NewEventTarget() *EventTarget
NewEventTarget returns a *EventTarget instance.
func (*EventTarget) AddEventListener ¶
func (t *EventTarget) AddEventListener(event string, fn EventListenerFunc)
AddEventListener registers a listener on the target.
func (*EventTarget) DispatchEvent ¶
func (t *EventTarget) DispatchEvent(event string, target interface{}, args *EventArgs) error
DispatchEvent dispatches the given event to any registered listeners.
func (*EventTarget) RemoveEventListener ¶
func (t *EventTarget) RemoveEventListener(event string, fn EventListenerFunc)
RemoveEventListener removes a registered listener on the target.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a input type file, that includes the fileName and a io.reader
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form is the default form element.
func (*Form) ClickByValue ¶
Click submits the form by clicking the button with the given name and value.
func (*Form) File ¶
File sets the value for an form input type file, it returns an ElementNotFound error if the field does not exists
func (*Form) Input ¶
Input sets the value of a form field. it returns an ElementNotFound error if the field does not exists
func (*Form) Set ¶
Set will set the value of a form field if it exists, or create and set it if it does not.
type ImageElement ¶
type ImageElement struct { BaseDownloadableElement // Alt is the value of the image alt attribute if available. Alt string // Title is the value of the image title attribute if available. Title string }
ImageElement stores the properties of an image.
func NewImageElement ¶
func NewImageElement(url *gourl.URL, id, alt, title string) *ImageElement
NewImageElement creates and returns a new *ImageElement instance.
type Navigator ¶
type Navigator struct { }
Navigator represents the state and the identity of the user agent. See https://developer.mozilla.org/en-US/docs/Web/API/Navigator
func (*Navigator) AppName ¶
AppName returns the name of the browser. See https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/appName
func (*Navigator) AppVersion ¶
AppVersion returns the browser version. See https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/appVersion
func (*Navigator) UserAgent ¶
UserAgent returns the browser user agent. See https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/userAgent
type ScriptElement ¶
type ScriptElement struct { BaseDownloadableElement // Type is the value of the type attribute. Defaults to "text/javascript" when not specified. TypeAttr string }
ScriptElement stores the properties of a linked script.
func NewScriptElement ¶
func NewScriptElement(url *gourl.URL, id, typ string) *ScriptElement
NewScriptElement creates and returns a new *ScriptElement instance.
type StylesheetElement ¶
type StylesheetElement struct { BaseDownloadableElement // Media is the value of the media attribute. Defaults to "all" when not specified. Media string // TypeAttr is the value of the type attribute. Defaults to "text/css" when not specified. TypeAttr string }
StylesheetElement stores the properties of a linked stylesheet.
func NewStylesheetElement ¶
func NewStylesheetElement(url *gourl.URL, id, media, typ string) *StylesheetElement
NewStylesheetElement creates and returns a new *StylesheetElement instance.
type Submittable ¶
type Submittable interface { Method() string Action() string Input(name, value string) error Set(name, value string) error File(name string, fileName string, data io.Reader) error SetFile(name string, fileName string, data io.Reader) Click(button string) error ClickByValue(name, value string) error Submit() error Dom() *goquery.Selection }
Submittable represents an element that may be submitted, such as a form.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package agent generates user agents strings for well known browsers and for custom browsers.
|
Package agent generates user agents strings for well known browsers and for custom browsers. |
Package errors contains error types specific to the Surf library.
|
Package errors contains error types specific to the Surf library. |
Package jar has containers for storing data, such as bookmarks and cookies.
|
Package jar has containers for storing data, such as bookmarks and cookies. |
Package util contains some utility methods used by other packages.
|
Package util contains some utility methods used by other packages. |