-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
57 lines (47 loc) · 1.46 KB
/
background.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
50
51
52
53
54
55
56
57
browser.browserAction.onClicked.addListener(handleClick);
function handleClick(){
MeasureConnectionSpeed();
}
var imageAddr = "http://www.kenrockwell.com/contax/images/g2/examples/31120037-5mb.jpg";
var downloadSize = 4995374; //bytes
var startTime, endTime;
function ShowProgressMessage(msg){
if (typeof msg == "string"){
console.log(msg);
} else {
for ( var i = 0 ; i < msg.length ; ++i ){
console.log(msg[i]);
}
}
}
if (window.addEventListener) {
window.addEventListener('load', MeasureConnectionSpeed, false);
} else if (window.attachEvent) {
window.attachEvent('onload', MeasureConnectionSpeed);
}
function MeasureConnectionSpeed() {
var download = new Image();
download.onload = function () {
endTime = (new Date()).getTime();
showResults();
}
download.onerror = function (err, msg) {
ShowProgressMessage("Error During Speed Test");
}
startTime = (new Date()).getTime();
var cacheBuster = "?nnn=" + startTime;
download.src = imageAddr + cacheBuster;
}
function showResults() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = downloadSize * 8;
var speedBps = (bitsLoaded / duration).toFixed(2);
var speedKbps = (speedBps / 1024).toFixed(2);
var speedMbps = (speedKbps / 1024).toFixed(2);
ShowProgressMessage([
"Your connection speed is:",
speedBps + " bps",
speedKbps + " kbps",
speedMbps + " Mbps"
]);
}