-
Notifications
You must be signed in to change notification settings - Fork 2
/
giohyperlink_js.go
55 lines (48 loc) · 1.48 KB
/
giohyperlink_js.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
// +build js
package giohyperlink
import (
"gioui.org/io/event"
"gioui.org/io/system"
"net/url"
"syscall/js"
)
var (
_document = js.Global().Get("document")
_body = js.Global().Get("document").Get("body")
)
func listenEvents(event event.Event) {
if _, ok := event.(system.StageEvent); ok {
links := _body.Call("querySelectorAll", "a.giohyperlink")
if !links.Truthy() {
return
}
for i := 0; i < links.Length(); i++ {
_body.Call("removeChild", links.Index(0))
}
}
}
func open(u *url.URL) error {
if ok := js.Global().Call("open", u.String(), "_blank", "noreferrer,noopener").Truthy(); !ok {
// If there's a error let's use the hacky way:
// It will create a "fullscreen <a>", which clicking will
// open the URL.
// Generally, it will need two clicks to open the URL.
// We can't hook into `a` (adding `a.addEvenetListener("click")` will make it fail again,
// not sure why.
// We remove this `a` when the app lost focus (based on Page Visibility API, which Gio relies on).
a := _document.Call("createElement", "a")
a.Set("href", u.String())
a.Set("target", "_blank")
a.Set("rel", "noreferrer,noopener")
a.Set("innerText", " ")
a.Get("classList").Call("add", "giohyperlink")
a.Get("style").Set("display", "block")
a.Get("style").Set("width", "100vw")
a.Get("style").Set("height", "100vh")
a.Get("style").Set("position", "fixed")
a.Get("style").Set("top", "0")
a.Get("style").Set("z-index", "100")
_body.Call("appendChild", a)
}
return nil
}