-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (47 loc) · 1.6 KB
/
script.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const inputText = document.querySelector('[data-input]');
const outputText = document.querySelector('[data-output]');
const trash = document.querySelector('[data-trash]');
const copyClip = document.querySelector('[data-copy]');
var undecodedMessage = '';
//event listeners
inputText.addEventListener('click', clearInputText);
inputText.addEventListener('paste', inputMessage);
trash.addEventListener('click', () => {
clearInputText();
clearOutputText();
});
copyClip.addEventListener('click', copyToClip);
//functions
function clearInputText() {
inputText.value = '';
}
function clearOutputText() {
outputText.value = '';
}
function inputMessage() {
clearInputText();
clearOutputText();
let paste = (event.clipboardData || window.clipboardData).getData('text');
undecodedMessage = paste;
decodeMessage(undecodedMessage);
}
function decodeMessage(undecodedMessage) {
// set output text to whatever our decode fn rets
outputText.value = b64DecodeUnicode(undecodedMessage);
}
function copyToClip() {
copyClip.innerHTML="";
var tempInput = document.createElement("input");
tempInput.style = "position: absolute; left: -1000px; top: -1000px";
tempInput.value = outputText.value;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
}
// base64 to UTF8, fn that works on older browsers
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''));
}