5b10b419a2
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package browser
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// CdpNavBack retrocede una entrada en el historial de navegacion de la pestana activa.
|
|
// Obtiene el historial via Page.getNavigationHistory, calcula el indice anterior y
|
|
// navega a esa entrada via Page.navigateToHistoryEntry.
|
|
// Retorna error si ya estamos al inicio del historial.
|
|
func CdpNavBack(c *CDPConn) error {
|
|
if c == nil {
|
|
return fmt.Errorf("cdp nav back: conexion nula")
|
|
}
|
|
|
|
result, err := c.sendCDP("Page.getNavigationHistory", nil)
|
|
if err != nil {
|
|
return fmt.Errorf("cdp nav back: obtener historial: %w", err)
|
|
}
|
|
|
|
currentIndexRaw, ok := result["currentIndex"]
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: respuesta sin currentIndex")
|
|
}
|
|
currentIndex, ok := currentIndexRaw.(float64)
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: currentIndex tipo inesperado: %T", currentIndexRaw)
|
|
}
|
|
|
|
entriesRaw, ok := result["entries"]
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: respuesta sin entries")
|
|
}
|
|
entries, ok := entriesRaw.([]any)
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: entries tipo inesperado: %T", entriesRaw)
|
|
}
|
|
|
|
idx := int(currentIndex) - 1
|
|
if idx < 0 {
|
|
return fmt.Errorf("cdp nav back: ya en el inicio del historial")
|
|
}
|
|
if idx >= len(entries) {
|
|
return fmt.Errorf("cdp nav back: indice %d fuera de rango (len=%d)", idx, len(entries))
|
|
}
|
|
|
|
entry, ok := entries[idx].(map[string]any)
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: entrada[%d] tipo inesperado: %T", idx, entries[idx])
|
|
}
|
|
entryIDRaw, ok := entry["id"]
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: entrada sin campo id")
|
|
}
|
|
entryIDFloat, ok := entryIDRaw.(float64)
|
|
if !ok {
|
|
return fmt.Errorf("cdp nav back: entry id tipo inesperado: %T", entryIDRaw)
|
|
}
|
|
entryID := int(entryIDFloat)
|
|
|
|
_, err = c.sendCDP("Page.navigateToHistoryEntry", map[string]any{"entryId": entryID})
|
|
if err != nil {
|
|
return fmt.Errorf("cdp nav back: navegar a entrada %d: %w", entryID, err)
|
|
}
|
|
|
|
return nil
|
|
}
|