11// Ensure functions are globally accessible
2+
3+ // Get the user's IP address
24async function getMyIP ( ) {
35 try {
46 const response = await fetch ( "https://api.ipify.org?format=json" ) ;
57 if ( ! response . ok ) throw new Error ( "Failed to fetch IP" ) ;
68 const data = await response . json ( ) ;
79 document . getElementById ( "my-ip" ) . textContent = `Your IP: ${ data . ip } ` ;
810 } catch ( error ) {
9- showError ( "Error fetching IP." ) ;
11+ showError ( "Error fetching IP address. Please try again ." ) ;
1012 console . error ( error ) ;
1113 }
1214}
1315
16+ // Lookup IP details
1417async function lookupIP ( ) {
15- const ip = document . getElementById ( "ip-input" ) . value ;
18+ const ip = document . getElementById ( "ip-input" ) . value . trim ( ) ;
1619 if ( ! validateIP ( ip ) ) {
1720 showError ( "Please enter a valid IP address." ) ;
1821 return ;
@@ -25,34 +28,37 @@ async function lookupIP() {
2528 document . getElementById ( "ip-info" ) . textContent =
2629 `Location: ${ data . city } , ${ data . region } , ${ data . country } (ISP: ${ data . isp } )` ;
2730 } else {
28- showError ( "Invalid IP address." ) ;
31+ showError ( "Invalid IP address or data not found ." ) ;
2932 }
3033 } catch ( error ) {
31- showError ( "Error fetching IP data." ) ;
34+ showError ( "Error fetching IP data. Please try again. " ) ;
3235 console . error ( error ) ;
3336 }
3437}
3538
39+ // Run an internet speed test
3640function runSpeedTest ( ) {
3741 const startTime = Date . now ( ) ;
38- const imageUrl = "https://via.placeholder.com/1000000" ; // Example file URL
42+ const imageUrl = "https://via.placeholder.com/1000000" ; // Placeholder image for speed test
3943 fetch ( imageUrl )
4044 . then ( ( response ) => response . blob ( ) )
4145 . then ( ( ) => {
4246 const endTime = Date . now ( ) ;
43- const timeTaken = ( endTime - startTime ) / 1000 ; // seconds
44- const speed = ( 1000000 / timeTaken / 1024 ) . toFixed ( 2 ) ; // speed in KB/s
47+ const timeTaken = ( endTime - startTime ) / 1000 ; // Convert to seconds
48+ const fileSize = 1000000 ; // File size in bytes (1 MB)
49+ const speedMbps = ( ( fileSize * 8 ) / ( timeTaken * 1024 * 1024 ) ) . toFixed ( 2 ) ; // Convert to Mbps
4550 document . getElementById ( "speed-results" ) . textContent =
46- `Download speed: ${ speed } KB/s ` ;
51+ `Download speed: ${ speedMbps } Mbps ` ;
4752 } )
4853 . catch ( ( error ) => {
49- showError ( "Speed test failed." ) ;
54+ showError ( "Speed test failed. Please check your connection. " ) ;
5055 console . error ( error ) ;
5156 } ) ;
5257}
5358
59+ // Ping a URL
5460async function ping ( ) {
55- const url = document . getElementById ( "ping-url" ) . value ;
61+ const url = document . getElementById ( "ping-url" ) . value . trim ( ) ;
5662 if ( ! validateURL ( url ) ) {
5763 showError ( "Please enter a valid URL." ) ;
5864 return ;
@@ -65,11 +71,12 @@ async function ping() {
6571 document . getElementById ( "ping-results" ) . textContent =
6672 `Ping: ${ end - start } ms` ;
6773 } catch ( error ) {
68- showError ( "Ping failed." ) ;
74+ showError ( "Ping failed. Please check the URL or your connection. " ) ;
6975 console . error ( error ) ;
7076 }
7177}
7278
79+ // Show error message
7380function showError ( message ) {
7481 const errorMessage = document . getElementById ( "error-message" ) ;
7582 errorMessage . textContent = message ;
@@ -79,12 +86,14 @@ function showError(message) {
7986 } , 5000 ) ;
8087}
8188
89+ // Validate IP address
8290function validateIP ( ip ) {
8391 const regex =
8492 / ^ ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) $ / ;
8593 return regex . test ( ip ) ;
8694}
8795
96+ // Validate URL
8897function validateURL ( url ) {
8998 const regex = / ^ ( h t t p s ? : \/ \/ ) ? [ \w . - ] + ( \. [ \w . - ] + ) + [ / # ? ] ? .* $ / ;
9099 return regex . test ( url ) ;
0 commit comments