Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lorca

import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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{}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -123,15 +125,15 @@ 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 {
return "", err
}
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 {
Expand All @@ -150,15 +152,15 @@ 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 {
return "", err
}
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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 26 additions & 0 deletions examples/password/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
41 changes: 40 additions & 1 deletion ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
}