forked from YahooArchive/boomerang
/
util.js
105 lines (93 loc) · 2.32 KB
/
util.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
/**
util.js
Util plugin.
This plugin provides utility functions used by other plugins
*/
(function(w) {
var d=w.document;
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
var impl = {
complete: true, // Set when this plugin has completed
bIE: false,
};
BOOMR.plugins.UTIL = {
init: function(config) {
BOOMR.sendBeacon(); // we call sendBeacon() anyway because some other plugin
// may have blocked waiting for RT to complete
},
ObjectRecord: function(timer_name, host) {
this.timer = timer_name;
this.host = host;
},
searchElement: function(evt) {
if(impl.bIE) {
return evt.srcElement || {};
}
else {
return evt.currentTarget || evt.target || {};
}
},
addEvent: function(element, evnt_name, callback, useCapture) {
var n = 'on' + evnt_name;
if (element.addEventListener) {
element.addEventListener(evnt_name, callback, useCapture);
}
else if (element.attachEvent) {
element.attachEvent(n, callback);
}
else {
var x = element[n];
if (typeof element[n] != 'function') {
element[n] = callback;
}
else {
element[n] = function(a) { x(a); callback(a); };
}
}
},
// returns the hostname portion of the url passed in
formatUrl: function(url, t) {
try {
if (url) {
if (!/(^http|^https)/.test(url)) {
if(t == 1) {
return filterHost(location.hostname);
}
else {
return url;
}
}
var p = new RegExp('(^http|^https|):\/{2}([^\?#;]*)');
if (t == 1) {
p = new RegExp('(^http|^https|):\/{2}([^\/\?]*)');
}
var r = url.match(p);
if (r && t == 1) {
return this.filterHost(r[2]);
}
else if (r) {
return r[0];
}
}
return null;
}
catch(e) { return null; }
},
filterHost: function(h) {
if (h) {
if (h.indexOf('<')!=-1 || h.indexOf('%3C')!=-1 || h.indexOf('%3c')!=-1) {
return null;
}
if (window.decodeURIComponent) {
return decodeURIComponent(h);
}
else {
return unescape(h);
}
}
return null;
},
is_complete: function() { return impl.complete; }
};
}(window));