- document.go: check if resp is nil before calling resp.Status() in Refresh(), since Playwright's Reload() can return a nil response - archive.go: check SelectFirst() results for nil before calling Type() and Click(), preventing panics when DOM elements are missing Closes #10, #11 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
83 lines
1.5 KiB
Go
83 lines
1.5 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
|
|
}
|
|
|
|
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 {
|
|
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) 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,
|
|
})
|
|
}
|