replaycli-go/api/chrome/cli.go

91 lines
2.1 KiB
Go
Raw Permalink Normal View History

2021-09-30 00:00:49 +00:00
package chrome
import (
"encoding/json"
"fmt"
"go.digitalcircle.com.br/open/replaycli-go/api"
)
2021-11-14 13:31:07 +00:00
type Req struct {
To int
Url string
Id string
Eval string
Title string
Msg map[string]interface{}
}
type Res struct {
Id string
TabMetadata []TabMetadata
Ret map[string]interface{}
Raw []byte
}
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
type Cli struct {
*api.ApiCli
2021-09-30 00:00:49 +00:00
}
//func (c *Cli) HttpCli() *httpcli.Cli {
// return c.ApiCli.HttpCli()
//}
2021-09-30 00:00:49 +00:00
func (c *Cli) Start(to int) error {
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 {
err := c.HttpCli().JsonGet("/ipc/chrome/startHeadless", nil)
2021-09-30 00:00:49 +00:00
return err
}
func (c *Cli) Stop() error {
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) {
ret := ""
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/chrome/new?url=%s", url), &ret)
2021-09-30 00:00:49 +00:00
return ret, err
}
func (c *Cli) Close(id string) error {
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) {
ret := make(map[string]interface{})
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
}
func (c *Cli) Wait(id string, s string, to int) (string, error) {
ret := ""
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) {
ret := ""
in := map[string]interface{}{
"method": m,
"params": ps,
}
err := c.HttpCli().JsonPost("/ipc/chrome/eval/"+id, in, &ret)
2021-09-30 00:00:49 +00:00
return ret, err
}
func NewCli() *Cli {
ret := &Cli{ApiCli: api.NewApiCli()}
2021-09-30 00:00:49 +00:00
return ret
}