-
Notifications
You must be signed in to change notification settings - Fork 10
/
embed-code.js
205 lines (185 loc) · 7.27 KB
/
embed-code.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# The embed code is compressed and minified using the closure compiler.
# Please run ./compile to build the embed code.
#
# Lines starting with #<space> will be removed during compilation.
# Lines starting with #<A> will be included only for target A.
# A line can be included in multiple targets if the target names
# are separated with '|', e.g. #spacebook|myface alert('hello');
/**
* If the code is included twice, the second instance will not adversely
* affect the first.
* @param {Object} params - see bottom of this file for the definition
* of params. It contains the partner settings for the Meebo Bar.
*/
window.Meebo || (function(params) {
var win = window;
/**
* Return early for extensions if either:
* we are on http(s)?://www.meebo.com/
* we are on a *.dev.meebo.com page
* the page is iframed (top window is not this window)
* the page is a popout (win.opener exists)
*/
#everywhere|dogfood if (win.location.toString().match(/^https?:\/\/www\.meebo\.com\/?(messenger)?\/?$/)
#everywhere|dogfood || win.location.hostname.toString().match(/\.dev\.meebo\.com$/)
#everywhere|dogfood || win.location.hostname == '127.0.0.1' || win.location.hostname == 'localhost'
#everywhere|dogfood || win.location.protocol != 'http:'
#everywhere|dogfood || win.top != win) { return; }
/**
* This is the exported Meebo API function 'Meebo' that lives in the
* global scope. Partners can execute API functions by calling
* Meebo("<name of API method>", arg1, arg2, ...);
* Some partners also set custom parameters on the Meebo object for
* controlling certain aspects of the bar.
*
* The Meebo object also acts as an onload handler for Meebo. To
* be notified when the Meebo Bar loads, a partner can call:
* Meebo(function() {
* ... this function runs once the Meebo Bar loads ...
* });
*/
var Meebo = win.Meebo = win.Meebo || function() { (Meebo._ = Meebo._ || []).push(arguments); },
doc = document,
body = 'body',
bodyEl = doc[body],
caller;
/**
* If the embed code is placed (incorrectly) in the HEAD of a document,
* we will try initializing the bar again once the BODY element exists.
*/
if(!bodyEl) {
caller = arguments.callee;
return setTimeout(function() { caller(params); }, 100);
}
/**
* These properties are used for logging certain events that happen
* while the Meebo Bar loads. We use these to monitor, tune, and
* optimize the performance of the Meebo Bar to ensure it doesn't
* block partner sites and minimize the impact of loading the bar.
*/
Meebo.$ = {0: +new Date};
Meebo.T = function(key) {
Meebo.$[key] = new Date - Meebo.$[0];
};
/**
* This is the internal version number of the Meebo Bar embed code.
*/
Meebo.v = 4;
/**
* Storing string values for certain properties improves the minification of
* the code when using the closure compiler.
*/
var load = 'load',
appendChild = 'appendChild',
createElement = 'createElement',
src = 'src',
lang = 'lang',
network = 'network',
domain = 'domain',
/**
* Create placeholder DOM where the Meebo Bar will go.
*/
meeboDiv = doc[createElement]('div'),
el = meeboDiv[appendChild](doc[createElement]('m')),
iframe = doc[createElement]('iframe'),
/**
* Here we listen for the load event of the page to monitor page
* load times. This must be done in the embed code since the bar
* itself defers to the page for loading priority. In some cases,
* this means the bar will load after the page loads, in which case
* we wouldn't be able to measure this value.
*/
addEventListener = 'addEventListener',
attachEvent = 'attachEvent',
documentS = 'document',
contentWindow = 'contentWindow',
domainSrc,
onload = function() {
Meebo.T(load);
Meebo(load);
};
if (win[addEventListener]) {
win[addEventListener](load, onload, false);
} else {
win[attachEvent]('on' + load, onload);
}
/**
* Prepare the placeholder DOM (make it invisible) and insert it
* into the DOM.
*/
meeboDiv.style.display = 'none';
/**
* For the extensions, we'd like to append the #meebo div to the bottom of
* the page so that we're drawn on top of elements with the same z-index.
* Unfortunately, there is an IE issue when appending elements with
* innerHTML'ed contents while parsing happening. This special case should
* be safe for extensions, in which the DOM has finished loading at the time
* of embed code execution.
*/
#everywhere|dogfood bodyEl[appendChild](meeboDiv).id = 'meebo';
#dashboard|meebo-site|spacebook|myface bodyEl.insertBefore(meeboDiv, bodyEl.firstChild).id = 'meebo';
iframe.frameBorder = "0";
iframe.id = "meebo-iframe";
iframe.allowTransparency = "true";
el[appendChild](iframe);
/**
* Try to start writing into the blank iframe. In IE, this will fail if document.domain has been set,
* so fail back to using a javascript src for the frame. In IE > 6, these urls will normally prevent
* the window from triggering onload, so we only use the javascript url to open the document and set
* its document.domain
*/
try {
iframe[contentWindow][documentS].open();
} catch(e) {
params[domain] = doc[domain];
domainSrc = "javascript:var d=" + documentS + ".open();d.domain='" + doc.domain + "';";
iframe[src] = domainSrc + "void(0);";
}
/**
* html() builds the string for the HTML of the iframe.
*/
function html() {
return [
'<', body, ' onload="var d=', documentS, ";d.getElementsByTagName('head')[0].",
appendChild, '(d.', createElement, "('script')).", src, "='//",
#spacebook|myface /dev\.meebo\.com/.test(document.location.host)?document.location.host : params.host || 'cim.meebo.com',
#meebo-site '{{ bar_hostname }}',
#dashboard 'cim.meebo.com',
#everywhere|dogfood window.MeeboExtensionOptions && MeeboExtensionOptions.domain ||
#everywhere 'www.meebo.com',
#dogfood 'alpha.meebo.com',
'/cim?iv=', Meebo.v, '&',
network, '=', params[network],
#meebo-site '&v={{ bar_version }}',
#everywhere|dogfood '&extension=true',
#spacebook|myface params['barType'] == 'extension' || params['barType'] == 'mini' ? '&'+params['barType']+'=true' : '',
params[lang] ? '&' + lang + '=' + params[lang] : '',
params[domain] ? '&' + domain + '=' + params[domain] : '',
'\'"></', body, '>'
].join('');
};
/**
* Set the HTML of the iframe. In IE 6, the document.domain from the iframe src hasn't had time to
* "settle", so trying to access the contentDocument will throw an error. Luckily, in IE 7 we can
* finish writing the html with the iframe src without preventing the page from onloading
*/
try {
var d = iframe[contentWindow][documentS];
d.write(html());
d.close();
} catch(e) {
iframe[src] = domainSrc + 'd.write("' + html().replace(/"/g, '\\"') + '");d.close();';
}
/**
* All done! Record the time it took to run this code (should be < 10ms).
*/
Meebo.T(1);
})({
#spacebook|myface|meebo-site host: (function(){var match=document.cookie.match(/build=([\w\.-]+)/); return match ? match[1] : null})(),
#spacebook|myface barType: (function(){var match=document.cookie.match(/barType=([\w]+)/); return match ? match[1] : null})(),
#spacebook network: 'spacebook'
#myface network: 'myface'
#meebo-site network: 'meebo-site'
#everywhere|dogfood network: 'everywhere'
#dashboard network: '%(partner)s'
});