Files
react-native-webview-bridge/scripts/WebViewBridge.js
T

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-09-25 16:29:07 -04:00
(function () {
2016-09-28 23:49:20 -04:00
var WebViewBridge = window.WebViewBridge ? window.WebViewBridge : {}
var queue = []
var onMessageListeners = {}
2016-09-25 16:29:07 -04:00
//base64 encode and decode polyfil
//modified version of https://github.com/davidchambers/Base64.js
//you can replace the b64Encode and b64Decode for atob and btoa.
var b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
function b64Encode(input) {
var output = ''
for (
var block, charCode, idx = 0, map = b64ch;
input.charAt(idx | 0) || (map = '=', idx % 1);
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
) {
charCode = input.charCodeAt(idx += 3/4)
block = block << 8 | charCode
}
return output
}
function b64Decode(input) {
var str = input.replace(/=+$/, '')
var output = ''
for (
var bc = 0, bs, buffer, idx = 0;
buffer = str.charAt(idx++);
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
) {
buffer = b64ch.indexOf(buffer)
}
return output
}
function encode(input) {
if (typeof input !== 'string') {
try {
input = JSON.stringify(input)
} catch(e) {}
}
input = unescape(encodeURIComponent(input))
return b64Encode(input)
}
function decode(input) {
2016-09-27 01:10:31 -04:00
var result = decodeURIComponent(escape(b64Decode(input)))
2016-09-27 00:26:14 -04:00
try {
result = JSON.parse(result)
} catch(e){}
return result
2016-09-25 16:29:07 -04:00
}
function signalNative() {
2016-09-28 23:49:20 -04:00
if (WebViewBridge.nativeAndroidSend) {
WebViewBridge.nativeAndroidSend(WebViewBridge.__fetch__())
} else {
window.location = 'rnwb://message' + new Date().getTime()
}
2016-09-25 16:29:07 -04:00
}
function dispatch(name, value) {
var event = new CustomEvent(name, {
detail: value,
bubbles: true
});
setTimeout(function () {
2016-09-28 23:49:20 -04:00
window.document.dispatchEvent(event)
2016-09-25 16:29:07 -04:00
}, 15)
}
2016-09-28 23:49:20 -04:00
WebViewBridge.__dispatch__ = dispatch
WebViewBridge.__push__ = function (encoded) {
//we need to release native caller as soon as possible
//that's why we are wrap this on setTimeout
setTimeout(function () {
var fn = null
var decoded = decode(encoded)
Object.keys(onMessageListeners).forEach(function (onMessage) {
fn = onMessageListeners[onMessage]
fn(decoded)
})
}, 15)
}
WebViewBridge.__fetch__ = function () {
var val = JSON.stringify(queue)
queue = []
return val
}
WebViewBridge.send = function (input) {
queue.push(encode(input))
setTimeout(signalNative, 15)
}
WebViewBridge.addMessageListener = function(fn) {
onMessageListeners[fn] = fn
}
WebViewBridge.removeMessageListener = function (fn) {
delete onMessageListeners[fn]
2016-09-25 16:29:07 -04:00
}
2016-09-28 23:49:20 -04:00
window.WebViewBridge = WebViewBridge
dispatch('webviewbridge:init', WebViewBridge)
2016-09-25 16:29:07 -04:00
}())