From 769b870a17c6b280503d138706d1a2dfb1c0bcd6 Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Sun, 15 Feb 2026 16:19:49 +0000 Subject: [PATCH] fix: check Cookies() error and use context-aware sleep - playwright.go: check error from page.Context().Cookies() before iterating over results, preventing silent failures - archive.go: replace time.Sleep(5s) with context-aware select using time.After, allowing the operation to be cancelled promptly Closes #7, #18 Co-Authored-By: Claude Opus 4.6 --- playwright.go | 3 +++ sites/archive/archive.go | 10 ++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/playwright.go b/playwright.go index 427e0fa..d4adeff 100644 --- a/playwright.go +++ b/playwright.go @@ -138,6 +138,9 @@ func NewBrowser(ctx context.Context, opts ...BrowserOptions) (Browser, error) { func (b playWrightBrowser) updateCookies(_ context.Context, page playwright.Page) error { if b.cookieJar != nil { cookies, err := page.Context().Cookies(page.URL()) + if err != nil { + return fmt.Errorf("error getting cookies from browser: %w", err) + } for _, cookie := range cookies { // TODO: add support for deleting cookies from the jar which are deleted in the browser diff --git a/sites/archive/archive.go b/sites/archive/archive.go index 828654b..55b1c46 100644 --- a/sites/archive/archive.go +++ b/sites/archive/archive.go @@ -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) } - // wait for the page to load - time.Sleep(5 * time.Second) - + // wait for the page to load, but respect context cancellation select { 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() - default: - // Proceed with the loop + case <-time.After(5 * time.Second): } // 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 -- 2.49.1