-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
49 lines (40 loc) · 1.35 KB
/
script.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
42
43
44
45
46
47
48
49
function reloadPopup() {
location.reload();
}
function showProgressMessage(msg) {
console.log(msg);
var progressElement = document.getElementById("progress");
if (progressElement) {
progressElement.innerHTML = Array.isArray(msg) ? msg.join("<br>") : msg;
}
}
function initiateSpeedDetection() {
showProgressMessage("Loading, please wait...");
setTimeout(measureConnectionSpeed, 1);
}
function measureConnectionSpeed() {
var startTime, endTime;
var download = new Image();
download.onload = function () {
endTime = Date.now();
showResults();
};
download.onerror = function () {
showProgressMessage("Invalid image or error downloading");
};
startTime = Date.now();
var cacheBuster = "?nnn=" + startTime;
download.src = imageAddr + cacheBuster;
function showResults() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = downloadSize * 8;
var speedMbps = ((bitsLoaded / duration) / (1024 * 1024)).toFixed(2);
showProgressMessage(speedMbps + " Mbps");
}
}
document.getElementById("refreshButton").addEventListener("click", reloadPopup);
if (window.addEventListener) {
window.addEventListener('load', initiateSpeedDetection, false);
} else if (window.attachEvent) {
window.attachEvent('onload', initiateSpeedDetection);
}