-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
162 lines (143 loc) · 7.5 KB
/
index.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<html>
<head lang="en">
<title>Go Tools</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8"/>
<script src="https://cdn.jsdelivr.net/gh/golang/go@go1.22.5/misc/wasm/wasm_exec.js"></script>
<script>
const go = new Go();
const loadGo = WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject)
</script>
<style>
body, .column {
background-color: linen;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.btn-space {
margin-top: 8px;
}
</style>
</head>
<body>
<h1>Go Tools</h1>
<p>Transforms written in Golang using WASM.</p>
<div class="row">
<div class="column">
<textarea id="transform-text" rows="24" cols="70">{"foo":"bar"}</textarea>
<div>
<button id="ex-json">Example JSON</button>
<button id="ex-html">Example HTML</button>
<button id="ex-bytes">Example Bytes</button>
<button id="gen-uuidv7">Generate UUIDv7</button>
</div>
</div>
<div class="column">
<button id="pretty" disabled>JSON Pretty</button>
<button id="compress" disabled>JSON Compress</button>
<div class="btn-space"></div>
<button id="escape">JSON Escape</button>
<button id="unescape">JSON Unescape</button>
<div class="btn-space"></div>
<button id="b64-encode">Base64 Encode</button>
<button id="b64-decode">Base64 Decode</button>
<div class="btn-space"></div>
<button id="url-encode">URL Encode</button>
<button id="url-decode">URL Decode</button>
<div class="btn-space"></div>
<button id="html-escape">HTML Escape</button>
<button id="html-unescape">HTML Unescape</button>
<div class="btn-space"></div>
<button id="php-serialize-encode">PHP Serialize</button>
<button id="php-serialize-decode">PHP Unserialize</button>
<div class="btn-space"></div>
<button id="string-byte">String to Bytes</button>
<button id="byte-string">Bytes to String</button>
</div>
</div>
<style>#forkongithub a{background:#000;color:#fff;text-decoration:none;font-family:arial,sans-serif;text-align:center;font-weight:bold;padding:5px 40px;font-size:1rem;line-height:2rem;position:relative;transition:0.5s;}#forkongithub a:hover{background:#c11;color:#fff;}#forkongithub a::before,#forkongithub a::after{content:"";width:100%;display:block;position:absolute;top:1px;left:0;height:1px;background:#fff;}#forkongithub a::after{bottom:1px;top:auto;}@media screen and (min-width:800px){#forkongithub{position:fixed;display:block;top:0;right:0;width:200px;overflow:hidden;height:200px;z-index:9999;}#forkongithub a{width:200px;position:absolute;top:60px;right:-60px;transform:rotate(45deg);-webkit-transform:rotate(45deg);-ms-transform:rotate(45deg);-moz-transform:rotate(45deg);-o-transform:rotate(45deg);box-shadow:4px 4px 10px rgba(0,0,0,0.8);}}</style>
<span id="forkongithub"><a href="https://github.com/mvndaai/go_wasm_tools">Fork me on GitHub</a></span>
<script>
const btns = document.querySelectorAll('button');
loadGo.then((result) => {
go.run(result.instance);
document.querySelectorAll('button').forEach(btn => btn.disabled = false);
});
function addOnClick(button, goFn, input, output) {
button.onclick = (e) => {
e.preventDefault();
const k = JSON.parse(golang[goFn](input.value));
console.log(`Response from Go ${goFn}(${input.value})`, k);
if (k.error) {
console.error(k.error)
return;
}
output.value = k.response;
}
}
const transformText = document.querySelector('#transform-text');
transformText.focus();
const pretty = document.querySelector('#pretty');
const compress = document.querySelector('#compress');
addOnClick(pretty, 'prettyJSON', transformText, transformText);
addOnClick(compress, 'compressJSON', transformText, transformText);
const escape = document.querySelector('#escape');
const unescape = document.querySelector('#unescape');
addOnClick(escape, 'escapeJSON', transformText, transformText);
addOnClick(unescape, 'unescapeJSON', transformText, transformText);
const htmlEscape = document.querySelector('#html-escape');
const htmlEnescape = document.querySelector('#html-unescape');
addOnClick(htmlEscape, 'escapeHTML', transformText, transformText);
addOnClick(htmlEnescape, 'unescapeHTML', transformText, transformText);
const b64Encode = document.querySelector('#b64-encode');
const b64Decode = document.querySelector('#b64-decode');
addOnClick(b64Encode, 'bEncode', transformText, transformText);
addOnClick(b64Decode, 'bDecode', transformText, transformText);
const urlEncode = document.querySelector('#url-encode');
const urlDecode = document.querySelector('#url-decode');
addOnClick(urlEncode, 'urlEncode', transformText, transformText);
addOnClick(urlDecode, 'urlDecode', transformText, transformText);
const bytesString = document.querySelector('#byte-string');
const stringBytes = document.querySelector('#string-byte');
addOnClick(bytesString, 'bytesToString', transformText, transformText);
addOnClick(stringBytes, 'stringToBytes', transformText, transformText);
const phpEncode = document.querySelector('#php-serialize-encode');
const phpDecode = document.querySelector('#php-serialize-decode');
addOnClick(phpEncode, 'phpSerializeEncode', transformText, transformText);
addOnClick(phpDecode, 'phpSerializeDecode', transformText, transformText);
// Example text
examples = [
{
selector: '#ex-html',
text: () => '<input/>'
},
{
selector: '#ex-json',
text: () =>'{"foo":"bar"}'
},
{
selector: '#ex-bytes',
text: () => '[72 101 108 108 111 32 87 111 114 108 100]'
},
{
selector: '#gen-uuidv7',
text: () => JSON.parse(golang['genUUIDv7']()).response
}
]
for (const ex of examples) {
document.querySelector(ex.selector).onclick = (e) => {
e.preventDefault();
transformText.value = ex.text();
}
}
</script>
</body>
</html>