-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
83 lines (69 loc) · 2.13 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gofirefox
import (
"context"
"fmt"
"os"
"strings"
)
// UI interface allows talking to the HTML5 UI from Go.
type UI interface {
Load(url string) error
Run(ctx context.Context) error
Stop() error
}
type ui struct {
firefox *firefox
done chan struct{}
}
var defaultArgs = []string{}
// 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
// string - a temporary directory is created and it will be removed on
// ui.Stop().
// There are 3 execution modes:
// 1. url only is provided - run firefox with the url
// 2. url is provided with prefix "data:" - run firefox with the url encoded content data:
// 3. url is directory with index.html as postfix - serve directory as file://
func New(url string, customArgs, userPreferences []string) (UI, error) {
// there is 3 execution modes:
// 1. url only is provided - run firefox with the url
// 2. url is provided with prefix "data:" - run firefox with the url (same code behaviour as 1)
// 3. url and dir is provided - service directory and open file provided in url
if url == "" {
url = "data:text/html,<html>Hello from Unikiosk!</html>"
}
// split for parsing
urlParts := strings.Split(url, "/")
postfix := urlParts[len(urlParts)-1]
if strings.Contains(postfix, ".html") || strings.Contains(postfix, ".htm") || strings.Contains(postfix, ".php") {
_, err := os.Stat(url)
if err != nil {
return nil, err
}
url = "file://" + url
}
args := customArgs
args = append(args, fmt.Sprintf("--new-window=%s", url))
args = append(args, "--kiosk")
firefox, err := new(args, userPreferences)
if err != nil {
return nil, err
}
return &ui{firefox: firefox}, nil
}
func (u *ui) Stop() error {
// ignore err, as the chrome process might be already dead, when user close the window.
err := u.firefox.stop()
if err != nil {
return err
}
<-u.done
return nil
}
func (u *ui) Load(url string) error {
return u.firefox.load(url)
}
func (u *ui) Run(ctx context.Context) error {
return u.firefox.run(ctx)
}