123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 'use strict';
- var eventUtils = require('./event')
- , browser = require('./browser')
- ;
- var debug = function() {};
- if (process.env.NODE_ENV !== 'production') {
- debug = require('debug')('sockjs-client:utils:iframe');
- }
- module.exports = {
- WPrefix: '_jp'
- , currentWindowId: null
- , polluteGlobalNamespace: function() {
- if (!(module.exports.WPrefix in global)) {
- global[module.exports.WPrefix] = {};
- }
- }
- , postMessage: function(type, data) {
- if (global.parent !== global) {
- global.parent.postMessage(JSON.stringify({
- windowId: module.exports.currentWindowId
- , type: type
- , data: data || ''
- }), '*');
- } else {
- debug('Cannot postMessage, no parent window.', type, data);
- }
- }
- , createIframe: function(iframeUrl, errorCallback) {
- var iframe = global.document.createElement('iframe');
- var tref, unloadRef;
- var unattach = function() {
- debug('unattach');
- clearTimeout(tref);
-
- try {
- iframe.onload = null;
- } catch (x) {
-
- }
- iframe.onerror = null;
- };
- var cleanup = function() {
- debug('cleanup');
- if (iframe) {
- unattach();
-
-
-
- setTimeout(function() {
- if (iframe) {
- iframe.parentNode.removeChild(iframe);
- }
- iframe = null;
- }, 0);
- eventUtils.unloadDel(unloadRef);
- }
- };
- var onerror = function(err) {
- debug('onerror', err);
- if (iframe) {
- cleanup();
- errorCallback(err);
- }
- };
- var post = function(msg, origin) {
- debug('post', msg, origin);
- setTimeout(function() {
- try {
-
-
- if (iframe && iframe.contentWindow) {
- iframe.contentWindow.postMessage(msg, origin);
- }
- } catch (x) {
-
- }
- }, 0);
- };
- iframe.src = iframeUrl;
- iframe.style.display = 'none';
- iframe.style.position = 'absolute';
- iframe.onerror = function() {
- onerror('onerror');
- };
- iframe.onload = function() {
- debug('onload');
-
-
- clearTimeout(tref);
- tref = setTimeout(function() {
- onerror('onload timeout');
- }, 2000);
- };
- global.document.body.appendChild(iframe);
- tref = setTimeout(function() {
- onerror('timeout');
- }, 15000);
- unloadRef = eventUtils.unloadAdd(cleanup);
- return {
- post: post
- , cleanup: cleanup
- , loaded: unattach
- };
- }
- , createHtmlfile: function(iframeUrl, errorCallback) {
- var axo = ['Active'].concat('Object').join('X');
- var doc = new global[axo]('htmlfile');
- var tref, unloadRef;
- var iframe;
- var unattach = function() {
- clearTimeout(tref);
- iframe.onerror = null;
- };
- var cleanup = function() {
- if (doc) {
- unattach();
- eventUtils.unloadDel(unloadRef);
- iframe.parentNode.removeChild(iframe);
- iframe = doc = null;
- CollectGarbage();
- }
- };
- var onerror = function(r) {
- debug('onerror', r);
- if (doc) {
- cleanup();
- errorCallback(r);
- }
- };
- var post = function(msg, origin) {
- try {
-
-
- setTimeout(function() {
- if (iframe && iframe.contentWindow) {
- iframe.contentWindow.postMessage(msg, origin);
- }
- }, 0);
- } catch (x) {
-
- }
- };
- doc.open();
- doc.write('<html><s' + 'cript>' +
- 'document.domain="' + global.document.domain + '";' +
- '</s' + 'cript></html>');
- doc.close();
- doc.parentWindow[module.exports.WPrefix] = global[module.exports.WPrefix];
- var c = doc.createElement('div');
- doc.body.appendChild(c);
- iframe = doc.createElement('iframe');
- c.appendChild(iframe);
- iframe.src = iframeUrl;
- iframe.onerror = function() {
- onerror('onerror');
- };
- tref = setTimeout(function() {
- onerror('timeout');
- }, 15000);
- unloadRef = eventUtils.unloadAdd(cleanup);
- return {
- post: post
- , cleanup: cleanup
- , loaded: unattach
- };
- }
- };
- module.exports.iframeEnabled = false;
- if (global.document) {
-
-
- module.exports.iframeEnabled = (typeof global.postMessage === 'function' ||
- typeof global.postMessage === 'object') && (!browser.isKonqueror());
- }
|