-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.js
37 lines (36 loc) · 849 Bytes
/
base64.js
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
const template = `<section>
<h2>BASE64</h2>
<p>Decoded string</p>
<textarea class="half-height" v-on:input="encode" v-model="decoded"></textarea>
<p>Encoded string</p>
<textarea class="half-height" v-on:input="decode" v-model="encoded"></textarea>
<p class="invalid" v-if="error"><small>{{ error}}</small></p>
</section>`
export default {
template,
data: function() {
return {
encoded: "",
decoded: "",
error: null
}
},
mounted: function() {
this.decoded = 'Hello, world!'
this.encode()
},
methods: {
encode: function() {
this.error = null
this.encoded = btoa(this.decoded)
},
decode: function() {
this.error = null
try {
this.decoded = atob(this.encoded)
} catch (err) {
this.error = `Invalid base64 string: ${err.message}`
}
}
}
}