|
1 | 1 | <!doctype html>
|
2 |
| -<html><head> |
3 |
| - <meta http-equiv="Content-Type" content="text/html; charset=utf8"> |
4 |
| - <title>Demo</title> |
5 |
| - <style type='text/css'> |
6 |
| - html { font-family:Helvetica; color:#222; } |
7 |
| - h1 { color:steelblue; font-size:24px; margin-top:24px; } |
8 |
| - button { margin:0 3px 10px; font-size:12px; } |
9 |
| - .logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; } |
10 |
| - </style><script language="javascript"> |
11 |
| - var uniqueId = 1; |
12 |
| - var testCallback; |
13 |
| - |
14 |
| - var JSBridge = window.Bridge; |
15 |
| - var JSPage = window.Page; |
16 |
| - |
17 |
| - function log(message, data) { |
18 |
| - var log = document.getElementById('log') |
19 |
| - var el = document.createElement('div') |
20 |
| - el.className = 'logLine' |
21 |
| - el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data) |
22 |
| - if (log.children.length) { log.insertBefore(el, log.children[0]) } |
23 |
| - else { log.appendChild(el) } |
24 |
| - } |
25 |
| - |
26 |
| - function setRightButtons() { |
27 |
| - var buttons = [{ "title": '按钮0' }, { "title": '按钮1' }]; |
28 |
| - log('JS calling native method "setButtons"', buttons); |
29 |
| - JSPage.callHandler('setButtons', {'callback': 'handleTapButtonAction', 'data': buttons}, function (result) { |
30 |
| - log('JS got native `setButtons` response', result); |
31 |
| - }); |
32 |
| - } |
33 |
| - |
34 |
| - function nslog(data) { |
35 |
| - JSBridge.callHandler('nslog', data, function (data) { |
36 |
| - log('JS got native `nslog` response', data); |
37 |
| - }); |
38 |
| - } |
39 |
| - |
40 |
| - function test(data) { |
41 |
| - if (testCallback) { |
42 |
| - log('calling callback', data); |
43 |
| - testCallback(data); |
44 |
| - } else { |
45 |
| - log('callback is undefined'); |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - JSBridge.callHandler('testNativeMethod', { foo: 'foo', bar: 'bar' }, function (data) { |
50 |
| - log('JS got native `testNativeMethod` response', data); |
51 |
| - }); |
52 |
| - |
53 |
| - JSBridge.registerHandler('log', function (data, callback) { |
54 |
| - var message = data; |
55 |
| - log('Native calling js method `log`', message); |
56 |
| - callback({ key: 'from js', value: data }); |
57 |
| - }); |
58 |
| - |
59 |
| - JSBridge.registerHandler('test', function(data, callback) { |
60 |
| - log('save callback'); |
61 |
| - testCallback = callback; |
62 |
| - }); |
63 |
| - |
64 |
| - JSPage.registerHandler('handleTapButtonAction', function(index) { |
65 |
| - var response = '点击了按钮' + index; |
66 |
| - log('Native calling js method `handleTapButtonAction`', response); |
67 |
| - }); |
68 |
| - |
69 |
| - </script> |
70 |
| -</head><body> |
71 |
| - <input type="button" value="Set Right Buttons" onclick="setRightButtons()" /> |
72 |
| - <input type="button" value="nslog" onclick="nslog({'foo': 'foo'})" /> |
73 |
| - <input type="button" value="test callback" onclick="test({'key': 'value'})" /> |
74 |
| - <div id='log'></div> |
75 |
| -</body></html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta http-equiv="Content-Type" content="text/html; charset=utf8"> |
| 6 | + <title>Demo</title> |
| 7 | + <style type='text/css'> |
| 8 | + html { |
| 9 | + font-family: Helvetica; |
| 10 | + color: #222; |
| 11 | + } |
| 12 | + |
| 13 | + h1 { |
| 14 | + color: steelblue; |
| 15 | + font-size: 24px; |
| 16 | + margin-top: 24px; |
| 17 | + } |
| 18 | + |
| 19 | + button { |
| 20 | + margin: 0 3px 10px; |
| 21 | + font-size: 12px; |
| 22 | + } |
| 23 | + |
| 24 | + .logLine { |
| 25 | + border-bottom: 1px solid #ccc; |
| 26 | + padding: 4px 2px; |
| 27 | + font-family: courier; |
| 28 | + font-size: 11px; |
| 29 | + } |
| 30 | + </style> |
| 31 | + <script language="javascript"> |
| 32 | + var uniqueId = 1; |
| 33 | + var testCallback; |
| 34 | + |
| 35 | + var JSBridge = window.Bridge; |
| 36 | + var JSPage = window.Page; |
| 37 | + |
| 38 | + function log(message, data) { |
| 39 | + var log = document.getElementById('log') |
| 40 | + var el = document.createElement('div') |
| 41 | + el.className = 'logLine' |
| 42 | + el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data) |
| 43 | + if (log.children.length) { log.insertBefore(el, log.children[0]) } |
| 44 | + else { log.appendChild(el) } |
| 45 | + } |
| 46 | + |
| 47 | + function setRightButtons() { |
| 48 | + var buttons = [{ "title": '按钮0' }, { "title": '按钮1' }]; |
| 49 | + log('JS calling native method "setButtons"', buttons); |
| 50 | + JSPage.callHandler('setButtons', { 'callback': 'handleTapButtonAction', 'data': buttons }, function (result) { |
| 51 | + log('JS got native `setButtons` response', result); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + function nslog(data) { |
| 56 | + JSBridge.callHandler('nslog', data, function (data) { |
| 57 | + log('JS got native `nslog` response', data); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + async function promise(data) { |
| 62 | + // JSBridge.callHandler('nslog', data).then( |
| 63 | + // result => log('JS got native `nslog` response', result), |
| 64 | + // error => log('JS got native `nslog` error', error) |
| 65 | + // ) |
| 66 | + |
| 67 | + var p = JSBridge.callHandler('nslog', data); |
| 68 | + var result = await p; |
| 69 | + log('JS got native `nslog` response', result); |
| 70 | + } |
| 71 | + |
| 72 | + function test(data) { |
| 73 | + if (testCallback) { |
| 74 | + log('calling callback', data); |
| 75 | + testCallback(data); |
| 76 | + } else { |
| 77 | + log('callback is undefined'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + JSBridge.callHandler('testNativeMethod', { foo: 'foo', bar: 'bar' }, function (data) { |
| 82 | + log('JS got native `testNativeMethod` response', data); |
| 83 | + }); |
| 84 | + |
| 85 | + JSBridge.registerHandler('log', function (data, callback) { |
| 86 | + var message = data; |
| 87 | + log('Native calling js method `log`', message); |
| 88 | + callback({ key: 'from js', value: data }); |
| 89 | + }); |
| 90 | + |
| 91 | + JSBridge.registerHandler('test', function (data, callback) { |
| 92 | + log('save callback'); |
| 93 | + testCallback = callback; |
| 94 | + }); |
| 95 | + |
| 96 | + JSPage.registerHandler('handleTapButtonAction', function (index) { |
| 97 | + var response = '点击了按钮' + index; |
| 98 | + log('Native calling js method `handleTapButtonAction`', response); |
| 99 | + }); |
| 100 | + </script> |
| 101 | +</head> |
| 102 | + |
| 103 | +<body> |
| 104 | + <input type="button" value="Set Right Buttons" onclick="setRightButtons()" /> |
| 105 | + <input type="button" value="nslog" onclick="nslog({'foo': 'foo'})" /> |
| 106 | + <input type="button" value="promise" onclick="promise({'bar': 'bar'})" /> |
| 107 | + <input type="button" value="test callback" onclick="test({'key': 'value'})" /> |
| 108 | + <div id='log'></div> |
| 109 | +</body> |
| 110 | + |
| 111 | +</html> |
0 commit comments