-
Notifications
You must be signed in to change notification settings - Fork 27
/
stats_sendto.js
87 lines (75 loc) · 2.54 KB
/
stats_sendto.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
/**
* Coverage for off-package LMD modules
*
* Flag "stats_sendto"
*
* This plugin provides sendTo private function and require.stats.sendTo() public function
*
* This plugin depends on stats
*/
/**
* @name sandbox
*/
(function (sb) {
/**
* XDomain post
*
* @param {String} host
* @param {String} method
* @param {Object} data
* @param {String} [reportName]
*
*/
var sendTo = function () {
var runId = function () {
var userAgent = navigator.userAgent,
rchrome = /(chrome)[ \/]([\w.]+)/i,
rwebkit = /(webkit)[ \/]([\w.]+)/i,
ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/i,
rmsie = /(msie) ([\w.]+)/i,
rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/i;
userAgent = (userAgent.match(rchrome) ||
userAgent.match(rwebkit) ||
userAgent.match(ropera) ||
userAgent.match(rmsie) ||
userAgent.match(rmozilla)
);
return (userAgent ? userAgent.slice(1).join('-') : 'undefined-0') + '-' +
(new Date+'').split(' ').slice(1, 5).join('_') + '-' +
Math.random();
}();
/**
* @return {HTMLIFrameElement}
*/
return function (host, method, data, reportName) {
var JSON = sb.trigger('*:request-json', sb.global.JSON)[0];
// Add the iframe with a unique name
var iframe = sb.document.createElement("iframe"),
uniqueString = sb.global.Math.random();
sb.document.body.appendChild(iframe);
iframe.style.visibility = "hidden";
iframe.style.position = "absolute";
iframe.style.left = "-1000px";
iframe.style.top = "-1000px";
iframe.contentWindow.name = uniqueString;
// construct a form with hidden inputs, targeting the iframe
var form = sb.document.createElement("form");
form.target = uniqueString;
form.action = host + "/" + method + '/' + (reportName || runId).replace(/\/|\\|\./g, '_');
form.method = "POST";
form.setAttribute('accept-charset', 'utf-8');
// repeat for each parameter
var input = sb.document.createElement("input");
input.type = "hidden";
input.name = "json";
input.value = JSON.stringify(data);
form.appendChild(input);
document.body.appendChild(form);
form.submit();
return iframe;
}
}();
sb.require.stats.sendTo = function (host) {
return sendTo(host, "stats", sb.require.stats());
};
}(sandbox));