-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
123 lines (102 loc) · 2.91 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html>
<head>
<title>Hello Static World</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=320, user-scalable=no">
<style type="text/css">
body {
display: flex;
background: azure;
justify-content: center;
align-content: center;
}
button {
text-align: center;
align-self: center;
}
</style>
</head>
<body>
<div id="container">
<button id="button" onclick="clickHandler();" >Send to RN</button>
</div>
<script>
var promiseChain = Promise.resolve();
var callbacks = {};
var init = function() {
const guid = function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}
window.webViewBridge = {
/**
* send message to the React-Native WebView onMessage handler
* @param targetFunc - name of the function to invoke on the React-Native side
* @param data - data to pass
* @param success - success callback
* @param error - error callback
*/
send: function(targetFunc, data, success, error) {
var msgObj = {
targetFunc: targetFunc,
data: data || {}
};
if (success || error) {
msgObj.msgId = guid();
}
var msg = JSON.stringify(msgObj);
promiseChain = promiseChain.then(function () {
return new Promise(function (resolve, reject) {
console.log("sending message " + msgObj.targetFunc);
if (msgObj.msgId) {
callbacks[msgObj.msgId] = {
onsuccess: success,
onerror: error
};
}
window.ReactNativeWebView.postMessage(msg);
resolve();
})
}).catch(function (e) {
console.error('rnBridge send failed ' + e.message);
});
},
};
window.addEventListener('message', function(e) {
console.log("message received from react native");
var message;
try {
message = JSON.parse(e.data)
}
catch(err) {
console.error("failed to parse message from react-native " + err);
return;
}
//trigger callback
if (message.args && callbacks[message.msgId]) {
if (message.isSuccessfull) {
callbacks[message.msgId].onsuccess.apply(null, message.args);
}
else {
callbacks[message.msgId].onerror.apply(null, message.args);
}
delete callbacks[message.msgId];
}
});
};
init();
window.counter = 0;
function clickHandler() {
window.counter++;
window.webViewBridge.send('handleDataReceived', window.counter, function(res) {
window.document.getElementById("button").setAttribute("style", "background-color: " + res);
}, function(err) {
window.document.getElementById("container").setAttribute("style", "background-color: " + err);
});
}
</script>
</body>
</html>