Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 107 additions & 104 deletions scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,136 +2,139 @@

// Cache DOM elements
const elements = {
toolsContainer: null,
ipInput: null,
pingUrl: null,
myIp: null,
ipInfo: null,
pingResults: null,
errorMessage: null
toolsContainer: null,
ipInput: null,
pingUrl: null,
myIp: null,
ipInfo: null,
pingResults: null,
errorMessage: null,
};

// Initialize DOM elements and event listeners
function initializeApp() {
// Cache DOM elements
elements.toolsContainer = document.getElementById('tools-container');
elements.ipInput = document.getElementById('ip-input');
elements.pingUrl = document.getElementById('ping-url');
elements.myIp = document.getElementById('my-ip');
elements.ipInfo = document.getElementById('ip-info');
elements.pingResults = document.getElementById('ping-results');
elements.errorMessage = document.getElementById('error-message');

// Add Enter key support
if (elements.ipInput) {
elements.ipInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') lookupIP();
});
}
// Cache DOM elements
elements.toolsContainer = document.getElementById("tools-container");
elements.ipInput = document.getElementById("ip-input");
elements.pingUrl = document.getElementById("ping-url");
elements.myIp = document.getElementById("my-ip");
elements.ipInfo = document.getElementById("ip-info");
elements.pingResults = document.getElementById("ping-results");
elements.errorMessage = document.getElementById("error-message");

// Add Enter key support
if (elements.ipInput) {
elements.ipInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") lookupIP();
});
}

if (elements.pingUrl) {
elements.pingUrl.addEventListener('keypress', (e) => {
if (e.key === 'Enter') ping();
});
}
if (elements.pingUrl) {
elements.pingUrl.addEventListener("keypress", (e) => {
if (e.key === "Enter") ping();
});
}
}

// Show/hide the tools container
function openNetworkTools() {
const container = document.getElementById('tools-container');
if (container) {
container.style.display = container.style.display === 'none' ? 'block' : 'none';
}
const container = document.getElementById("tools-container");
if (container) {
container.style.display =
container.style.display === "none" ? "block" : "none";
}
}

// Get user's IP address
async function getMyIP() {
try {
const response = await fetch('https://api.ipify.org?format=json');
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
const myIpElement = document.getElementById('my-ip');
if (myIpElement) {
myIpElement.textContent = `Your IP: ${data.ip}`;
}
} catch (error) {
showError('Unable to fetch IP address');
console.error('Error fetching IP:', error);
try {
const response = await fetch("https://api.ipify.org?format=json");
if (!response.ok) throw new Error("Network response was not ok");
const data = await response.json();
const myIpElement = document.getElementById("my-ip");
if (myIpElement) {
myIpElement.textContent = `Your IP: ${data.ip}`;
}
} catch (error) {
showError("Unable to fetch IP address");
console.error("Error fetching IP:", error);
}
}

// Lookup IP or domain
async function lookupIP() {
try {
const input = elements.ipInput?.value?.trim();
if (!input) throw new Error('Please enter an IP or domain');

const response = await fetch(`https://dns.google/resolve?name=${input}`);
if (!response.ok) throw new Error('Lookup failed');
const data = await response.json();

if (elements.ipInfo) {
elements.ipInfo.textContent = data.Answer
? `Resolved: ${data.Answer[0].data}`
: 'Lookup failed';
}
} catch (error) {
showError(error.message || 'Unable to lookup address');
console.error('Error during lookup:', error);
try {
const input = elements.ipInput?.value?.trim();
if (!input) throw new Error("Please enter an IP or domain");

const response = await fetch(`https://dns.google/resolve?name=${input}`);
if (!response.ok) throw new Error("Lookup failed");
const data = await response.json();

if (elements.ipInfo) {
elements.ipInfo.textContent = data.Answer
? `Resolved: ${data.Answer[0].data}`
: "Lookup failed";
}
} catch (error) {
showError(error.message || "Unable to lookup address");
console.error("Error during lookup:", error);
}
}

// Ping function
async function ping() {
try {
const input = elements.pingUrl?.value?.trim();
if (!input) throw new Error('Please enter a URL');

const startTime = performance.now();
await fetch(`https://${input.replace(/^https?:\/\//, '')}`, {
mode: 'no-cors'
});

if (elements.pingResults) {
elements.pingResults.textContent =
`Ping time: ${Math.round(performance.now() - startTime)}ms`;
}
} catch (error) {
showError('Unable to ping the specified URL');
console.error('Error during ping:', error);
try {
const input = elements.pingUrl?.value?.trim();
if (!input) throw new Error("Please enter a URL");

const startTime = performance.now();
await fetch(`https://${input.replace(/^https?:\/\//, "")}`, {
mode: "no-cors",
});

if (elements.pingResults) {
elements.pingResults.textContent = `Ping time: ${Math.round(performance.now() - startTime)}ms`;
}
} catch (error) {
showError("Unable to ping the specified URL");
console.error("Error during ping:", error);
}
}

// Format URL helper
function formatUrl(input) {
try {
let cleanInput = input.toLowerCase().trim()
.replace(/^(https?:\/\/)/, '')
.split('/')[0];
return `https://${cleanInput}`;
} catch (error) {
throw new Error('Invalid URL format');
}
try {
const cleanInput = input
.toLowerCase()
.trim()
.replace(/^(https?:\/\/)/, "")
.split("/")[0];
return `https://${cleanInput}`;
} catch (error) {
throw new Error("Invalid URL format");
}
}

// Show error messages
function showError(message) {
const errorElement = document.getElementById('error-message');
if (!errorElement) return;
errorElement.textContent = message;
errorElement.style.display = 'block';
setTimeout(() => errorElement.style.display = 'none', 5000);
const errorElement = document.getElementById("error-message");
if (!errorElement) return;

errorElement.textContent = message;
errorElement.style.display = "block";
setTimeout(() => (errorElement.style.display = "none"), 5000);

Check notice

Code scanning / devskim

If untrusted data (data from HTTP requests, user submitted files, etc.) is included in an setTimeout statement it can allow an attacker to inject their own code. Note

Review setTimeout for untrusted data
}

// Run speed test
function runSpeedTest() {
showError('Speed test functionality coming soon!');
showError("Speed test functionality coming soon!");
}

// Utility functions for validation
function validateIP(ip) {
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
const ipRegex =
/^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
return ipRegex.test(ip);
}

Expand All @@ -147,29 +150,29 @@

// Validate IP address format
function validateIPAddress(ip) {
const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
if (!ipRegex.test(ip)) return false;
const parts = ip.split('.');
return parts.every(part => {
const num = parseInt(part, 10);
return num >= 0 && num <= 255;
});
const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
if (!ipRegex.test(ip)) return false;

const parts = ip.split(".");
return parts.every((part) => {
const num = parseInt(part, 10);
return num >= 0 && num <= 255;
});
}

// Theme toggle functionality
document.getElementById('theme-toggle').addEventListener('change', function () {
document.getElementById("theme-toggle").addEventListener("change", function () {
if (this.checked) {
document.body.classList.remove('light-theme');
document.body.classList.add('dark-theme');
document.body.classList.remove("light-theme");
document.body.classList.add("dark-theme");
} else {
document.body.classList.remove('dark-theme');
document.body.classList.add('light-theme');
document.body.classList.remove("dark-theme");
document.body.classList.add("light-theme");
}
});

// Initialize app when document loads
document.addEventListener('DOMContentLoaded', initializeApp);
document.addEventListener("DOMContentLoaded", initializeApp);

// Expose necessary functions to global scope
window.getMyIP = getMyIP;
Expand Down
Loading