This package provides a high-level API for managing and interacting with browser instances using the rod library. It supports features such as browser pooling, headless mode, proxy configuration, cookies, and automatic idle timeout.
To install the package, use go get
:
go get -u github.com/simp-lee/browser
Here's a basic example of how to use the package:
package main
import (
"fmt"
"github.com/simp-lee/browser"
"time"
)
func main() {
// Get a browser instance with default options
b, err := browser.GetBrowser()
if err != nil {
panic(err)
}
defer b.Close()
// Get a page instance from the browser
page, err := b.GetPage()
if err != nil {
panic(err)
}
defer b.PutPage(page)
page.MustNavigate("https://example.com")
fmt.Println(page.MustInfo().Title)
}
You can configure various options for the browser instance:
package main
import (
"github.com/simp-lee/browser"
"time"
)
func main() {
// Get a browser instance with custom options
b, err := browser.GetBrowser(
browser.WithHeadless(false),
browser.WithProxy("127.0.0.1:8080"),
browser.WithPoolSize(5),
browser.WithIdleTimeout(10*time.Minute),
)
if err != nil {
panic(err)
}
defer b.Close()
}
You can configure various options for the page instance:
package main
import (
"fmt"
"github.com/simp-lee/browser"
)
func main() {
// Get a browser instance with default options
b, err := browser.GetBrowser()
if err != nil {
panic(err)
}
defer b.Close()
// Get a page instance with custom options
page, err := b.GetPage(
browser.WithUserAgent("CustomUserAgent"),
browser.WithReferer("https://example.com"),
browser.WithViewport(1920, 1080, 1.0, false),
browser.WithExtraHeaders(map[string]string{
"X-Custom-Header": "custom_value",
}),
browser.WithCookies(browser.Cookie{
Name: "example_cookie",
Value: "cookie_value",
Domain: "example.com",
}),
)
if err != nil {
panic(err)
}
defer b.PutPage(page)
page.MustNavigate("https://example.com")
fmt.Println(page.MustInfo().Title)
}
You can block image loading on a page by calling b.BlockImageLoading(page)
to save bandwidth`:
package main
import (
"fmt"
"github.com/simp-lee/browser"
)
func main() {
// Get a browser instance with default options
b, err := browser.GetBrowser()
if err != nil {
panic(err)
}
defer b.Close()
// Get a page instance from the browser
page, err := b.GetPage()
if err != nil {
panic(err)
}
defer b.PutPage(page)
// Block image loading on the page
if err := b.BlockImageLoading(page); err != nil {
panic(err)
}
page.MustNavigate("https://example.com")
fmt.Println(page.MustInfo().Title)
}
You can retrieve cookies from a page instance using the GetCookies
method::
package main
import (
"fmt"
"github.com/simp-lee/browser"
)
func main() {
// Get a browser instance with default options
b, err := browser.GetBrowser()
if err != nil {
panic(err)
}
defer b.Close()
// Get a page instance from the browser
page, err := b.GetPage()
if err != nil {
panic(err)
}
defer b.PutPage(page)
// Navigate to a URL
page.MustNavigate("https://example.com")
// Get cookies from the page
cookies, err := b.GetCookies(page)
if err != nil {
panic(err)
}
// Print the cookies
for _, cookie := range cookies {
fmt.Printf("Cookie: %+v\n", cookie)
}
}