1 | 'use strict';
|
---|
2 |
|
---|
3 | var eventUtils = require('./event')
|
---|
4 | , JSON3 = require('json3')
|
---|
5 | , browser = require('./browser')
|
---|
6 | ;
|
---|
7 |
|
---|
8 | var debug = function() {};
|
---|
9 | if (process.env.NODE_ENV !== 'production') {
|
---|
10 | debug = require('debug')('sockjs-client:utils:iframe');
|
---|
11 | }
|
---|
12 |
|
---|
13 | module.exports = {
|
---|
14 | WPrefix: '_jp'
|
---|
15 | , currentWindowId: null
|
---|
16 |
|
---|
17 | , polluteGlobalNamespace: function() {
|
---|
18 | if (!(module.exports.WPrefix in global)) {
|
---|
19 | global[module.exports.WPrefix] = {};
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | , postMessage: function(type, data) {
|
---|
24 | if (global.parent !== global) {
|
---|
25 | global.parent.postMessage(JSON3.stringify({
|
---|
26 | windowId: module.exports.currentWindowId
|
---|
27 | , type: type
|
---|
28 | , data: data || ''
|
---|
29 | }), '*');
|
---|
30 | } else {
|
---|
31 | debug('Cannot postMessage, no parent window.', type, data);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | , createIframe: function(iframeUrl, errorCallback) {
|
---|
36 | var iframe = global.document.createElement('iframe');
|
---|
37 | var tref, unloadRef;
|
---|
38 | var unattach = function() {
|
---|
39 | debug('unattach');
|
---|
40 | clearTimeout(tref);
|
---|
41 | // Explorer had problems with that.
|
---|
42 | try {
|
---|
43 | iframe.onload = null;
|
---|
44 | } catch (x) {
|
---|
45 | // intentionally empty
|
---|
46 | }
|
---|
47 | iframe.onerror = null;
|
---|
48 | };
|
---|
49 | var cleanup = function() {
|
---|
50 | debug('cleanup');
|
---|
51 | if (iframe) {
|
---|
52 | unattach();
|
---|
53 | // This timeout makes chrome fire onbeforeunload event
|
---|
54 | // within iframe. Without the timeout it goes straight to
|
---|
55 | // onunload.
|
---|
56 | setTimeout(function() {
|
---|
57 | if (iframe) {
|
---|
58 | iframe.parentNode.removeChild(iframe);
|
---|
59 | }
|
---|
60 | iframe = null;
|
---|
61 | }, 0);
|
---|
62 | eventUtils.unloadDel(unloadRef);
|
---|
63 | }
|
---|
64 | };
|
---|
65 | var onerror = function(err) {
|
---|
66 | debug('onerror', err);
|
---|
67 | if (iframe) {
|
---|
68 | cleanup();
|
---|
69 | errorCallback(err);
|
---|
70 | }
|
---|
71 | };
|
---|
72 | var post = function(msg, origin) {
|
---|
73 | debug('post', msg, origin);
|
---|
74 | setTimeout(function() {
|
---|
75 | try {
|
---|
76 | // When the iframe is not loaded, IE raises an exception
|
---|
77 | // on 'contentWindow'.
|
---|
78 | if (iframe && iframe.contentWindow) {
|
---|
79 | iframe.contentWindow.postMessage(msg, origin);
|
---|
80 | }
|
---|
81 | } catch (x) {
|
---|
82 | // intentionally empty
|
---|
83 | }
|
---|
84 | }, 0);
|
---|
85 | };
|
---|
86 |
|
---|
87 | iframe.src = iframeUrl;
|
---|
88 | iframe.style.display = 'none';
|
---|
89 | iframe.style.position = 'absolute';
|
---|
90 | iframe.onerror = function() {
|
---|
91 | onerror('onerror');
|
---|
92 | };
|
---|
93 | iframe.onload = function() {
|
---|
94 | debug('onload');
|
---|
95 | // `onload` is triggered before scripts on the iframe are
|
---|
96 | // executed. Give it few seconds to actually load stuff.
|
---|
97 | clearTimeout(tref);
|
---|
98 | tref = setTimeout(function() {
|
---|
99 | onerror('onload timeout');
|
---|
100 | }, 2000);
|
---|
101 | };
|
---|
102 | global.document.body.appendChild(iframe);
|
---|
103 | tref = setTimeout(function() {
|
---|
104 | onerror('timeout');
|
---|
105 | }, 15000);
|
---|
106 | unloadRef = eventUtils.unloadAdd(cleanup);
|
---|
107 | return {
|
---|
108 | post: post
|
---|
109 | , cleanup: cleanup
|
---|
110 | , loaded: unattach
|
---|
111 | };
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* eslint no-undef: "off", new-cap: "off" */
|
---|
115 | , createHtmlfile: function(iframeUrl, errorCallback) {
|
---|
116 | var axo = ['Active'].concat('Object').join('X');
|
---|
117 | var doc = new global[axo]('htmlfile');
|
---|
118 | var tref, unloadRef;
|
---|
119 | var iframe;
|
---|
120 | var unattach = function() {
|
---|
121 | clearTimeout(tref);
|
---|
122 | iframe.onerror = null;
|
---|
123 | };
|
---|
124 | var cleanup = function() {
|
---|
125 | if (doc) {
|
---|
126 | unattach();
|
---|
127 | eventUtils.unloadDel(unloadRef);
|
---|
128 | iframe.parentNode.removeChild(iframe);
|
---|
129 | iframe = doc = null;
|
---|
130 | CollectGarbage();
|
---|
131 | }
|
---|
132 | };
|
---|
133 | var onerror = function(r) {
|
---|
134 | debug('onerror', r);
|
---|
135 | if (doc) {
|
---|
136 | cleanup();
|
---|
137 | errorCallback(r);
|
---|
138 | }
|
---|
139 | };
|
---|
140 | var post = function(msg, origin) {
|
---|
141 | try {
|
---|
142 | // When the iframe is not loaded, IE raises an exception
|
---|
143 | // on 'contentWindow'.
|
---|
144 | setTimeout(function() {
|
---|
145 | if (iframe && iframe.contentWindow) {
|
---|
146 | iframe.contentWindow.postMessage(msg, origin);
|
---|
147 | }
|
---|
148 | }, 0);
|
---|
149 | } catch (x) {
|
---|
150 | // intentionally empty
|
---|
151 | }
|
---|
152 | };
|
---|
153 |
|
---|
154 | doc.open();
|
---|
155 | doc.write('<html><s' + 'cript>' +
|
---|
156 | 'document.domain="' + global.document.domain + '";' +
|
---|
157 | '</s' + 'cript></html>');
|
---|
158 | doc.close();
|
---|
159 | doc.parentWindow[module.exports.WPrefix] = global[module.exports.WPrefix];
|
---|
160 | var c = doc.createElement('div');
|
---|
161 | doc.body.appendChild(c);
|
---|
162 | iframe = doc.createElement('iframe');
|
---|
163 | c.appendChild(iframe);
|
---|
164 | iframe.src = iframeUrl;
|
---|
165 | iframe.onerror = function() {
|
---|
166 | onerror('onerror');
|
---|
167 | };
|
---|
168 | tref = setTimeout(function() {
|
---|
169 | onerror('timeout');
|
---|
170 | }, 15000);
|
---|
171 | unloadRef = eventUtils.unloadAdd(cleanup);
|
---|
172 | return {
|
---|
173 | post: post
|
---|
174 | , cleanup: cleanup
|
---|
175 | , loaded: unattach
|
---|
176 | };
|
---|
177 | }
|
---|
178 | };
|
---|
179 |
|
---|
180 | module.exports.iframeEnabled = false;
|
---|
181 | if (global.document) {
|
---|
182 | // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with
|
---|
183 | // huge delay, or not at all.
|
---|
184 | module.exports.iframeEnabled = (typeof global.postMessage === 'function' ||
|
---|
185 | typeof global.postMessage === 'object') && (!browser.isKonqueror());
|
---|
186 | }
|
---|