github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/lib/utils/iframe.js (about) 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 try { 75 // When the iframe is not loaded, IE raises an exception 76 // on 'contentWindow'. 77 setTimeout(function() { 78 if (iframe && iframe.contentWindow) { 79 iframe.contentWindow.postMessage(msg, origin); 80 } 81 }, 0); 82 } catch (x) { 83 // intentionally empty 84 } 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 /* jshint undef: false, newcap: false */ 115 /* eslint no-undef: 0, new-cap: 0 */ 116 , createHtmlfile: function(iframeUrl, errorCallback) { 117 var axo = ['Active'].concat('Object').join('X'); 118 var doc = new global[axo]('htmlfile'); 119 var tref, unloadRef; 120 var iframe; 121 var unattach = function() { 122 clearTimeout(tref); 123 iframe.onerror = null; 124 }; 125 var cleanup = function() { 126 if (doc) { 127 unattach(); 128 eventUtils.unloadDel(unloadRef); 129 iframe.parentNode.removeChild(iframe); 130 iframe = doc = null; 131 CollectGarbage(); 132 } 133 }; 134 var onerror = function(r) { 135 debug('onerror', r); 136 if (doc) { 137 cleanup(); 138 errorCallback(r); 139 } 140 }; 141 var post = function(msg, origin) { 142 try { 143 // When the iframe is not loaded, IE raises an exception 144 // on 'contentWindow'. 145 setTimeout(function() { 146 if (iframe && iframe.contentWindow) { 147 iframe.contentWindow.postMessage(msg, origin); 148 } 149 }, 0); 150 } catch (x) { 151 // intentionally empty 152 } 153 }; 154 155 doc.open(); 156 doc.write('<html><s' + 'cript>' + 157 'document.domain="' + global.document.domain + '";' + 158 '</s' + 'cript></html>'); 159 doc.close(); 160 doc.parentWindow[module.exports.WPrefix] = global[module.exports.WPrefix]; 161 var c = doc.createElement('div'); 162 doc.body.appendChild(c); 163 iframe = doc.createElement('iframe'); 164 c.appendChild(iframe); 165 iframe.src = iframeUrl; 166 iframe.onerror = function() { 167 onerror('onerror'); 168 }; 169 tref = setTimeout(function() { 170 onerror('timeout'); 171 }, 15000); 172 unloadRef = eventUtils.unloadAdd(cleanup); 173 return { 174 post: post 175 , cleanup: cleanup 176 , loaded: unattach 177 }; 178 } 179 }; 180 181 module.exports.iframeEnabled = false; 182 if (global.document) { 183 // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with 184 // huge delay, or not at all. 185 module.exports.iframeEnabled = (typeof global.postMessage === 'function' || 186 typeof global.postMessage === 'object') && (!browser.isKonqueror()); 187 }