-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-encoder-and-decoder-tool.js
More file actions
64 lines (48 loc) · 1.63 KB
/
Copy pathhtml-encoder-and-decoder-tool.js
File metadata and controls
64 lines (48 loc) · 1.63 KB
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
// HTML Encoder and Decoder Tool
// developed by Tawhidur Rahman Dear, https://www.tawhidurrahmandear.com
// Live Preview available at https://www.devilhunter.net/p/html-encoder-and-decoder-tool.html
/* ========= HTML Encoder / Decoder ========= */
function encodeHTML(str) {
if (!str) return '';
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function decodeHTML(str) {
if (!str) return '';
var textarea = document.createElement('textarea');
textarea.innerHTML = str;
return textarea.value;
}
/* ========= Live Encode / Decode ========= */
document.getElementById('decoded').addEventListener('input', function () {
document.getElementById('encoded').value = encodeHTML(this.value);
});
document.getElementById('encoded').addEventListener('input', function () {
document.getElementById('decoded').value = decodeHTML(this.value);
});
/* ========= Copy Functions ========= */
function copyDecoded() {
copyText('decoded');
}
function copyEncoded() {
copyText('encoded');
}
function copyText(id) {
var el = document.getElementById(id);
el.select();
el.setSelectionRange(0, 999999);
document.execCommand('copy');
}
/* ========= Clear Functions ========= */
function eraseDecoded() {
document.getElementById('decoded').value = '';
document.getElementById('encoded').value = '';
}
function eraseEncoded() {
document.getElementById('encoded').value = '';
document.getElementById('decoded').value = '';
}