feat: add Redirect and ReplaceURL methods for browser navigation

Add SSE-based navigation methods to Context:
- Redirect(url) / Redirectf() - navigate to new page
- ReplaceURL(url) / ReplaceURLf() - update URL without navigation

Update session example to demonstrate full login flow with redirects.
This commit is contained in:
Ryan Hamamura
2026-01-12 00:47:52 -10:00
parent 9a23188973
commit 03b6d7453a
3 changed files with 97 additions and 17 deletions

18
via.go
View File

@@ -15,6 +15,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
@@ -359,6 +360,8 @@ const (
patchTypeElements = iota
patchTypeSignals
patchTypeScript
patchTypeRedirect
patchTypeReplaceURL
)
type patch struct {
@@ -453,6 +456,21 @@ func New() *V {
v.logErr(c, "ExecuteScript failed: %v", err)
}
}
case patchTypeRedirect:
if err := sse.Redirect(patch.content); err != nil {
if sse.Context().Err() == nil {
v.logErr(c, "Redirect failed: %v", err)
}
}
case patchTypeReplaceURL:
parsedURL, err := url.Parse(patch.content)
if err != nil {
v.logErr(c, "ReplaceURL failed to parse URL: %v", err)
} else if err := sse.ReplaceURL(*parsedURL); err != nil {
if sse.Context().Err() == nil {
v.logErr(c, "ReplaceURL failed: %v", err)
}
}
}
}
}