-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.js
133 lines (109 loc) · 4.31 KB
/
send.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
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
function init() {
for (let id of ['sender-input', 'recipient-input', 'message']) {
document.getElementById(id).addEventListener('input', function(e) {
if (this.value.includes('=') || this.value.includes('&')) {
this.value = this.value.replaceAll('&', '').replaceAll('=', '');
alert('Please don\'t use = or & in your inputs.');
}
})
}
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(arg1, arg2) {
let toRet = this.slice();
while (toRet.includes(arg1)) {
toRet = toRet.replace(arg1, arg2);
}
return toRet;
}
}
document.getElementById('generate-btn').addEventListener('click', function(e) {
e.preventDefault();
for (let cls of ['freepik', 'result', 'share']) {
let existing = document.getElementsByClassName(cls);
if (existing) {
for (let y of existing) {
y.remove();
}
}
}
let result = document.createElement('div');
result.classList.add('result');
let url = generateLink();
let resInfo = document.createElement('div');
resInfo.style.textAlign = 'center';
resInfo.append('Your generated link: ');
result.appendChild(resInfo);
let link = document.createElement('input');
link.classList.add('link');
link.value = url;
result.appendChild(link);
let copyBtn = document.createElement('button');
copyBtn.innerHTML = "Copy";
copyBtn.addEventListener('click', function() {
let linkField = document.querySelector('.link');
linkField.click();
linkField.select();
linkField.setSelectionRange(0, linkField.value.length);
document.execCommand('copy');
});
let viewBtn = document.createElement('button');
viewBtn.innerHTML = "View";
viewBtn.onclick = function() {
window.location.replace(url);
}
let actions = document.createElement('div');
actions.appendChild(copyBtn);
actions.appendChild(viewBtn);
actions.classList.add('actions');
result.appendChild(actions);
document.body.appendChild(result);
let shareDiv = document.createElement('div');
shareDiv.classList.add('share');
shareDiv.append('Share with your friend! ');
let waBtn = document.createElement('button');
waBtn.classList.add('icon');
waBtn.style.backgroundImage = 'url(\'img/whatsapp.svg\')';
shareDiv.appendChild(waBtn);
waBtn.onclick = function() {
window.location.replace(`https://api.whatsapp.com/send?text=${encodeURIComponent(url)}`)
}
let emailBtn = document.createElement('button');
emailBtn.classList.add('icon');
emailBtn.style.backgroundImage = 'url(\'img/arroba.svg\')';
shareDiv.appendChild(emailBtn);
emailBtn.onclick = function() {
window.location.replace(`mailto:?body=${encodeURIComponent(url)}`)
}
document.body.appendChild(shareDiv);
let legalese = document.createElement('div');
legalese.id = 'legalese';
legalese.classList.add('freepik');
legalese.innerHTML = 'WhatsApp and e-mail icons by Freepik';
document.body.appendChild(legalese);
})
document.getElementById('message').addEventListener('input', function(e) {
if (this.value.length > 140) {
this.value = this.value.substring(0, 140);
}
})
}
function generateLink() {
let raw = {
sender: document.getElementById('sender-input').value,
recipient: document.getElementById('recipient-input').value,
message: document.getElementById('message').value
}
cleaned = {}
encoded = {}
let params = new URLSearchParams();
params.set("new", "true");
for (let key of Object.keys(raw)) {
cleaned[key] = raw[key].replaceAll('>', '>').replaceAll('<', '<');
encoded[key] = Base64.encode(cleaned[key]);
params.set(key, encoded[key]);
}
let str = window.location.href.replace('send.html', 'index.html');
str += `?${params.toString()}`;
return str;
}
window.onload = init;