|
29 | 29 | var has_primary;
|
30 | 30 | var iframes = [];
|
31 | 31 | var index = 0;
|
| 32 | + var proxy_mode = false; |
32 | 33 | var channel;
|
33 | 34 | var primary = true;
|
34 | 35 | // we use id because storage event is not executed if message was not
|
|
83 | 84 | serializer.to = to;
|
84 | 85 | serializer.from = from;
|
85 | 86 | },
|
86 |
| - proxy: function(url) { |
87 |
| - if (typeof url === 'string' && host(url) !== window.location.host) { |
88 |
| - domains = domains || []; |
89 |
| - domains.push(origin(url)); |
90 |
| - var iframe = document.createElement('iframe'); |
91 |
| - iframe.style.width = iframe.style.height = 0; |
92 |
| - iframe.style.border = 'none'; |
93 |
| - var proxy_url = url; |
94 |
| - if (!url.match(/\.html$/)) { |
95 |
| - proxy_url = url.replace(/\/$/, '') + '/proxy.html'; |
96 |
| - } |
97 |
| - iframe.addEventListener('error', function handler() { |
98 |
| - setTimeout(function() { |
99 |
| - throw new Error('html proxy file not found on "' + url + |
100 |
| - '" url'); |
101 |
| - }, 0); |
102 |
| - iframe.removeEventListener('error', handler); |
103 |
| - }); |
104 |
| - iframe.addEventListener('load', function handler() { |
105 |
| - var win; |
106 |
| - // fix for Safari |
107 |
| - // https://stackoverflow.com/q/42632188/387194 |
108 |
| - try { |
109 |
| - win = iframe.contentWindow; |
110 |
| - } catch(e) { |
111 |
| - win = iframe.contentWindow; |
| 87 | + proxy: function() { |
| 88 | + [].slice.call(arguments).forEach(function(url) { |
| 89 | + if (typeof url === 'string' && host(url) !== window.location.host) { |
| 90 | + domains = domains || []; |
| 91 | + domains.push(origin(url)); |
| 92 | + var iframe = document.createElement('iframe'); |
| 93 | + iframe.style.width = iframe.style.height = 0; |
| 94 | + iframe.style.border = 'none'; |
| 95 | + var proxy_url = url; |
| 96 | + if (!url.match(/\.html$/)) { |
| 97 | + proxy_url = url.replace(/\/$/, '') + '/proxy.html'; |
112 | 98 | }
|
113 |
| - iframes.push({window: win, node: iframe}); |
114 |
| - iframe.removeEventListener('load', handler); |
115 |
| - }); |
116 |
| - document.body.appendChild(iframe); |
117 |
| - iframe.src = proxy_url; |
| 99 | + iframe.addEventListener('error', function handler() { |
| 100 | + setTimeout(function() { |
| 101 | + throw new Error('html proxy file not found on "' + url + |
| 102 | + '" url'); |
| 103 | + }, 0); |
| 104 | + iframe.removeEventListener('error', handler); |
| 105 | + }); |
| 106 | + iframe.addEventListener('load', function handler() { |
| 107 | + var win; |
| 108 | + // fix for Safari |
| 109 | + // https://stackoverflow.com/q/42632188/387194 |
| 110 | + try { |
| 111 | + win = iframe.contentWindow; |
| 112 | + } catch(e) { |
| 113 | + win = iframe.contentWindow; |
| 114 | + } |
| 115 | + iframes.push({window: win, node: iframe}); |
| 116 | + iframe.removeEventListener('load', handler); |
| 117 | + }); |
| 118 | + document.body.appendChild(iframe); |
| 119 | + iframe.src = proxy_url; |
| 120 | + } |
| 121 | + }); |
| 122 | + if (!arguments.length && is_iframe) { |
| 123 | + proxy_mode = true; |
118 | 124 | }
|
119 | 125 | },
|
120 | 126 | on: function(event, fn) {
|
|
360 | 366 | }
|
361 | 367 | })();
|
362 | 368 | // -------------------------------------------------------------------------
|
| 369 | + function is_proxy_iframe() { |
| 370 | + return is_iframe && proxy_mode; |
| 371 | + } |
| 372 | + // ------------------------------------------------------------------------- |
363 | 373 | function send_to_iframes(key, data) {
|
364 | 374 | // propagate events to iframes
|
365 | 375 | iframes.forEach(function(iframe) {
|
|
439 | 449 | sysend.emit('__primary__');
|
440 | 450 | }
|
441 | 451 | // -------------------------------------------------------------------------
|
442 |
| - init(); |
| 452 | + window.addEventListener('load', function() { |
| 453 | + setTimeout(init, 0); |
| 454 | + }); |
443 | 455 | // -------------------------------------------------------------------------
|
444 | 456 | function init() {
|
445 | 457 | if (typeof window.BroadcastChannel === 'function') {
|
446 | 458 | channel = new window.BroadcastChannel(uniq_prefix);
|
447 | 459 | channel.addEventListener('message', function(event) {
|
448 | 460 | if (event.target.name === uniq_prefix) {
|
449 |
| - if (is_iframe) { |
| 461 | + if (is_proxy_iframe()) { |
450 | 462 | var payload = {
|
451 | 463 | name: uniq_prefix,
|
452 | 464 | data: event.data,
|
|
494 | 506 | }, false);
|
495 | 507 | }
|
496 | 508 |
|
497 |
| - if (is_iframe) { |
| 509 | + if (is_proxy_iframe()) { |
498 | 510 | window.addEventListener('message', function(e) {
|
499 | 511 | if (is_sysend_post_message(e) && is_valid_origin(e.origin)) {
|
500 | 512 | try {
|
|
0 commit comments