Skip to content

Commit

Permalink
Update main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaTheLEGEND authored Sep 23, 2023
1 parent 3f0bab0 commit 031ba39
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,48 @@ var protocol = window.location.protocol;
if(askBeforeUnload)return'Someone may be attempting to close you window. Please confirm or deny this action.';
}

//********************************************************************************************
// end go-gardian anti-close
//
// start offline detection
//********************************************************************************************

let isOnline = true; // Assume online initially

const checkInternetConnection = async () => {
try {
await fetch('https://www.google.com', { method: 'HEAD' });
if (!isOnline) {
console.log('Online now');
// Call the function when it goes from offline to online
onlineCallback();
}
isOnline = true;
} catch (error) {
if (isOnline) {
console.error('Offline now');
// Call the function when it goes from online to offline
offlineCallback();
}
isOnline = false;
}
};

const onlineCallback = () => {
// Function to call when going online
console.log('Online callback function');
// Add your code here
};

const offlineCallback = () => {
// Function to call when going offline
console.log('Offline callback function');
// Add your code here
};

// Initial check
checkInternetConnection();

// Check every 5 seconds
setInterval(checkInternetConnection, 5000);

0 comments on commit 031ba39

Please sign in to comment.