Nav apraksta

WebViewJavascriptBridge.js.txt 3.4KB

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