-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (52 loc) · 1.46 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/prop"
"github.com/iocat/youtube"
)
const (
youtubeIframeAPISrc = "https://www.youtube.com/iframe_api"
playerID = "playerID"
)
func main() {
// 1. Add the Youtube script to your header
yourAddScriptFunc(youtubeIframeAPISrc)
// 2. Place the player container as a div in your html with a predefined id
var app = &App{playerID: playerID}
vecty.RenderBody(app)
// 3. Initialize the player when the Youtube API's done loading
js.Global.Set("onYouTubeIframeAPIReady", func() {
// Create and set the initial properties of the player (check the document
// for the specific fields)
var props = youtube.NewProperties()
props.Width = 640
props.Height = 390
props.VideoID = "dQw4w9WgXcQ"
props.PlayerVars.EnableJsAPI = 1
props.Events.OnReady = func(e *youtube.Event) {
e.Target.PlayVideo()
}
// Create and cache the created player
app.player = youtube.NewPlayer(playerID, props)
})
}
type App struct {
vecty.Core
player *youtube.Player
playerID string
}
func (b *App) Render() *vecty.HTML {
return elem.Body(
elem.Div(
prop.ID(b.playerID),
),
)
}
func yourAddScriptFunc(url string) {
script := js.Global.Get("document").Call("createElement", "script")
script.Set("src", url)
script.Set("type", "text/javascript")
js.Global.Get("document").Get("head").Call("appendChild", script)
}