-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_internet.js
41 lines (33 loc) · 1.35 KB
/
check_internet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const util = require('util');
const dns = require('dns');
const puppeteer = require('puppeteer');
async function isConnected() {
try {
const lookupService = util.promisify(dns.lookupService);
const result = await lookupService('8.8.8.8', 53);
return true;
} catch (err) {
return false;
}
}
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('online', () => console.info('Online!'));
page.on('offline', () => console.info('Offline!'));
// Adds window.connectionChange in page.
await page.exposeFunction('connectionChange', async online => {
// Since online/offline events aren't 100% reliable, do an
// actual dns lookup to confirm connectivity.
const isReallyConnected = await isConnected();
page.emit(isReallyConnected ? 'online' : 'offline');
});
// Monitor browser online/offline events in the page.
await page.evaluateOnNewDocument(() => {
window.addEventListener('online', e => window.connectionChange(navigator.onLine));
window.addEventListener('offline', e => window.connectionChange(navigator.onLine));
});
// Kick off a navigation so evaluateOnNewDocument runs.
await page.goto('data:text/html,hi');
// await browser.close(); // Don't close the browser so we can monitor!
//run the code and try turning internet connection on/off to validate the logic
});