forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl-encode.html
More file actions
162 lines (138 loc) · 5.25 KB
/
url-encode.html
File metadata and controls
162 lines (138 loc) · 5.25 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URL Encoder</title>
<link rel="stylesheet" href="styles.css">
<style>
body {
max-width: 960px;
margin: 0 auto;
padding: clamp(1.5rem, 3vw, 2.5rem) clamp(1.25rem, 3vw, 2.75rem) clamp(3rem, 4vw, 4rem);
}
header {
margin-bottom: clamp(1.5rem, 3vw, 2.5rem);
}
main {
display: grid;
gap: 1.25rem;
}
.tool-card {
padding: clamp(1.25rem, 3vw, 2rem);
}
.tool-actions {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
}
textarea {
width: 100%;
min-height: 180px;
}
.status {
margin: 0.25rem 0 0;
color: var(--tx-3);
min-height: 1.25rem;
}
.secondary {
background: var(--bg);
color: var(--tx);
border: 1px solid var(--ui-2);
}
.secondary:hover,
.secondary:focus-visible {
color: var(--accent);
border-color: var(--accent);
}
@media (max-width: 640px) {
body {
padding: 1.25rem 1rem 2.5rem;
}
}
</style>
</head>
<body>
<header class="page-header">
<a class="site-link" href="https://tools.mathspp.com/" aria-label="Back to tools.mathspp.com">← tools.mathspp.com</a>
<h1>URL Encoder</h1>
<p class="lead">Convert text into a URL-safe encoded string and copy it with one click.</p>
</header>
<main>
<section class="surface tool-card">
<form id="encode-form" autocomplete="off">
<div class="form-group">
<label for="input-text">Text to encode</label>
<textarea id="input-text" name="input-text" placeholder="Paste any text to make it safe for a URL" spellcheck="false"></textarea>
</div>
<div class="tool-actions">
<button type="submit">Encode for URL</button>
<button type="button" class="secondary" id="clear-button">Clear</button>
</div>
</form>
</section>
<section class="surface tool-card" aria-live="polite">
<h2>Encoded content</h2>
<p class="lead">The encoded string appears here. Copy it to use in query strings or path segments.</p>
<div class="form-group">
<label for="encoded-text">URL encoded string</label>
<textarea id="encoded-text" readonly placeholder="Your encoded text will appear here"></textarea>
</div>
<div class="tool-actions">
<button type="button" id="copy-button" disabled>Copy encoded text</button>
</div>
<p class="status" id="copy-status" aria-live="polite"></p>
</section>
</main>
<script>
(function () {
const form = document.getElementById('encode-form');
const input = document.getElementById('input-text');
const encodedOutput = document.getElementById('encoded-text');
const copyButton = document.getElementById('copy-button');
const clearButton = document.getElementById('clear-button');
const copyStatus = document.getElementById('copy-status');
function encodeInput() {
const rawText = input.value || '';
const encoded = encodeURIComponent(rawText);
encodedOutput.value = encoded;
copyButton.disabled = encoded.length === 0;
copyStatus.textContent = '';
}
form.addEventListener('submit', event => {
event.preventDefault();
encodeInput();
});
clearButton.addEventListener('click', () => {
input.value = '';
encodedOutput.value = '';
copyButton.disabled = true;
copyStatus.textContent = '';
input.focus();
});
copyButton.addEventListener('click', async () => {
const textToCopy = encodedOutput.value;
if (!textToCopy) {
return;
}
try {
if (navigator.clipboard && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(textToCopy);
} else {
encodedOutput.select();
document.execCommand('copy');
}
copyStatus.textContent = 'Copied to clipboard';
copyStatus.style.color = 'var(--tx-2)';
} catch (error) {
copyStatus.textContent = 'Copy failed. You can select the text and copy it manually.';
copyStatus.style.color = 'var(--rd)';
}
});
})();
</script>
<footer class="page-footer">
<p>Built with ❤️, 🤖, and 🐍, by <a href="https://mathspp.com/">Rodrigo Girão Serrão</a></p>
</footer>
</body>
</html>