8742cb25be
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
3.4 KiB
3.4 KiB
id, name, kind, lang, domain, purity, version, tested, description, tags, signature, uses_functions, uses_types, returns, returns_optional, error_type, imports, file_path, example, params, output
| id | name | kind | lang | domain | purity | version | tested | description | tags | signature | uses_functions | uses_types | returns | returns_optional | error_type | imports | file_path | example | params | output | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| cdp_get_text_in_frame_go_browser | cdp_get_text_in_frame | function | go | browser | impure | 1.0.0 | false | Devuelve el texto visible (innerText) del documento de un iframe concreto componiendo sobre CdpEvalInFrame en un mundo aislado CDP, sin parsear HTML crudo. Trunca a maxBytes con corte rune-safe igual que CdpGetText. |
|
func CdpGetTextInFrame(c *CDPConn, frameID string, maxBytes int) (string, error) |
|
false | error_go_core | functions/browser/cdp_get_text_in_frame.go | conn, _ := CdpConnect("localhost", 9222, "") frames, _ := CdpListFrames(conn) text, err := CdpGetTextInFrame(conn, frames[1].ID, 4096) fmt.Println(text) // texto visible del primer iframe, truncado a 4096 bytes |
|
String con el innerText visible del documento del iframe (document.body.innerText, o document.documentElement.innerText si no hay body), opcionalmente truncado a maxBytes; error si la conexión es nula, el frameID está vacío o la evaluación CDP del frame falla. |
Ejemplo
conn, err := CdpConnect("localhost", 9222, "")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// 1. Listar frames para localizar el iframe deseado
frames, err := CdpListFrames(conn)
if err != nil {
log.Fatal(err)
}
// 2. Leer el texto visible de cada iframe (saltando el frame raíz)
for _, f := range frames {
if f.ParentID == "" { // frame raíz, no es un iframe
continue
}
text, err := CdpGetTextInFrame(conn, f.ID, 4096)
if err != nil {
log.Printf("error en frame %s: %v", f.ID, err)
continue
}
fmt.Printf("=== iframe %s (%s) ===\n%s\n", f.ID, f.URL, text)
}
Cuando usarla
Cuando necesites leer los datos visibles dentro de un iframe sin parsear HTML crudo: extraer el contenido textual de un widget embebido, un panel de pago, un captcha de texto o cualquier documento dentro de un <iframe>. Flujo típico: CdpListFrames → elegir frame por URL → CdpGetTextInFrame. Para HTML estructural completo usa CdpGetFrameHTML; para texto visible usa esta.
Gotchas
- Impura: el frame debe existir y haber terminado de cargar. Un
frameIDobsoleto (frame recargado/navegado) o un frame aún sin cargar propaga el error deCdpEvalInFrame. - Cross-origin OOPIF (out-of-process iframe): el mundo aislado puede vivir en un contexto distinto; si el frame es de otro origen y aislado del proceso, la lectura puede fallar o requerir el
frameIDexacto del OOPIF. innerTextomite el texto oculto por CSS (display:none,visibility:hidden) y colapsa espacios; refleja lo visible, no el contenido literal del DOM. Si necesitas todo el texto del DOM usatextContentvíaCdpEvalInFrame, o el HTML completo víaCdpGetFrameHTML.- El corte por
maxByteses rune-safe pero ciego al contenido: puede cortar a mitad de una palabra o de una línea.