22
33// Cache DOM elements
44const elements = {
5- toolsContainer : null ,
6- ipInput : null ,
7- pingUrl : null ,
8- myIp : null ,
9- ipInfo : null ,
10- pingResults : null ,
11- errorMessage : null
5+ toolsContainer : null ,
6+ ipInput : null ,
7+ pingUrl : null ,
8+ myIp : null ,
9+ ipInfo : null ,
10+ pingResults : null ,
11+ errorMessage : null ,
1212} ;
1313
1414// Initialize DOM elements and event listeners
1515function initializeApp ( ) {
16- // Cache DOM elements
17- elements . toolsContainer = document . getElementById ( ' tools-container' ) ;
18- elements . ipInput = document . getElementById ( ' ip-input' ) ;
19- elements . pingUrl = document . getElementById ( ' ping-url' ) ;
20- elements . myIp = document . getElementById ( ' my-ip' ) ;
21- elements . ipInfo = document . getElementById ( ' ip-info' ) ;
22- elements . pingResults = document . getElementById ( ' ping-results' ) ;
23- elements . errorMessage = document . getElementById ( ' error-message' ) ;
24-
25- // Add Enter key support
26- if ( elements . ipInput ) {
27- elements . ipInput . addEventListener ( ' keypress' , ( e ) => {
28- if ( e . key === ' Enter' ) lookupIP ( ) ;
29- } ) ;
30- }
16+ // Cache DOM elements
17+ elements . toolsContainer = document . getElementById ( " tools-container" ) ;
18+ elements . ipInput = document . getElementById ( " ip-input" ) ;
19+ elements . pingUrl = document . getElementById ( " ping-url" ) ;
20+ elements . myIp = document . getElementById ( " my-ip" ) ;
21+ elements . ipInfo = document . getElementById ( " ip-info" ) ;
22+ elements . pingResults = document . getElementById ( " ping-results" ) ;
23+ elements . errorMessage = document . getElementById ( " error-message" ) ;
24+
25+ // Add Enter key support
26+ if ( elements . ipInput ) {
27+ elements . ipInput . addEventListener ( " keypress" , ( e ) => {
28+ if ( e . key === " Enter" ) lookupIP ( ) ;
29+ } ) ;
30+ }
3131
32- if ( elements . pingUrl ) {
33- elements . pingUrl . addEventListener ( ' keypress' , ( e ) => {
34- if ( e . key === ' Enter' ) ping ( ) ;
35- } ) ;
36- }
32+ if ( elements . pingUrl ) {
33+ elements . pingUrl . addEventListener ( " keypress" , ( e ) => {
34+ if ( e . key === " Enter" ) ping ( ) ;
35+ } ) ;
36+ }
3737}
3838
3939// Show/hide the tools container
4040function openNetworkTools ( ) {
41- const container = document . getElementById ( 'tools-container' ) ;
42- if ( container ) {
43- container . style . display = container . style . display === 'none' ? 'block' : 'none' ;
44- }
41+ const container = document . getElementById ( "tools-container" ) ;
42+ if ( container ) {
43+ container . style . display =
44+ container . style . display === "none" ? "block" : "none" ;
45+ }
4546}
4647
4748// Get user's IP address
4849async function getMyIP ( ) {
49- try {
50- const response = await fetch ( 'https://api.ipify.org?format=json' ) ;
51- if ( ! response . ok ) throw new Error ( 'Network response was not ok' ) ;
52- const data = await response . json ( ) ;
53- const myIpElement = document . getElementById ( 'my-ip' ) ;
54- if ( myIpElement ) {
55- myIpElement . textContent = `Your IP: ${ data . ip } ` ;
56- }
57- } catch ( error ) {
58- showError ( 'Unable to fetch IP address' ) ;
59- console . error ( 'Error fetching IP:' , error ) ;
50+ try {
51+ const response = await fetch ( "https://api.ipify.org?format=json" ) ;
52+ if ( ! response . ok ) throw new Error ( "Network response was not ok" ) ;
53+ const data = await response . json ( ) ;
54+ const myIpElement = document . getElementById ( "my-ip" ) ;
55+ if ( myIpElement ) {
56+ myIpElement . textContent = `Your IP: ${ data . ip } ` ;
6057 }
58+ } catch ( error ) {
59+ showError ( "Unable to fetch IP address" ) ;
60+ console . error ( "Error fetching IP:" , error ) ;
61+ }
6162}
6263
6364// Lookup IP or domain
6465async function lookupIP ( ) {
65- try {
66- const input = elements . ipInput ?. value ?. trim ( ) ;
67- if ( ! input ) throw new Error ( 'Please enter an IP or domain' ) ;
68-
69- const response = await fetch ( `https://dns.google/resolve?name=${ input } ` ) ;
70- if ( ! response . ok ) throw new Error ( 'Lookup failed' ) ;
71- const data = await response . json ( ) ;
72-
73- if ( elements . ipInfo ) {
74- elements . ipInfo . textContent = data . Answer
75- ? `Resolved: ${ data . Answer [ 0 ] . data } `
76- : 'Lookup failed' ;
77- }
78- } catch ( error ) {
79- showError ( error . message || 'Unable to lookup address' ) ;
80- console . error ( 'Error during lookup:' , error ) ;
66+ try {
67+ const input = elements . ipInput ?. value ?. trim ( ) ;
68+ if ( ! input ) throw new Error ( "Please enter an IP or domain" ) ;
69+
70+ const response = await fetch ( `https://dns.google/resolve?name=${ input } ` ) ;
71+ if ( ! response . ok ) throw new Error ( "Lookup failed" ) ;
72+ const data = await response . json ( ) ;
73+
74+ if ( elements . ipInfo ) {
75+ elements . ipInfo . textContent = data . Answer
76+ ? `Resolved: ${ data . Answer [ 0 ] . data } `
77+ : "Lookup failed" ;
8178 }
79+ } catch ( error ) {
80+ showError ( error . message || "Unable to lookup address" ) ;
81+ console . error ( "Error during lookup:" , error ) ;
82+ }
8283}
8384
8485// Ping function
8586async function ping ( ) {
86- try {
87- const input = elements . pingUrl ?. value ?. trim ( ) ;
88- if ( ! input ) throw new Error ( 'Please enter a URL' ) ;
89-
90- const startTime = performance . now ( ) ;
91- await fetch ( `https://${ input . replace ( / ^ h t t p s ? : \/ \/ / , '' ) } ` , {
92- mode : 'no-cors'
93- } ) ;
94-
95- if ( elements . pingResults ) {
96- elements . pingResults . textContent =
97- `Ping time: ${ Math . round ( performance . now ( ) - startTime ) } ms` ;
98- }
99- } catch ( error ) {
100- showError ( 'Unable to ping the specified URL' ) ;
101- console . error ( 'Error during ping:' , error ) ;
87+ try {
88+ const input = elements . pingUrl ?. value ?. trim ( ) ;
89+ if ( ! input ) throw new Error ( "Please enter a URL" ) ;
90+
91+ const startTime = performance . now ( ) ;
92+ await fetch ( `https://${ input . replace ( / ^ h t t p s ? : \/ \/ / , "" ) } ` , {
93+ mode : "no-cors" ,
94+ } ) ;
95+
96+ if ( elements . pingResults ) {
97+ elements . pingResults . textContent = `Ping time: ${ Math . round ( performance . now ( ) - startTime ) } ms` ;
10298 }
99+ } catch ( error ) {
100+ showError ( "Unable to ping the specified URL" ) ;
101+ console . error ( "Error during ping:" , error ) ;
102+ }
103103}
104104
105105// Format URL helper
106106function formatUrl ( input ) {
107- try {
108- let cleanInput = input . toLowerCase ( ) . trim ( )
109- . replace ( / ^ ( h t t p s ? : \/ \/ ) / , '' )
110- . split ( '/' ) [ 0 ] ;
111- return `https://${ cleanInput } ` ;
112- } catch ( error ) {
113- throw new Error ( 'Invalid URL format' ) ;
114- }
107+ try {
108+ const cleanInput = input
109+ . toLowerCase ( )
110+ . trim ( )
111+ . replace ( / ^ ( h t t p s ? : \/ \/ ) / , "" )
112+ . split ( "/" ) [ 0 ] ;
113+ return `https://${ cleanInput } ` ;
114+ } catch ( error ) {
115+ throw new Error ( "Invalid URL format" ) ;
116+ }
115117}
116118
117119// Show error messages
118120function showError ( message ) {
119- const errorElement = document . getElementById ( ' error-message' ) ;
120- if ( ! errorElement ) return ;
121-
122- errorElement . textContent = message ;
123- errorElement . style . display = ' block' ;
124- setTimeout ( ( ) => errorElement . style . display = ' none' , 5000 ) ;
121+ const errorElement = document . getElementById ( " error-message" ) ;
122+ if ( ! errorElement ) return ;
123+
124+ errorElement . textContent = message ;
125+ errorElement . style . display = " block" ;
126+ setTimeout ( ( ) => ( errorElement . style . display = " none" ) , 5000 ) ;
125127}
126128
127129// Run speed test
128130function runSpeedTest ( ) {
129- showError ( ' Speed test functionality coming soon!' ) ;
131+ showError ( " Speed test functionality coming soon!" ) ;
130132}
131133
132134// Utility functions for validation
133135function validateIP ( ip ) {
134- const ipRegex = / ^ ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 - 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( \d { 1 , 3 } ) \. ( \d { 1 , 3 } ) \. ( \d { 1 , 3 } ) $ / ;
136+ const ipRegex =
137+ / ^ ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 - 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( \d { 1 , 3 } ) \. ( \d { 1 , 3 } ) \. ( \d { 1 , 3 } ) $ / ;
135138 return ipRegex . test ( ip ) ;
136139}
137140
@@ -147,29 +150,29 @@ function validateURL(url) {
147150
148151// Validate IP address format
149152function validateIPAddress ( ip ) {
150- const ipRegex = / ^ ( \d { 1 , 3 } \. ) { 3 } \d { 1 , 3 } $ / ;
151- if ( ! ipRegex . test ( ip ) ) return false ;
152-
153- const parts = ip . split ( '.' ) ;
154- return parts . every ( part => {
155- const num = parseInt ( part , 10 ) ;
156- return num >= 0 && num <= 255 ;
157- } ) ;
153+ const ipRegex = / ^ ( \d { 1 , 3 } \. ) { 3 } \d { 1 , 3 } $ / ;
154+ if ( ! ipRegex . test ( ip ) ) return false ;
155+
156+ const parts = ip . split ( "." ) ;
157+ return parts . every ( ( part ) => {
158+ const num = parseInt ( part , 10 ) ;
159+ return num >= 0 && num <= 255 ;
160+ } ) ;
158161}
159162
160163// Theme toggle functionality
161- document . getElementById ( ' theme-toggle' ) . addEventListener ( ' change' , function ( ) {
164+ document . getElementById ( " theme-toggle" ) . addEventListener ( " change" , function ( ) {
162165 if ( this . checked ) {
163- document . body . classList . remove ( ' light-theme' ) ;
164- document . body . classList . add ( ' dark-theme' ) ;
166+ document . body . classList . remove ( " light-theme" ) ;
167+ document . body . classList . add ( " dark-theme" ) ;
165168 } else {
166- document . body . classList . remove ( ' dark-theme' ) ;
167- document . body . classList . add ( ' light-theme' ) ;
169+ document . body . classList . remove ( " dark-theme" ) ;
170+ document . body . classList . add ( " light-theme" ) ;
168171 }
169172} ) ;
170173
171174// Initialize app when document loads
172- document . addEventListener ( ' DOMContentLoaded' , initializeApp ) ;
175+ document . addEventListener ( " DOMContentLoaded" , initializeApp ) ;
173176
174177// Expose necessary functions to global scope
175178window . getMyIP = getMyIP ;
0 commit comments