5b10b419a2
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2.2 KiB
2.2 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_close_tab_go_browser | cdp_close_tab | function | go | browser | impure | 1.0.0 | false | Cierra una pestaña Chrome por su ID via GET /json/close/<id>. Sin WebSocket — solo HTTP. Util para limpiar pestañas abiertas por automatizaciones. |
|
func CdpCloseTab(host string, port int, tabID string) error | false | error_go_core | functions/browser/cdp_list_tabs.go | tabs, _ := browser.CdpListTabs("localhost", 9222) for _, t := range tabs { if t.URL == "https://example.com" { _ = browser.CdpCloseTab("localhost", 9222, t.ID) } } |
|
nil si la pestaña se cerró correctamente; error si tabID está vacío, la conexión falla o Chrome devuelve status != 200 |
Ejemplo
// Listar tabs y cerrar la primera que coincida con una URL
tabs, err := browser.CdpListTabs("localhost", 9222)
if err != nil {
log.Fatal(err)
}
for _, t := range tabs {
if t.URL == "https://example.com/login" {
if err := browser.CdpCloseTab("localhost", 9222, t.ID); err != nil {
log.Printf("error cerrando tab %s: %v", t.ID, err)
}
}
}
Cuando usarla
Después de terminar una sesión de scraping o automatización: cierra las pestañas abiertas programáticamente sin afectar el resto del perfil. También útil para liberar recursos cuando CdpNewTab ha creado muchas pestañas temporales.
Gotchas
- No requiere conexión WebSocket previa; usa HTTP puro contra
/json/close/<id>. - Si Chrome ya cerró la pestaña (o el ID es inválido), devuelve error de status HTTP.
- El ID debe obtenerse de
CdpListTabs— no es el índice visible del tab, es el UUID interno de Chrome. - No espera confirmación de cierre; para saber si la pestaña desapareció, volver a llamar
CdpListTabs.