forked from YahooArchive/boomerang
/
images.js
117 lines (98 loc) · 3.2 KB
/
images.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
/**
images.js
Images plugin.
This plugin will add RT tracking to all of the images contained on the page
Requires UTIL plugin
*/
(function(w) {
var d=w.document;
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
var impl = {
complete: false, // Set when this plugin has completed
last_image: 0,
images_poll_id: null,
image_store: new Object,
getVar: function(v,d){
return v ? v:d
},
imageLoaded: function(e) {
var i = impl.getObject(e);
if(i) {
BOOMR.plugins.RT.endTimer(i.timer)
}
},
imageError: function(e) {
var i = impl.getObject(e);
if(i) {
BOOMR.plugins.RT.endTimer(i.timer)
}
// set error flag
},
imageAbort: function(e) {
var i = impl.getObject(e);
if(i) {
BOOMR.plugins.RT.endTimer(i.timer)
}
// set abort flag
},
getObject: function(e) {
var evt = window.event ? window.event : e;
var a = BOOMR.plugins.UTIL.searchElement(evt);
var i;
i = impl.image_store[a.src || a.href];
return i;
},
pollImages: function() {
var image_cnt = document.images.length;
if (image_cnt > impl.last_image) {
for(var i = impl.last_image; i < image_cnt; ++i) {
var image_path = document.images[i].src;
if (image_path) {
var timer_name = "t_img_" + i;
BOOMR.plugins.RT.startTimer(timer_name);
impl.image_store[image_path] = new BOOMR.plugins.UTIL.ObjectRecord(timer_name, BOOMR.plugins.UTIL.formatUrl(image_path, 1));
BOOMR.plugins.UTIL.addEvent(document.images[i], 'load', impl.imageLoaded, false);
BOOMR.plugins.UTIL.addEvent(document.images[i], 'error', impl.imageError, false);
BOOMR.plugins.UTIL.addEvent(document.images[i], 'abort', impl.imageAbort, false);
}
}
}
impl.last_image = image_cnt;
},
};
BOOMR.plugins.IMGS = {
init: function(config) {
impl.pollImages();
impl.images_poll_id = setInterval(impl.pollImages, 10);
BOOMR.subscribe("page_ready", this.done, null, this);
return this;
},
// Called when the page has reached a "usable" state. This may be when the
// onload event fires, or it could be at some other moment during/after page
// load when the page is usable by the user
done: function() {
if(impl.images_poll_id) clearInterval(impl.images_poll_id);
impl.complete = true;
// only add beacon vars if this plugin is still enabled
if (!BOOMR.plugins.CUST ||
BOOMR.plugins.CUST && BOOMR.plugins.CUST.isEnabled('IMGS')) {
var count=0;
for(var prop in impl.image_store) {
if (impl.image_store.hasOwnProperty(prop)) {
var timers = BOOMR.plugins.RT.getTimers();
var start = timers[impl.image_store[prop].timer].start;
var end = timers[impl.image_store[prop].timer].end;
BOOMR.addVar("imgs_obj_" + count++, impl.image_store[prop].host + '|' + start + '|' + end);
}
}
}
BOOMR.sendBeacon(); // we call sendBeacon() anyway because some other plugin
// may have blocked waiting for RT to complete
},
getImageStore: function() {
return impl.image_store;
},
is_complete: function() { return impl.complete; }
};
}(window));