Spaces:
Running
Running
Set default IP to 192.168.0.1 without prefix and theme dropdown options
Browse files- app.js +37 -24
- index.html +1 -1
- style.css +5 -0
app.js
CHANGED
|
@@ -154,31 +154,52 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 154 |
|
| 155 |
// Live calculation function
|
| 156 |
function update() {
|
| 157 |
-
|
| 158 |
if (!rawVal) {
|
| 159 |
clearResults();
|
| 160 |
hideError();
|
| 161 |
return;
|
| 162 |
}
|
| 163 |
|
| 164 |
-
|
| 165 |
-
if (
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
clearResults();
|
| 168 |
return;
|
| 169 |
}
|
| 170 |
|
| 171 |
hideError();
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
prefixDropdown.value = parsed.prefix;
|
| 176 |
-
} else {
|
| 177 |
-
// If typing without slash, update input with slash when dropdown is used
|
| 178 |
-
cidrInput.value = `${parsed.ip}/${prefixDropdown.value}`;
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
const results = calculateCIDR(parsed.ip, parsed.prefix);
|
| 182 |
displayResults(results);
|
| 183 |
}
|
| 184 |
|
|
@@ -212,16 +233,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 212 |
// Listeners
|
| 213 |
cidrInput.addEventListener('input', update);
|
| 214 |
|
| 215 |
-
prefixDropdown.addEventListener('change',
|
| 216 |
-
const rawVal = cidrInput.value.trim();
|
| 217 |
-
if (rawVal) {
|
| 218 |
-
const parsed = parseCIDR(rawVal);
|
| 219 |
-
if (parsed) {
|
| 220 |
-
cidrInput.value = `${parsed.ip}/${prefixDropdown.value}`;
|
| 221 |
-
update();
|
| 222 |
-
}
|
| 223 |
-
}
|
| 224 |
-
});
|
| 225 |
|
| 226 |
// Copy to Clipboard
|
| 227 |
document.querySelectorAll('.copy-btn').forEach(btn => {
|
|
@@ -274,8 +286,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 274 |
applyTheme(themes[currentThemeIndex]);
|
| 275 |
});
|
| 276 |
|
| 277 |
-
// Init theme
|
| 278 |
applyTheme('system');
|
|
|
|
| 279 |
});
|
| 280 |
|
| 281 |
// --- PWA Service Worker Registration ---
|
|
|
|
| 154 |
|
| 155 |
// Live calculation function
|
| 156 |
function update() {
|
| 157 |
+
let rawVal = cidrInput.value.trim();
|
| 158 |
if (!rawVal) {
|
| 159 |
clearResults();
|
| 160 |
hideError();
|
| 161 |
return;
|
| 162 |
}
|
| 163 |
|
| 164 |
+
// Auto-sync prefix dropdown if typed/pasted with a slash, then clean the input field to show only the IP
|
| 165 |
+
if (rawVal.includes('/')) {
|
| 166 |
+
const parts = rawVal.split('/');
|
| 167 |
+
const ipPart = parts[0].trim();
|
| 168 |
+
const prefixPart = parts[1].trim();
|
| 169 |
+
if (/^\d+$/.test(prefixPart)) {
|
| 170 |
+
const prefixVal = parseInt(prefixPart, 10);
|
| 171 |
+
if (prefixVal >= 0 && prefixVal <= 32) {
|
| 172 |
+
prefixDropdown.value = prefixVal;
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
cidrInput.value = ipPart;
|
| 176 |
+
rawVal = ipPart;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// Validate IP format
|
| 180 |
+
const ipRegex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
| 181 |
+
const match = rawVal.match(ipRegex);
|
| 182 |
+
let isValidIp = true;
|
| 183 |
+
if (!match) {
|
| 184 |
+
isValidIp = false;
|
| 185 |
+
} else {
|
| 186 |
+
for (let i = 1; i <= 4; i++) {
|
| 187 |
+
const val = parseInt(match[i], 10);
|
| 188 |
+
if (val < 0 || val > 255) isValidIp = false;
|
| 189 |
+
if (match[i].length > 1 && match[i].startsWith('0')) isValidIp = false;
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
if (!isValidIp) {
|
| 194 |
+
showError('Invalid IPv4 address. Use format: e.g. 192.168.0.1');
|
| 195 |
clearResults();
|
| 196 |
return;
|
| 197 |
}
|
| 198 |
|
| 199 |
hideError();
|
| 200 |
|
| 201 |
+
const prefix = parseInt(prefixDropdown.value, 10);
|
| 202 |
+
const results = calculateCIDR(rawVal, prefix);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
displayResults(results);
|
| 204 |
}
|
| 205 |
|
|
|
|
| 233 |
// Listeners
|
| 234 |
cidrInput.addEventListener('input', update);
|
| 235 |
|
| 236 |
+
prefixDropdown.addEventListener('change', update);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
// Copy to Clipboard
|
| 239 |
document.querySelectorAll('.copy-btn').forEach(btn => {
|
|
|
|
| 286 |
applyTheme(themes[currentThemeIndex]);
|
| 287 |
});
|
| 288 |
|
| 289 |
+
// Init theme and calculation
|
| 290 |
applyTheme('system');
|
| 291 |
+
update();
|
| 292 |
});
|
| 293 |
|
| 294 |
// --- PWA Service Worker Registration ---
|
index.html
CHANGED
|
@@ -35,7 +35,7 @@
|
|
| 35 |
<div class="input-section">
|
| 36 |
<div class="input-group">
|
| 37 |
<div class="input-wrapper">
|
| 38 |
-
<input type="text" id="cidr-input" placeholder="e.g. 192.168.
|
| 39 |
</div>
|
| 40 |
<select id="prefix-dropdown" aria-label="Prefix Length">
|
| 41 |
<!-- Prefixes populated via JS -->
|
|
|
|
| 35 |
<div class="input-section">
|
| 36 |
<div class="input-group">
|
| 37 |
<div class="input-wrapper">
|
| 38 |
+
<input type="text" id="cidr-input" value="192.168.0.1" placeholder="e.g. 192.168.0.1" autocomplete="off" spellcheck="false">
|
| 39 |
</div>
|
| 40 |
<select id="prefix-dropdown" aria-label="Prefix Length">
|
| 41 |
<!-- Prefixes populated via JS -->
|
style.css
CHANGED
|
@@ -230,6 +230,11 @@ select {
|
|
| 230 |
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
| 231 |
}
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
input[type="text"]:focus, select:focus {
|
| 234 |
outline: none;
|
| 235 |
border-color: var(--input-focus-border);
|
|
|
|
| 230 |
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
| 231 |
}
|
| 232 |
|
| 233 |
+
select option {
|
| 234 |
+
background-color: var(--bg-color-2);
|
| 235 |
+
color: var(--text-main);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
input[type="text"]:focus, select:focus {
|
| 239 |
outline: none;
|
| 240 |
border-color: var(--input-focus-border);
|