-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (77 loc) · 1.88 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io/ioutil"
"strings"
"syscall/js"
)
var (
host string
hash string
)
func main() {
c := make(chan struct{}, 0)
js.Global().Set("url", js.FuncOf(getCurrentUrl))
js.Global().Set("copy", js.FuncOf(copyURL))
fmt.Println("Initialized!")
<-c
}
func getCurrentUrl(this js.Value, args []js.Value) interface{} {
//fmt.Println("Getting url...")
host = args[0].String() + args[2].String()
hash = strings.ReplaceAll(args[1].String(), "#", "")
//encoded := strings.ReplaceAll(args[0].String(), "#", "")
fmt.Println("Host:" + host)
fmt.Println("Hash:" + hash)
if hash != "" {
text := decode(hash)
document := js.Global().Get("document")
editor := document.Call("getElementById", "editor")
editor.Set("value", text)
}
return js.ValueOf(nil)
}
func copyURL(this js.Value, args []js.Value) interface{} {
//host = args[0].String()
fmt.Println("Copy")
document := js.Global().Get("document")
editor := document.Call("getElementById", "editor")
text := editor.Get("value").String()
//fmt.Println("Text\n" + text)
encoded := encode(text)
url := host + "#" + encoded
fmt.Println("URL:" + url)
js.Global().Get("navigator").Get("clipboard").Call("writeText", js.ValueOf(url))
return nil
}
func encode(text string) string {
textbytes := []byte(text)
var b bytes.Buffer
gz := gzip.NewWriter(&b)
_, err := gz.Write(textbytes)
if err != nil {
fmt.Println(err)
}
gz.Close()
return base64.URLEncoding.EncodeToString(b.Bytes())
}
func decode(encoded string) string {
decodedbytes, err := base64.URLEncoding.DecodeString(encoded)
if err != nil {
fmt.Print("error: ")
fmt.Println(err)
}
gz, err1 := gzip.NewReader(bytes.NewReader(decodedbytes))
if err1 != nil {
fmt.Println(err1)
}
//fix for Go >1.13
decoded, err2 := ioutil.ReadAll(gz)
if err2 != nil {
fmt.Println(err2)
}
return string(decoded)
}