2021-09-30 00:00:49 +00:00
package chrome
import (
"encoding/json"
"fmt"
2021-11-29 15:02:23 +00:00
2021-09-30 00:00:49 +00:00
"go.digitalcircle.com.br/open/replaycli-go/api"
)
type Cli struct {
2021-10-11 14:53:44 +00:00
* api . ApiCli
2021-09-30 00:00:49 +00:00
}
2021-11-29 15:02:23 +00:00
type TabMetadata struct {
Description string ` json:"description" `
DevtoolsFrontendUrl string ` json:"devtoolsFrontendUrl" `
Id string ` json:"id" `
Title string ` json:"title" `
Type string ` json:"type" `
Url string ` json:"url" `
WebSocketDebuggerUrl string ` json:"webSocketDebuggerUrl" `
}
2021-09-30 00:00:49 +00:00
func ( c * Cli ) Start ( to int ) error {
2021-12-09 13:30:53 +00:00
// to -> Time out para a conexão com o Chrome. Dado em segundos.
2021-10-11 14:53:44 +00:00
err := c . HttpCli ( ) . JsonGet ( fmt . Sprintf ( "/ipc/chrome/start?to=%d" , to ) , nil )
2021-09-30 00:00:49 +00:00
return err
}
func ( c * Cli ) StartHeadless ( ) error {
2021-10-11 14:53:44 +00:00
err := c . HttpCli ( ) . JsonGet ( "/ipc/chrome/startHeadless" , nil )
2021-09-30 00:00:49 +00:00
return err
}
func ( c * Cli ) Stop ( ) error {
2021-10-11 14:53:44 +00:00
err := c . HttpCli ( ) . JsonGet ( "/ipc/chrome/stop" , nil )
2021-09-30 00:00:49 +00:00
return err
}
func ( c * Cli ) New ( url string ) ( string , error ) {
2021-12-09 13:30:53 +00:00
// url -> Site no qual se quer iniciar uma nova aba.
2021-09-30 00:00:49 +00:00
ret := ""
2021-10-11 14:53:44 +00:00
err := c . HttpCli ( ) . JsonGet ( fmt . Sprintf ( "/ipc/chrome/new?url=%s" , url ) , & ret )
2021-09-30 00:00:49 +00:00
return ret , err
2021-12-09 13:30:53 +00:00
// ret -> ID do Websocket criado para navegação na "url" informada.
2021-09-30 00:00:49 +00:00
}
func ( c * Cli ) Close ( id string ) error {
2021-12-09 13:30:53 +00:00
// id -> ID do websocket ativo. É o primeiro retorno do método "New".
2021-10-11 14:53:44 +00:00
err := c . HttpCli ( ) . JsonGet ( "/ipc/chrome/close/" + id , nil )
2021-09-30 00:00:49 +00:00
return err
}
func ( c * Cli ) Eval ( id string , s string ) ( map [ string ] interface { } , error ) {
2021-12-09 13:30:53 +00:00
// id -> ID do websocket no qual se quer fazer um comando JavaScript.
// s -> Comando JavaScript a ser executado no terminal web do WebSocket informado.
2021-09-30 00:00:49 +00:00
ret := make ( map [ string ] interface { } )
2021-10-11 14:53:44 +00:00
bs , err := c . HttpCli ( ) . RawPost ( "/ipc/chrome/eval/" + id , [ ] byte ( s ) )
2021-09-30 00:00:49 +00:00
json . Unmarshal ( bs , & ret )
return ret , err
2021-12-09 13:30:53 +00:00
// ret -> O valor retornado pelo comando JavaScript executado.
2021-09-30 00:00:49 +00:00
}
func ( c * Cli ) Wait ( id string , s string , to int ) ( string , error ) {
2021-12-09 13:30:53 +00:00
// id -> ID do websocket no qual se quer fazer um comando JavaScript.
// s -> Comando JavaScript a ser executado no terminal web do WebSocket informado. Deve ser uma expressão que retorne um valor booleano para, por exemplo, verificar se um elemento já foi carregado.
// to -> TimeOut de espera para retorno "true" da expressão JavaScript informada. Informado em segundos.
2021-09-30 00:00:49 +00:00
ret := ""
2021-10-11 14:53:44 +00:00
bs , err := c . HttpCli ( ) . RawPost ( fmt . Sprintf ( "/ipc/chrome/wait/%s?to=%d" , id , to ) , [ ] byte ( s ) )
2021-09-30 00:00:49 +00:00
json . Unmarshal ( bs , & ret )
return ret , err
}
func ( c * Cli ) Send ( id string , m string , ps map [ string ] interface { } ) ( string , error ) {
2021-12-09 13:30:53 +00:00
// id -> ID do Websocket no qual se deseja realizar a alteração.
2021-09-30 00:00:49 +00:00
ret := ""
in := map [ string ] interface { } {
"method" : m ,
"params" : ps ,
}
2021-10-11 14:53:44 +00:00
err := c . HttpCli ( ) . JsonPost ( "/ipc/chrome/eval/" + id , in , & ret )
2021-09-30 00:00:49 +00:00
return ret , err
2021-12-09 13:30:53 +00:00
// ret ->
2021-09-30 00:00:49 +00:00
}
2021-12-01 20:12:14 +00:00
func ( c * Cli ) OpenTabs ( ) ( ret [ ] map [ string ] string , err error ) {
2021-11-29 15:02:23 +00:00
err = c . HttpCli ( ) . JsonGet ( "/ipc/chrome/opentabs" , & ret )
return ret , err
2021-12-09 13:30:53 +00:00
// ret -> Array contendo as abas abertas e metadados sobre as mesmas.
2021-11-29 15:02:23 +00:00
}
2021-12-01 20:19:03 +00:00
func ( c * Cli ) FindTabByUrl ( url string ) ( ret string , err error ) {
2021-12-09 13:30:53 +00:00
// url -> URL ativa em uma das abas abertas.
2021-11-29 15:02:23 +00:00
err = c . HttpCli ( ) . JsonGet ( "/ipc/chrome/findtabbyurl/" + url , & ret )
return ret , err
2021-12-09 13:30:53 +00:00
// ret -> ID do WebSocket contendo a aba encontrada.
2021-11-29 15:02:23 +00:00
}
2021-12-01 20:19:03 +00:00
func ( c * Cli ) FindTabByTitle ( title string ) ( ret string , err error ) {
2021-12-09 13:30:53 +00:00
// title -> Título de uma das abas abertas.
2021-11-29 15:02:23 +00:00
err = c . HttpCli ( ) . JsonGet ( "/ipc/chrome/findtabbytitle/" + title , & ret )
return ret , err
2021-12-09 13:30:53 +00:00
// ret -> ID do WebSocket contendo a aba encontrada.
2021-11-29 15:02:23 +00:00
}
2021-09-30 00:00:49 +00:00
func NewCli ( ) * Cli {
2021-10-11 14:53:44 +00:00
ret := & Cli { ApiCli : api . NewApiCli ( ) }
2021-09-30 00:00:49 +00:00
return ret
2021-12-09 13:30:53 +00:00
// ret -> Nova instância do Chrome para utilização.
2021-09-30 00:00:49 +00:00
}