forked from shane-tomlinson/testmob
/
winchan.js
269 lines (244 loc) · 8.38 KB
/
winchan.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
;WinChan = (function() {
var IFRAME_NAME = "_moz_vep_comm_iframe";
// a portable addListener implementation
function addListener(w, event, cb) {
if(w.attachEvent) w.attachEvent('on' + event, cb);
else if (w.addEventListener) w.addEventListener(event, cb, false);
}
// a portable removeListener implementation
function removeListener(w, event, cb) {
if(w.detachEvent) w.detachEvent('on' + event, cb);
else if (w.removeEventListener) w.removeEventListener(event, cb, false);
}
// checking for IE8 or above
function isInternetExplorer() {
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat(RegExp.$1);
}
return rv >= 8;
}
// checking Mobile Firefox (Fennec)
function isFennec() {
try {
return (navigator.userAgent.indexOf('Fennec/') != -1);
} catch(e) {};
return false;
}
// feature checking to see if this platform is supported at all
function isSupported() {
return (window.JSON && window.JSON.stringify &&
window.JSON.parse && window.postMessage);
}
// given a URL, extract the origin
function extractOrigin(url) {
var m = /^(https?:\/\/[-_a-zA-Z\.0-9:]+)/.exec(url);
if (m) return m[1];
return url;
}
if (isInternetExplorer()) {
// find the relay iframe in the opener
function findRelay() {
var loc = window.location;
var frames = window.opener.frames;
var origin = loc.protocol + '//' + loc.host;
for (i = frames.length - 1; i >= 0; i++) {
try {
if (frames[i].location.href.indexOf(origin) === 0 &&
frames[i].name === IFRAME_NAME)
{
return frames[i];
}
} catch(e) { }
}
return;
}
/* This is how we roll on IE:
* 0. user clicks
* 1. caller adds relay iframe (served from trusted domain) to DOM
* 2. caller opens window (with content from trusted domain)
* 3. window on opening adds a listener to 'message'
* 4. window on opening finds iframe
* 5. window checks if iframe is "loaded" - has a 'doPost' function yet
* 5a. if iframe.doPost exists, window uses it to send ready event to caller
* 5b. if iframe.doPost doesn't exist, window waits for frame ready
* 5bi. once ready, window calls iframe.doPost to send ready event
* 6. caller upon reciept of 'ready', sends args
*/
return {
open: function(url, relay_url, winopts, arg, cb) {
if (!cb) throw "missing required callback argument";
// sanity check, are url and relay_url the same origin?
var origin = extractOrigin(url);
if (origin !== extractOrigin(relay_url)) {
setTimeout(function() {
cb('invalid arguments: origin of url and relay_url must match');
})
return;
}
// first we need to add a "relay" iframe to the document that's served
// from the target domain. We can postmessage into a iframe, but not a
// window
var iframe = document.createElement("iframe");
// iframe.setAttribute('name', framename);
iframe.setAttribute('src', relay_url);
iframe.style.display = "none";
iframe.setAttribute('name', IFRAME_NAME);
document.body.appendChild(iframe);
var w = window.open(url, null, winopts);
var req = JSON.stringify({a: 'request', d: arg});
// cleanup on unload
function cleanup() {
document.body.removeChild(iframe);
if (w) w.close();
w = undefined;
}
addListener(window, 'unload', cleanup);
function onMessage(e) {
try {
var d = JSON.parse(e.data);
if (d.a === 'ready') iframe.contentWindow.postMessage(req, origin);
else if (d.a === 'error') cb(d.d);
else if (d.a === 'response') {
removeListener(window, 'message', onMessage);
removeListener(window, 'unload', cleanup);
cleanup();
cb(null, d.d);
}
} catch(e) { }
};
addListener(window, 'message', onMessage);
return {
close: function() {
if (w) w.close();
w = undefined;
},
focus: function() {
if (w) w.focus();
}
};
},
onOpen: function(cb) {
var o = "*";
var theFrame = findRelay();
if (!theFrame) throw "can't find relay frame";
function onMessage(e) {
var d;
o = e.origin;
try {
d = JSON.parse(e.data);
} catch(e) { }
if (cb) cb(o, d.d, function(r) {
cb = undefined;
theFrame.doPost(JSON.stringify({a: 'response', d: r}), o);
});
}
addListener(theFrame, 'message', onMessage);
// we cannot post to our parent that we're ready before the iframe
// is loaded.
try {
theFrame.doPost('{"a": "ready"}', o);
} catch(e) {
addListener(theFrame, 'load', function(e) {
theFrame.doPost('{"a": "ready"}', o);
});
}
// if window is unloaded and the client hasn't called cb, it's an error
addListener(window, 'unload', function() {
if (cb) theFrame.doPost(JSON.stringify({
a: 'error', d: 'client closed window'
}), o);
cb = undefined;
// explicitly close the window, in case the client is trying to reload or nav
try { window.close(); } catch (e) { };
});
}
};
} else if (isSupported()) {
return {
open: function(url, relay_url, winopts, arg, cb) {
if (!cb) throw "missing required callback argument";
// sanity check, are url and relay_url the same origin?
var origin = extractOrigin(url);
if (origin !== extractOrigin(relay_url)) {
setTimeout(function() {
cb('invalid arguments: origin of url and relay_url must match');
})
return;
}
var w = window.open(url, null, isFennec() ? undefined : winopts);
var req = JSON.stringify({a: 'request', d: arg});
// cleanup on unload
function cleanup() {
if (w) w.close();
w = undefined;
}
addListener(window, 'unload', cleanup);
function onMessage(e) {
try {
var d = JSON.parse(e.data);
if (d.a === 'ready') w.postMessage(req, origin);
else if (d.a === 'error') cb(d.d);
else if (d.a === 'response') {
removeListener(window, 'message', onMessage);
removeListener(window, 'unload', cleanup);
cleanup();
cb(null, d.d);
}
} catch(e) { }
}
addListener(window, 'message', onMessage);
return {
close: function() {
if (w) w.close();
w = undefined;
},
focus: function() {
if (w) w.focus();
}
};
},
onOpen: function(cb) {
var o = "*";
var parentWin = window.opener;
function onMessage(e) {
var d;
o = e.origin;
try {
d = JSON.parse(e.data);
} catch(e) {
// ignore
}
cb(o, d.d, function(r) {
cb = undefined;
parentWin.postMessage(JSON.stringify({a: 'response', d: r}), o);
});
}
addListener(window, 'message', onMessage);
parentWin.postMessage('{"a": "ready"}', o);
// if window is unloaded and the client hasn't called cb, it's an error
addListener(window, 'unload', function() {
if (cb) parentWin.postMessage(JSON.stringify({
a: 'error',
d: 'client closed window'
}), o);
cb = undefined;
// explicitly close the window, in case the client is trying to reload or nav
try { window.close(); } catch (e) { };
});
}
};
} else {
return {
open: function(url, winopts, arg, cb) {
setTimeout(function() { cb("unsupported browser"); }, 0);
},
onOpen: function(cb) {
setTimeout(function() { cb("unsupported browser"); }, 0);
}
};
}
})();