surf.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Package surf ensembles other packages into a usable browser.
  2. package surf
  3. import (
  4. "github.com/headzoo/surf/agent"
  5. "github.com/headzoo/surf/browser"
  6. "github.com/headzoo/surf/jar"
  7. )
  8. var (
  9. // DefaultUserAgent is the global user agent value.
  10. DefaultUserAgent = agent.Create()
  11. // DefaultSendReferer is the global value for the AttributeSendReferer attribute.
  12. DefaultSendReferer = true
  13. // DefaultMetaRefreshHandling is the global value for the AttributeHandleRefresh attribute.
  14. DefaultMetaRefreshHandling = true
  15. // DefaultFollowRedirects is the global value for the AttributeFollowRedirects attribute.
  16. DefaultFollowRedirects = true
  17. // DefaultMaxHistoryLength is the global value for max history length.
  18. DefaultMaxHistoryLength = 0
  19. )
  20. // NewBrowser creates and returns a *browser.Browser type.
  21. func NewBrowser() *browser.Browser {
  22. bow := &browser.Browser{}
  23. bow.SetUserAgent(DefaultUserAgent)
  24. bow.SetState(&jar.State{})
  25. bow.SetCookieJar(jar.NewMemoryCookies())
  26. bow.SetBookmarksJar(jar.NewMemoryBookmarks())
  27. hist := jar.NewMemoryHistory()
  28. hist.SetMax(DefaultMaxHistoryLength)
  29. bow.SetHistoryJar(hist)
  30. bow.SetHeadersJar(jar.NewMemoryHeaders())
  31. bow.SetAttributes(browser.AttributeMap{
  32. browser.SendReferer: DefaultSendReferer,
  33. browser.MetaRefreshHandling: DefaultMetaRefreshHandling,
  34. browser.FollowRedirects: DefaultFollowRedirects,
  35. })
  36. return bow
  37. }