|
1 | 1 | ;(function() { |
2 | | - if (window.WebViewJavascriptBridge) { return } |
3 | | - var messagingIframe |
4 | | - var sendMessageQueue = [] |
5 | | - var receiveMessageQueue = [] |
6 | | - var messageHandlers = {} |
| 2 | + if (window.WebViewJavascriptBridge) { |
| 3 | + return; |
| 4 | + } |
| 5 | + window.WebViewJavascriptBridge = { |
| 6 | + init: init, |
| 7 | + send: send, |
| 8 | + registerHandler: registerHandler, |
| 9 | + callHandler: callHandler, |
| 10 | + _fetchQueue: _fetchQueue, |
| 11 | + _handleMessageFromObjC: _handleMessageFromObjC |
| 12 | + }; |
| 13 | + |
| 14 | + var messagingIframe; |
| 15 | + var sendMessageQueue = []; |
| 16 | + var receiveMessageQueue = []; |
| 17 | + var messageHandlers = {}; |
7 | 18 |
|
8 | | - var CUSTOM_PROTOCOL_SCHEME = 'wvjbscheme' |
9 | | - var QUEUE_HAS_MESSAGE = '__WVJB_QUEUE_MESSAGE__' |
| 19 | + var CUSTOM_PROTOCOL_SCHEME = 'wvjbscheme'; |
| 20 | + var QUEUE_HAS_MESSAGE = '__WVJB_QUEUE_MESSAGE__'; |
10 | 21 |
|
11 | | - var responseCallbacks = {} |
12 | | - var uniqueId = 1 |
| 22 | + var responseCallbacks = {}; |
| 23 | + var uniqueId = 1; |
13 | 24 |
|
14 | 25 | function _createQueueReadyIframe(doc) { |
15 | | - messagingIframe = doc.createElement('iframe') |
16 | | - messagingIframe.style.display = 'none' |
17 | | - messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE |
18 | | - doc.documentElement.appendChild(messagingIframe) |
| 26 | + messagingIframe = doc.createElement('iframe'); |
| 27 | + messagingIframe.style.display = 'none'; |
| 28 | + messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE; |
| 29 | + doc.documentElement.appendChild(messagingIframe); |
19 | 30 | } |
20 | 31 |
|
21 | 32 | function init(messageHandler) { |
22 | | - if (WebViewJavascriptBridge._messageHandler) { throw new Error('WebViewJavascriptBridge.init called twice') } |
23 | | - WebViewJavascriptBridge._messageHandler = messageHandler |
24 | | - var receivedMessages = receiveMessageQueue |
25 | | - receiveMessageQueue = null |
| 33 | + if (WebViewJavascriptBridge._messageHandler) { |
| 34 | + throw new Error('WebViewJavascriptBridge.init called twice'); |
| 35 | + } |
| 36 | + WebViewJavascriptBridge._messageHandler = messageHandler; |
| 37 | + var receivedMessages = receiveMessageQueue; |
| 38 | + receiveMessageQueue = null; |
26 | 39 | for (var i=0; i<receivedMessages.length; i++) { |
27 | | - _dispatchMessageFromObjC(receivedMessages[i]) |
| 40 | + _dispatchMessageFromObjC(receivedMessages[i]); |
28 | 41 | } |
29 | 42 | } |
30 | 43 |
|
31 | 44 | function send(data, responseCallback) { |
32 | | - _doSend({ data:data }, responseCallback) |
| 45 | + _doSend({ data:data }, responseCallback); |
33 | 46 | } |
34 | 47 |
|
35 | 48 | function registerHandler(handlerName, handler) { |
36 | | - messageHandlers[handlerName] = handler |
| 49 | + messageHandlers[handlerName] = handler; |
37 | 50 | } |
38 | 51 |
|
39 | 52 | function callHandler(handlerName, data, responseCallback) { |
40 | | - _doSend({ handlerName:handlerName, data:data }, responseCallback) |
| 53 | + _doSend({ handlerName:handlerName, data:data }, responseCallback); |
41 | 54 | } |
42 | 55 |
|
43 | 56 | function _doSend(message, responseCallback) { |
44 | 57 | if (responseCallback) { |
45 | | - var callbackId = 'cb_'+(uniqueId++)+'_'+new Date().getTime() |
46 | | - responseCallbacks[callbackId] = responseCallback |
47 | | - message['callbackId'] = callbackId |
| 58 | + var callbackId = 'cb_'+(uniqueId++)+'_'+new Date().getTime(); |
| 59 | + responseCallbacks[callbackId] = responseCallback; |
| 60 | + message['callbackId'] = callbackId; |
48 | 61 | } |
49 | | - sendMessageQueue.push(message) |
50 | | - messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE |
| 62 | + sendMessageQueue.push(message); |
| 63 | + messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE; |
51 | 64 | } |
52 | 65 |
|
53 | 66 | function _fetchQueue() { |
54 | | - var messageQueueString = JSON.stringify(sendMessageQueue) |
55 | | - sendMessageQueue = [] |
56 | | - return messageQueueString |
| 67 | + var messageQueueString = JSON.stringify(sendMessageQueue); |
| 68 | + sendMessageQueue = []; |
| 69 | + return messageQueueString; |
57 | 70 | } |
58 | 71 |
|
59 | 72 | function _dispatchMessageFromObjC(messageJSON) { |
60 | 73 | setTimeout(function _timeoutDispatchMessageFromObjC() { |
61 | | - var message = JSON.parse(messageJSON) |
62 | | - var messageHandler |
63 | | - var responseCallback |
| 74 | + var message = JSON.parse(messageJSON); |
| 75 | + var messageHandler; |
| 76 | + var responseCallback; |
64 | 77 |
|
65 | 78 | if (message.responseId) { |
66 | | - responseCallback = responseCallbacks[message.responseId] |
67 | | - if (!responseCallback) { return; } |
68 | | - responseCallback(message.responseData) |
69 | | - delete responseCallbacks[message.responseId] |
| 79 | + responseCallback = responseCallbacks[message.responseId]; |
| 80 | + if (!responseCallback) { |
| 81 | + return; |
| 82 | + } |
| 83 | + responseCallback(message.responseData); |
| 84 | + delete responseCallbacks[message.responseId]; |
70 | 85 | } else { |
71 | 86 | if (message.callbackId) { |
72 | | - var callbackResponseId = message.callbackId |
| 87 | + var callbackResponseId = message.callbackId; |
73 | 88 | responseCallback = function(responseData) { |
74 | | - _doSend({ responseId:callbackResponseId, responseData:responseData }) |
75 | | - } |
| 89 | + _doSend({ responseId:callbackResponseId, responseData:responseData }); |
| 90 | + }; |
76 | 91 | } |
77 | 92 |
|
78 | | - var handler = WebViewJavascriptBridge._messageHandler |
| 93 | + var handler = WebViewJavascriptBridge._messageHandler; |
79 | 94 | if (message.handlerName) { |
80 | | - handler = messageHandlers[message.handlerName] |
| 95 | + handler = messageHandlers[message.handlerName]; |
81 | 96 | } |
82 | 97 |
|
83 | 98 | try { |
84 | | - handler(message.data, responseCallback) |
| 99 | + handler(message.data, responseCallback); |
85 | 100 | } catch(exception) { |
86 | 101 | if (typeof console != 'undefined') { |
87 | | - console.log("WebViewJavascriptBridge: WARNING: javascript handler threw.", message, exception) |
| 102 | + console.log("WebViewJavascriptBridge: WARNING: javascript handler threw.", message, exception); |
88 | 103 | } |
89 | 104 | } |
90 | 105 | } |
91 | | - }) |
| 106 | + }); |
92 | 107 | } |
93 | 108 |
|
94 | 109 | function _handleMessageFromObjC(messageJSON) { |
95 | 110 | if (receiveMessageQueue) { |
96 | | - receiveMessageQueue.push(messageJSON) |
| 111 | + receiveMessageQueue.push(messageJSON); |
97 | 112 | } else { |
98 | | - _dispatchMessageFromObjC(messageJSON) |
| 113 | + _dispatchMessageFromObjC(messageJSON); |
99 | 114 | } |
100 | 115 | } |
101 | 116 |
|
102 | | - window.WebViewJavascriptBridge = { |
103 | | - init: init, |
104 | | - send: send, |
105 | | - registerHandler: registerHandler, |
106 | | - callHandler: callHandler, |
107 | | - _fetchQueue: _fetchQueue, |
108 | | - _handleMessageFromObjC: _handleMessageFromObjC |
109 | | - } |
110 | | - |
111 | | - var doc = document |
112 | | - _createQueueReadyIframe(doc) |
113 | | - var readyEvent = doc.createEvent('Events') |
114 | | - readyEvent.initEvent('WebViewJavascriptBridgeReady') |
115 | | - readyEvent.bridge = WebViewJavascriptBridge |
116 | | - doc.dispatchEvent(readyEvent) |
| 117 | + var doc = document; |
| 118 | + _createQueueReadyIframe(doc); |
| 119 | + var readyEvent = doc.createEvent('Events'); |
| 120 | + readyEvent.initEvent('WebViewJavascriptBridgeReady'); |
| 121 | + readyEvent.bridge = WebViewJavascriptBridge; |
| 122 | + doc.dispatchEvent(readyEvent); |
117 | 123 | })(); |
0 commit comments