-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
47 lines (39 loc) · 1.06 KB
/
main.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
package main
import (
"net/url"
"github.com/zserge/lorca"
)
var ui lorca.UI
func main() {
ui, _ = lorca.New("", "", 480, 320)
defer ui.Close()
// GoのfuncをJavaScriptで呼び出せるようBind
ui.Bind("helloFromGo", helloFromGo)
// 今回はurl.PathEscapeでそのままHTMLを配置
// ※下記はHTTPサーバをたててそこから取得する例
// https://github.com/zserge/lorca/tree/master/examples/counter
ui.Load("data:text/html," + url.PathEscape(`
<!doctype html>
<html lang="ja">
<head>
<title>Lorca Sample</title>
<script>
function helloFromJavaScript() {
return 'Hello from JavaScript!'
}
</script>
</head>
<body>
<button onclick="helloFromGo()"> Press Me! </button>
<div id="content"></div>
</body>
</html>
`))
<-ui.Done()
}
func helloFromGo() {
// JavaScriptのfunction呼び出し
msg := ui.Eval(`helloFromJavaScript();`).String() + "<br>" + "Hello From Go!"
// HTMLに反映
ui.Eval(`document.getElementById('content').innerHTML="` + msg + `";`)
}