init_script_ios top-level property

String init_script_ios
read / write

Implementation

String init_script_ios = '''
(function() {
	if (window.JKEventHandler) {
		return;
	}

	if (!window.onerror) {
		window.onerror = function(msg, url, line) {
			console.log("JKEventHandler: ERROR:" + msg + "@" + url + ":" + line);
		}
	}
	var JKEventHandler = window.JKEventHandler = {
		registerHandler: registerHandler,
		callNativeFunction: callHandler,
		disableJavscriptAlertBoxSafetyTimeout: disableJavscriptAlertBoxSafetyTimeout,
		_handleMessageFromNative: _handleMessageFromNative,
		_fetchQueue: _fetchQueue
	};

	var messagingIframe;
	var sendMessageQueue = [];
	var messageHandlers = {};

	var CUSTOM_PROTOCOL_SCHEME = 'jsbridge';

	var responseCallbacks = {};
	var uniqueId = 1;
	var dispatchMessagesWithTimeoutSafety = true;

	function registerHandler(handlerName, handler) {
		messageHandlers[handlerName] = handler;
	}

	function callHandler(handlerName, data, responseCallback) {
		if (arguments.length == 2 && typeof data == 'function') {
			responseCallback = data;
			data = null;
		}
		_doSend({ handlerName:handlerName, data:data }, responseCallback);
	}

	function disableJavscriptAlertBoxSafetyTimeout() {
		dispatchMessagesWithTimeoutSafety = false;
	}

	function _doSend(message, responseCallback) {
		if (responseCallback) {
			var callbackId = 'cb_'+(uniqueId++)+'_'+new Date().getTime();
			responseCallbacks[callbackId] = responseCallback;
			message['callbackId'] = callbackId;
		}
		sendMessageQueue.push(message);
		_getIframe().src = 'jsbridge://return/fetch'
	}

	function _dispatchMessageFromNative(messageJSON) {
		if (dispatchMessagesWithTimeoutSafety) {
			setTimeout(_doDispatchMessageFromNative, 0);
		} else {
			 _doDispatchMessageFromNative();
		}

		function _doDispatchMessageFromNative() {
			var message = JSON.parse(messageJSON);
			var messageHandler;
			var responseCallback;

			if (message.responseId) {
				responseCallback = responseCallbacks[message.responseId];
				if (!responseCallback) {
					return;
				}
				responseCallback(message.responseData);
				delete responseCallbacks[message.responseId];
			} else {
				if (message.callbackId) {
					var callbackResponseId = message.callbackId;
					responseCallback = function(responseData) {
						_doSend({ handlerName:message.handlerName, responseId:callbackResponseId, responseData:responseData });
					};
				}

				var handler = messageHandlers[message.handlerName];
				if (!handler) {
					console.log("JKEventHandler: WARNING: no handler for message from ObjC:", message);
				} else {
					handler(message.data, responseCallback);
				}
			}
		}
	}

	function _fetchQueue() {
	  if (Array.isArray(sendMessageQueue) && sendMessageQueue.length === 0) {
	    return;
	  }
		var messageQueueString = JSON.stringify(sendMessageQueue);
		sendMessageQueue = [];
		if (messageQueueString === '[]') {
		  return;
		}
		_getIframe().src = CUSTOM_PROTOCOL_SCHEME + '://return/sendMsg/' + encodeURIComponent(messageQueueString);
	}

	function _handleMessageFromNative(messageJSON) {
    _dispatchMessageFromNative(messageJSON);
	}

  function _getIframe() {
    if (typeof(messagingIframe) == 'undefined') {
      messagingIframe = document.createElement('iframe');
	    messagingIframe.style.display = 'none';
	    messagingIframe.src = 'jsbridge://return/fetch'
	    document.documentElement.appendChild(messagingIframe);
    }
    return messagingIframe;
  }

	registerHandler("_disableJavascriptAlertBoxSafetyTimeout", disableJavscriptAlertBoxSafetyTimeout);
	setTimeout(_callWVJBCallbacks, 0);

	function _callWVJBCallbacks() {
		var callbacks = window.WVJBCallbacks || [];
		delete window.WVJBCallbacks;
		for (var i=0; i<callbacks.length; i++) {
			callbacks[i](window.JKEventHandler);
		}
	}
})();''';