Files
go-extractor/document.go
Steve Dudenhoeffer e0da88b9b0
Some checks failed
CI / build (pull_request) Successful in 29s
CI / test (pull_request) Successful in 36s
CI / vet (pull_request) Failing after 6m18s
feat: add PromoteToInteractive and DemoteToDocument for mid-session page transfer
Allow transferring ownership of a Playwright page between Document and
InteractiveBrowser modes without tearing down the browser. This enables
handing a live page to a human (e.g. for captcha solving) and resuming
scraping on the same page afterward.

Closes #76

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 02:27:42 +00:00

91 lines
1.6 KiB
Go

package extractor
import (
"fmt"
"io"
"log/slog"
"time"
"github.com/playwright-community/playwright-go"
)
type Document interface {
io.Closer
Node
URL() string
Refresh() error
Content() (string, error)
WaitForNetworkIdle(timeout *time.Duration) error
}
type document struct {
node
pw *playwright.Playwright
browser playwright.Browser
page playwright.Page
detached bool
}
func newDocument(pw *playwright.Playwright, browser playwright.Browser, page playwright.Page) (Document, error) {
locator := page.Locator("html")
res := &document{
node: node{
locator: locator,
},
pw: pw,
browser: browser,
page: page,
}
slog.Info("new document", "url", page.URL(), "locator", locator)
return res, nil
}
func (d *document) Close() error {
if d.detached {
return nil
}
return d.page.Close()
}
func (d *document) URL() string {
return d.page.URL()
}
func (d *document) Content() (string, error) {
return d.page.Content()
}
func (d *document) Refresh() error {
resp, err := d.page.Reload()
if err != nil {
return fmt.Errorf("failed to reload page: %w", err)
}
if resp != nil && resp.Status() != 200 {
return fmt.Errorf("invalid status code: %d", resp.Status())
}
return nil
}
func (d *document) PageEvaluate(expression string) (interface{}, error) {
return d.page.Evaluate(expression)
}
func (d *document) WaitForNetworkIdle(timeout *time.Duration) error {
if timeout == nil {
t := 30 * time.Second
timeout = &t
}
ms := float64(timeout.Milliseconds())
return d.page.WaitForLoadState(playwright.PageWaitForLoadStateOptions{
State: playwright.LoadStateNetworkidle,
Timeout: &ms,
})
}