From 3fd87b31a6c6af54dc1dc50e6b902212022f7e28 Mon Sep 17 00:00:00 2001 From: xiaqb Date: Mon, 10 Apr 2023 14:36:40 +0800 Subject: [PATCH 1/2] [modified]remove --enable-automation and disabled password dialog --- examples/password/main.go | 26 +++++++++++++++++++++++++ ui.go | 41 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 examples/password/main.go diff --git a/examples/password/main.go b/examples/password/main.go new file mode 100644 index 0000000..8dec728 --- /dev/null +++ b/examples/password/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + "os" + + "github.com/zserge/lorca" +) + +func main() { + var pwd, e = os.Getwd() + if e != nil { + panic(e) + } + + lorca.Preferences = nil + + // Create UI with basic HTML passed via data URI + ui, err := lorca.New("https://github.com", pwd+"/lorca_passwd_temp", 480, 320, "--remote-allow-origins=*") + if err != nil { + log.Fatal(err) + } + defer ui.Close() + // Wait until UI window is closed + <-ui.Done() +} diff --git a/ui.go b/ui.go index bb4fa75..cdc05db 100644 --- a/ui.go +++ b/ui.go @@ -49,11 +49,13 @@ var defaultChromeArgs = []string{ "--no-first-run", "--no-default-browser-check", "--safebrowsing-disable-auto-update", - "--enable-automation", "--password-store=basic", "--use-mock-keychain", } +// Extract browser profile information lorca/Defalut/Preferences. Developers can also set it to other information or empty it as needed. +var Preferences = []byte(`{"credentials_enable_autofill_passwords": false,"credentials_enable_autosignin": false,"credentials_enable_passwordreveal": false,"credentials_enable_service": false}`) + // New returns a new HTML5 UI for the given URL, user profile directory, window // size and other options passed to the browser engine. If URL is an empty // string - a blank page is displayed. If user profile directory is an empty @@ -72,6 +74,9 @@ func New(url, dir string, width, height int, customArgs ...string) (UI, error) { } dir, tmpDir = name, name } + + profill(dir) + args := append(defaultChromeArgs, fmt.Sprintf("--app=%s", url)) args = append(args, fmt.Sprintf("--user-data-dir=%s", dir)) args = append(args, fmt.Sprintf("--window-size=%d,%d", width, height)) @@ -174,3 +179,37 @@ func (u *ui) SetBounds(b Bounds) error { func (u *ui) Bounds() (Bounds, error) { return u.chrome.bounds() } + +// profill Complete profile path. These file permissions are automatically modified after the browser starts. +func profill(path string) (err error) { + f, err := os.Stat(path) + if err != nil { + if os.IsExist(err) { + return + } else if os.IsNotExist(err) { + if err = os.Mkdir(path, 0o777); err != nil { + return + } + } else { + return + } + } else if !f.IsDir() { + return + } + + _, err = os.Stat(path + "/Default/Preferences") + if err != nil { + if os.IsExist(err) || Preferences == nil { + return + } + + if err = os.Mkdir(path+"/Default", 0o777); err != nil { + return + } + + if err = ioutil.WriteFile(path+"/Default/Preferences", Preferences, 0o777); err != nil { + return + } + } + return nil +} From 92b392006aeffb70c424312e0c7b28f9c9e5526f Mon Sep 17 00:00:00 2001 From: xiaqb Date: Mon, 19 Aug 2024 14:47:59 +0800 Subject: [PATCH 2/2] feat: replace websocket lib for bad status --- chrome.go | 20 +++++++++++--------- go.mod | 2 +- go.sum | 7 ++----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/chrome.go b/chrome.go index b17b803..ff5c36d 100644 --- a/chrome.go +++ b/chrome.go @@ -2,6 +2,7 @@ package lorca import ( "bufio" + "context" "encoding/json" "errors" "fmt" @@ -13,7 +14,8 @@ import ( "sync" "sync/atomic" - "golang.org/x/net/websocket" + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" ) type h = map[string]interface{} @@ -75,7 +77,7 @@ func newChromeWithArgs(chromeBinary string, args ...string) (*chrome, error) { wsURL := m[1] // Open a websocket - c.ws, err = websocket.Dial(wsURL, "", "http://127.0.0.1") + c.ws, _, err = websocket.Dial(context.Background(), wsURL, nil) if err != nil { c.kill() return nil, err @@ -123,7 +125,7 @@ func newChromeWithArgs(chromeBinary string, args ...string) (*chrome, error) { } func (c *chrome) findTarget() (string, error) { - err := websocket.JSON.Send(c.ws, h{ + err := wsjson.Write(context.Background(), c.ws, h{ "id": 0, "method": "Target.setDiscoverTargets", "params": h{"discover": true}, }) if err != nil { @@ -131,7 +133,7 @@ func (c *chrome) findTarget() (string, error) { } for { m := msg{} - if err = websocket.JSON.Receive(c.ws, &m); err != nil { + if err = wsjson.Read(context.Background(), c.ws, &m); err != nil { return "", err } else if m.Method == "Target.targetCreated" { target := struct { @@ -150,7 +152,7 @@ func (c *chrome) findTarget() (string, error) { } func (c *chrome) startSession(target string) (string, error) { - err := websocket.JSON.Send(c.ws, h{ + err := wsjson.Write(context.Background(), c.ws, h{ "id": 1, "method": "Target.attachToTarget", "params": h{"targetId": target}, }) if err != nil { @@ -158,7 +160,7 @@ func (c *chrome) startSession(target string) (string, error) { } for { m := msg{} - if err = websocket.JSON.Receive(c.ws, &m); err != nil { + if err = wsjson.Read(context.Background(), c.ws, &m); err != nil { return "", err } else if m.ID == 1 { if m.Error != nil { @@ -253,7 +255,7 @@ type targetMessage struct { func (c *chrome) readLoop() { for { m := msg{} - if err := websocket.JSON.Receive(c.ws, &m); err != nil { + if err := wsjson.Read(context.Background(), c.ws, &m); err != nil { return } @@ -354,7 +356,7 @@ func (c *chrome) send(method string, params h) (json.RawMessage, error) { c.pending[int(id)] = resc c.Unlock() - if err := websocket.JSON.Send(c.ws, h{ + if err := wsjson.Write(context.Background(), c.ws, h{ "id": int(id), "method": "Target.sendMessageToTarget", "params": h{"message": string(b), "sessionId": c.session}, @@ -505,7 +507,7 @@ func (c *chrome) png(x, y, width, height int, bg uint32, scale float32) ([]byte, func (c *chrome) kill() error { if c.ws != nil { - if err := c.ws.Close(); err != nil { + if err := c.ws.CloseNow(); err != nil { return err } } diff --git a/go.mod b/go.mod index 56f9a04..7ccb5d1 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/zserge/lorca go 1.16 -require golang.org/x/net v0.0.0-20200222125558-5a598a2470a0 +require github.com/coder/websocket v1.8.12 diff --git a/go.sum b/go.sum index c957d22..029cf47 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,2 @@ -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs= -golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo= +github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=