From 6c68062e56771043997021902b947721e675dcfb Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Sun, 15 Feb 2026 16:13:43 +0000 Subject: [PATCH] fix: add nil guards to prevent nil-pointer panics - 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 --- document.go | 2 +- sites/archive/archive.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/document.go b/document.go index 0666348..c0089fd 100644 --- a/document.go +++ b/document.go @@ -61,7 +61,7 @@ func (d *document) Refresh() error { return fmt.Errorf("failed to reload page: %w", err) } - if resp.Status() != 200 { + if resp != nil && resp.Status() != 200 { return fmt.Errorf("invalid status code: %d", resp.Status()) } diff --git a/sites/archive/archive.go b/sites/archive/archive.go index 5d2ded9..828654b 100644 --- a/sites/archive/archive.go +++ b/sites/archive/archive.go @@ -104,15 +104,25 @@ func (c Config) Archive(ctx context.Context, b extractor.Browser, target string) return nil, fmt.Errorf("failed to open url: %w", err) } - err = doc.SelectFirst("input[name='url']").Type(u.String()) + urlInput := doc.SelectFirst("input[name='url']") + if urlInput == nil { + _ = doc.Close() + return nil, fmt.Errorf("failed to find url input element") + } + err = urlInput.Type(u.String()) if err != nil { _ = doc.Close() return nil, fmt.Errorf("failed to type url: %w", err) } - err = doc.SelectFirst("form#submiturl input[type=\"submit\"]").Click() + submitBtn := doc.SelectFirst("form#submiturl input[type=\"submit\"]") + if submitBtn == nil { + _ = doc.Close() + return nil, fmt.Errorf("failed to find submit button") + } + err = submitBtn.Click() if err != nil { _ = doc.Close() return nil, fmt.Errorf("failed to click submit: %w", err) -- 2.49.1