Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
2ca2bb0742 | |||
8ad5a34f2d |
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@ -244,7 +245,33 @@ func (b playWrightBrowser) Open(ctx context.Context, url string, opts OpenPageOp
|
||||
|
||||
func (b playWrightBrowser) Close() error {
|
||||
return errors.Join(
|
||||
b.ctx.Close(),
|
||||
b.browser.Close(),
|
||||
b.ctx.Close(),
|
||||
b.pw.Stop(),
|
||||
)
|
||||
}
|
||||
|
||||
func deferClose(cl io.Closer) {
|
||||
_ = cl.Close()
|
||||
}
|
||||
|
||||
func Screenshot(ctx context.Context, target string, timeout time.Duration) ([]byte, error) {
|
||||
browser, err := NewPlayWrightBrowser(PlayWrightBrowserOptions{
|
||||
Timeout: &timeout,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating browser: %w", err)
|
||||
}
|
||||
|
||||
defer deferClose(browser)
|
||||
|
||||
doc, err := browser.Open(ctx, target, OpenPageOptions{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening page: %w", err)
|
||||
}
|
||||
|
||||
defer deferClose(doc)
|
||||
|
||||
return doc.Screenshot()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user