-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm.go
More file actions
46 lines (38 loc) · 1018 Bytes
/
wasm.go
File metadata and controls
46 lines (38 loc) · 1018 Bytes
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
//go:build wasm
package main
import (
"bytes"
"strconv"
"syscall/js"
"github.com/spenserblack/zal-go/corrupter"
)
func main() {
document := js.Global().Get("document")
getElementById := func(id string) js.Value {
return document.Call("getElementById", id)
}
inputTextarea := getElementById("zalgo-input")
outputEl := getElementById("zalgo-output")
minCorruption := must(intValue(getElementById("minimum-corruption")))
maxCorruption := must(intValue(getElementById("maximum-corruption")))
buf := new(bytes.Buffer)
corrupter := corrupter.New(buf)
corrupter.Min = minCorruption
corrupter.Max = maxCorruption
rawText := inputTextarea.Get("value").String()
for _, r := range rawText {
corrupter.WriteRune(r)
}
corruptedBytes := buf.Bytes()
outputEl.Set("textContent", string(corruptedBytes))
}
func intValue(el js.Value) (int, error) {
value := el.Get("value").String()
return strconv.Atoi(value)
}
func must[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}