Skip to content

Commit 788c930

Browse files
committed
fix running app inside iframe #34
1 parent 93d6b33 commit 788c930

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

proxy.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<script src="https://cdn.jsdelivr.net/npm/sysend"></script>
77
<!-- EDIT THIS FILE AND CALL sysend.channel() WITH ALLOWED DOMAINS -->
88
<script>
9-
sysend.channel();
9+
sysend.channel();
10+
sysend.proxy();
1011
</script>
1112
</head>
1213
<body>

sysend.js

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
var has_primary;
3030
var iframes = [];
3131
var index = 0;
32+
var proxy_mode = false;
3233
var channel;
3334
var primary = true;
3435
// we use id because storage event is not executed if message was not
@@ -83,38 +84,43 @@
8384
serializer.to = to;
8485
serializer.from = from;
8586
},
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';
11298
}
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;
118124
}
119125
},
120126
on: function(event, fn) {
@@ -360,6 +366,10 @@
360366
}
361367
})();
362368
// -------------------------------------------------------------------------
369+
function is_proxy_iframe() {
370+
return is_iframe && proxy_mode;
371+
}
372+
// -------------------------------------------------------------------------
363373
function send_to_iframes(key, data) {
364374
// propagate events to iframes
365375
iframes.forEach(function(iframe) {
@@ -439,14 +449,16 @@
439449
sysend.emit('__primary__');
440450
}
441451
// -------------------------------------------------------------------------
442-
init();
452+
window.addEventListener('load', function() {
453+
setTimeout(init, 0);
454+
});
443455
// -------------------------------------------------------------------------
444456
function init() {
445457
if (typeof window.BroadcastChannel === 'function') {
446458
channel = new window.BroadcastChannel(uniq_prefix);
447459
channel.addEventListener('message', function(event) {
448460
if (event.target.name === uniq_prefix) {
449-
if (is_iframe) {
461+
if (is_proxy_iframe()) {
450462
var payload = {
451463
name: uniq_prefix,
452464
data: event.data,
@@ -494,7 +506,7 @@
494506
}, false);
495507
}
496508

497-
if (is_iframe) {
509+
if (is_proxy_iframe()) {
498510
window.addEventListener('message', function(e) {
499511
if (is_sysend_post_message(e) && is_valid_origin(e.origin)) {
500512
try {

0 commit comments

Comments
 (0)