-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.bandwidth.js
119 lines (117 loc) · 3.67 KB
/
jquery.bandwidth.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Bandwith
* @author kristofer- @ bitbucket
* @version 0.0.1
* @description Test your bandwidth by downloading an image.
* @dependencies jQuery >2.0
*
* Usage:
* set attribute data-bandwidth="yourfile.jpg"
* check bandwidth:
* $('img').bandwidth();
*
* Options:
* @param - args
* {
* callback: function,
* setImage: true/false //if true then the original src will bechanged to the tested one
* }
*/
/*globals jQuery*/
/*jslint white: true, browser: true */
(function ($) {
"use strict";
/* @class Represents a test result set.
* @param mbps - megabits per second
* @param kbps - kilobits per second
* @param bps - bits per second
* @param duration - how long did it take to download the bits
* @param bits - the number of bits to be downloaded
*/
function BandwidthResult(mbps, kbps, bps, duration, bits) {
this.mbps = mbps;
this.kbps = kbps;
this.bps = bps;
this.duration = duration;
this.bits = bits;
}
$.fn.bandwidth = function (args) {
var self = this[0],
imageAddr = $(self).data("bandwidth") + "?n=" + Math.random(),
startTime,
downloadSize,
endTime,
result = new BandwidthResult();
/* 5.
* @description Writes attribute to element
* @param result - instance of bandwidthResult
*/
function returnResult(result) {
if (args) {
// Set source
if (args.setImage) {
$(self).attr('src', imageAddr);
}
// Run callback
if (args.callback) {
args.callback(result);
}
}
// set data attribute
else {
$(self).attr("data-bandwidth", JSON.stringify(result));
return result;
}
}
/* 4.
* @description Runs when image is loaded
*/
function onImageLoad() {
endTime = (new Date()).getTime();
result.duration = (endTime - startTime) / 1000;
result.bits = downloadSize * 8;
result.bps = (result.bits / result.duration).toFixed(2);
result.kbps = (result.bps / 1024).toFixed(2);
result.mbps = (result.kbps / 1024).toFixed(2);
$(self).trigger('bandwidth',returnResult(result));
}
/* 3.
* @description Runs when gotten file-size
* @param size - filesize
*/
function getImage(size,url) {
downloadSize = size;
var download = new Image();
download.onload = onImageLoad;
startTime = (new Date()).getTime();
download.src = url;
}
/* 2.
* @description Get file size
* @param url - test-file url
* @param call - callback function tat shall recieve byte-size
*/
function getFileSize(url, call) {
var xhr = new XMLHttpRequest();
xhr.open("HEAD", url, true); // Notice "HEAD" instead of "GET",
// to get only the header
xhr.onreadystatechange = function () {
if (this.readyState === this.DONE) {
var byteSize = parseInt(xhr.getResponseHeader("Content-Length"), 10);
call(byteSize,url);
}
};
xhr.send();
}
/* 1.
* @description - initialize plugin
*/
function init(url) {
getFileSize(url, getImage);
}
/*
* @constructor
*/
init(imageAddr);
};
}(jQuery));