-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 3.11 KB
/
Copy pathindex.js
File metadata and controls
69 lines (59 loc) · 3.11 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// See LICENSE for usage information
// The following lines allow the ping function to be loaded via commonjs, AMD,
// and script tags, directly into window globals.
// Thanks to https://github.com/umdjs/umd/blob/master/templates/returnExports.js
(function (root, factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof module === 'object' && module.exports) { module.exports = factory(); } else { root.ping = factory(); }
}(this, function () {
/**
* Creates and loads an image element by url.
* @param {String} url
* @return {Promise} promise that resolves to an image element or
* fails to an Error.
*/
function request_image(url) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.onload = function() { resolve(img); };
img.onerror = function() { reject(url); };
img.src = "http://" + url + '/favicon.ico?=' + Math.floor((1 + Math.random()) * 0x10000).toString(16);
});
}
/**
* Pings a url.
* @param {String} url
* @param {Number} multiplier - optional, factor to adjust the ping by. 0.3 works well for HTTP servers.
* @return {Promise} promise that resolves to a ping (ms, float).
*/
function ping(url, multiplier) {
return new Promise(function(resolve, reject) {
var start = (new Date()).getTime();
var response = function() {
var delta = ((new Date()).getTime() - start);
delta *= (multiplier || 1);
resolve(delta);
};
request_image(url).then(response).catch(response);
// Set a timeout for max-pings, 5s.
//setTimeout(function() { reject(Error('Timeout')); }, 5000);
});
}
return ping;
}));
const TRIAL_COUNT = 200 // We love double jeopardy here!
//"google.com", "youtube.com", "facebook.com",
sites = ["chinadaily.com.cn", "sueddeutsche.de", "deutschland.de", "google.com", "youtube.com", "facebook.com", "wikipedia.org", "yahoo.com", "amazon.com", "live.com", "reddit.com", "netflix.com", "blogspot.com", "bing.com", "instagram.com", "office.com", "linkedin.com", "microsoft.com", "twitter.com", "ebay.com", "twitch.tv", "stackoverflow.com", "msn.com", "imdb.com", "whatsapp.com", "apple.com", "github.com", "fandom.com", "paypal.com", "t.co", "pinterest.com", "espn.com", "nytimes.com", "walmart.com", "quora.com", "accuweather.com", "bitly.com", "nasa.gov", "ampproject.org", "wpi.edu"]
async function test(){
const row = document.getElementById("data_row")
// Initialize headers
for (let s = 0; s < sites.length; s ++) {
row.innerHTML += '<td id="cell' + s + '">' + sites[s] + '</td>'
}
for (let i = 0; i < TRIAL_COUNT; i ++) {
for (let s = 0; s < sites.length; s ++) {
const cell = document.getElementById("cell" + s)
// Wait for the result of the ping and add it to the table
const res = await ping("www." + sites[s], 1)
cell.innerHTML += "<br>" + res
}
}
}