Fix updateCookies error + context-aware sleep (#7, #18) #37

Merged
Claude merged 1 commits from fix/cookies-error-and-context-sleep into main 2026-02-15 16:20:06 +00:00
2 changed files with 7 additions and 6 deletions
Showing only changes of commit 769b870a17 - Show all commits

View File

@@ -138,6 +138,9 @@ func NewBrowser(ctx context.Context, opts ...BrowserOptions) (Browser, error) {
func (b playWrightBrowser) updateCookies(_ context.Context, page playwright.Page) error { func (b playWrightBrowser) updateCookies(_ context.Context, page playwright.Page) error {
if b.cookieJar != nil { if b.cookieJar != nil {
cookies, err := page.Context().Cookies(page.URL()) cookies, err := page.Context().Cookies(page.URL())
if err != nil {
return fmt.Errorf("error getting cookies from browser: %w", err)
}
for _, cookie := range cookies { for _, cookie := range cookies {
// TODO: add support for deleting cookies from the jar which are deleted in the browser // TODO: add support for deleting cookies from the jar which are deleted in the browser

View File

@@ -128,15 +128,13 @@ func (c Config) Archive(ctx context.Context, b extractor.Browser, target string)
return nil, fmt.Errorf("failed to click submit: %w", err) return nil, fmt.Errorf("failed to click submit: %w", err)
} }
// wait for the page to load // wait for the page to load, but respect context cancellation
time.Sleep(5 * time.Second)
select { select {
case <-ctx.Done(): case <-ctx.Done():
slog.Debug("context already done before entering the loop", "err", ctx.Err()) slog.Debug("context done during initial wait", "err", ctx.Err())
_ = doc.Close()
return nil, ctx.Err() return nil, ctx.Err()
default: case <-time.After(5 * time.Second):
// Proceed with the loop
} }
// now we are waiting for archive.ph to archive the page and redirect us to the archived page // now we are waiting for archive.ph to archive the page and redirect us to the archived page
// the way we can tell this is happening is by checking the url of the page periodically // the way we can tell this is happening is by checking the url of the page periodically