|
10 | 10 |
|
11 | 11 | function noop() {}
|
12 | 12 |
|
| 13 | + function _log(level, args) { |
| 14 | + |
| 15 | + args = Array.prototype.slice.call(args); |
| 16 | + args.unshift('[post-robot]'); |
| 17 | + args.push(window.location.href); |
| 18 | + if (window.name) { |
| 19 | + args.push(window.name); |
| 20 | + } |
| 21 | + |
| 22 | + if (!window.console) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (!window.console[level]) { |
| 27 | + level = 'log'; |
| 28 | + } |
| 29 | + |
| 30 | + if (!window.console[level]) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + window.console[level].apply(window.console, args); |
| 35 | + } |
| 36 | + |
| 37 | + function log() { |
| 38 | + _log('log', arguments); |
| 39 | + } |
| 40 | + |
| 41 | + function logError() { |
| 42 | + _log('error', arguments); |
| 43 | + } |
| 44 | + |
13 | 45 | if (!window.postMessage) {
|
14 | 46 |
|
15 | 47 | if (window.console && window.console.warn) {
|
|
42 | 74 |
|
43 | 75 | var requestListeners = {};
|
44 | 76 | var responseHandlers = {};
|
| 77 | + var mockRequestListeners = {}; |
45 | 78 | var proxies = [];
|
46 | 79 |
|
47 | 80 | var parent;
|
|
66 | 99 | }
|
67 | 100 |
|
68 | 101 | function sendMessage(win, message) {
|
69 |
| - console.error('%% send', message); |
70 |
| - |
| 102 | + log('#send', message.type, message.name, message); |
71 | 103 | win.postMessage(JSON.stringify(message), '*');
|
72 | 104 | }
|
73 | 105 |
|
|
81 | 113 | throw new Error('Expected options.name');
|
82 | 114 | }
|
83 | 115 |
|
84 |
| - if (typeof options.window === 'string') { |
| 116 | + if (requestListeners[options.name] && requestListeners[options.name].mock) { |
| 117 | + options.window = window; |
| 118 | + |
| 119 | + } else if (typeof options.window === 'string') { |
85 | 120 | var el = document.getElementById(options.window);
|
86 | 121 |
|
87 | 122 | if (!el) {
|
|
169 | 204 | throw new Error('Expected options.name');
|
170 | 205 | }
|
171 | 206 |
|
172 |
| - if (requestListeners[options.name]) { |
| 207 | + if (requestListeners[options.name] && !options.override) { |
173 | 208 | throw new Error('Post message response handler already registered: ' + options.name);
|
174 | 209 | }
|
175 | 210 |
|
|
188 | 223 | if (options.window.closed) {
|
189 | 224 | clearInterval(interval);
|
190 | 225 | delete requestListeners[options.name];
|
191 |
| - options.handler(new Error('Post message target window is closed'), null, noop); |
| 226 | + options.handler(new Error('Post message target window is closed'), null, function(err) { |
| 227 | + logError('Unhandled error', err.stack || err.toString()); |
| 228 | + }); |
192 | 229 | }
|
193 | 230 | }, 50);
|
194 | 231 | }
|
| 232 | + |
| 233 | + return { |
| 234 | + cancel: function() { |
| 235 | + delete requestListeners[options.name]; |
| 236 | + } |
| 237 | + }; |
195 | 238 | }
|
196 | 239 |
|
197 | 240 | function quickPostMessageListen(name, options, callback) {
|
|
204 | 247 | options.name = name;
|
205 | 248 | options.handler = options.handler || callback;
|
206 | 249 |
|
207 |
| - postMessageListen(options); |
| 250 | + return postMessageListen(options); |
208 | 251 | }
|
209 | 252 |
|
210 | 253 | function quickPostMessageListenOnce(name, options, callback) {
|
|
216 | 259 |
|
217 | 260 | options.once = true;
|
218 | 261 |
|
219 |
| - quickPostMessageListen(name, options, callback); |
| 262 | + return quickPostMessageListen(name, options, callback); |
| 263 | + } |
| 264 | + |
| 265 | + function quickPostMessageListenMock(name, options, callback) { |
| 266 | + |
| 267 | + if (!callback && options instanceof Function) { |
| 268 | + callback = options; |
| 269 | + options = {}; |
| 270 | + } |
| 271 | + |
| 272 | + options.mock = true; |
| 273 | + |
| 274 | + return quickPostMessageListen(name, options, callback); |
220 | 275 | }
|
221 | 276 |
|
222 | 277 | var POST_MESSAGE_HANDLERS = {};
|
|
373 | 428 | if (allowProxy(message)) {
|
374 | 429 | for (var i=0; i<proxies.length; i++) {
|
375 | 430 | if (event.source === proxies[i].from) {
|
| 431 | + log('#proxy', proxies[i].from, 'to', proxies[i].to); |
376 | 432 | return proxies[i].to.postMessage(event.data, '*');
|
377 | 433 | }
|
378 | 434 | }
|
|
382 | 438 | return;
|
383 | 439 | }
|
384 | 440 |
|
385 |
| - console.error('!! message', window.name, window.location.href, message.name, message); |
| 441 | + log('#receive', message.type, message.name, message); |
386 | 442 |
|
387 | 443 | POST_MESSAGE_HANDLERS[message.type](event, message);
|
388 | 444 | };
|
|
434 | 490 | on: quickPostMessageListen,
|
435 | 491 | once: quickPostMessageListenOnce,
|
436 | 492 |
|
| 493 | + mock: quickPostMessageListenMock, |
| 494 | + |
437 | 495 | sendToParent: function(name, data, callback) {
|
438 | 496 | return this.send(parent, name, data, callback);
|
439 | 497 | },
|
|
0 commit comments