-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (29 loc) · 887 Bytes
/
index.js
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
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
var callbackMap = {}
var chopperNative = window.webkit.messageHandlers.chopperNative
var chopperNativeCallback = window.webkit.messageHandlers.chopperNativeCallback
window.chopperWebCallback = {}
// chooper web
window.chopperWebCallback.callback = function (isSuccess, p) {
var callback = callbackMap[p.callbackID]
if (callback) {
callback(isSuccess, p.params)
delete callbackMap[p.callbackID]
}
}
function dispatch(module, action, params, callback) {
var UUID = uuidv4()
var message = {
'module': module,
'action': action,
'params': params,
'callbackID': UUID
}
chopperNative.postMessage(message)
callbackMap[UUID] = callback
}