|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>JSBridge Test</title> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <meta name="viewport" |
| 7 | + content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> |
| 8 | + <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.12.4/jquery.js"></script> |
| 9 | + |
| 10 | + <style type="text/css"> |
| 11 | + .btn { |
| 12 | + background-color: #aaa; |
| 13 | + height: 40px; |
| 14 | + margin: 10px; |
| 15 | + } |
| 16 | + |
| 17 | + #response { |
| 18 | + background: #eeeeee; |
| 19 | + word-wrap: break-word; |
| 20 | + display: block; |
| 21 | + outline: 1px solid #ccc; |
| 22 | + padding: 5px; |
| 23 | + margin: 5px; |
| 24 | + } |
| 25 | + </style> |
| 26 | +</head> |
| 27 | +<body> |
| 28 | +<button class="btn" id="getOS">获取当前操作系统</button> |
| 29 | +<button class="btn" id="login">发起登录</button> |
| 30 | + |
| 31 | +<pre id="response"></pre> |
| 32 | + |
| 33 | +<script type="text/javascript"> |
| 34 | + /** |
| 35 | + * 初始化jsbridge |
| 36 | + * @param readyCallback 初始化完成后的回调 |
| 37 | + */ |
| 38 | + function initJsBridge(readyCallback) { |
| 39 | + var u = navigator.userAgent; |
| 40 | + var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端 |
| 41 | + var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 |
| 42 | + |
| 43 | + // 注册jsbridge |
| 44 | + function connectWebViewJavascriptBridge(callback) { |
| 45 | + if (isAndroid) { |
| 46 | + if (window.WebViewJavascriptBridge) { |
| 47 | + callback(WebViewJavascriptBridge) |
| 48 | + } else { |
| 49 | + document.addEventListener( |
| 50 | + 'WebViewJavascriptBridgeReady' |
| 51 | + , function () { |
| 52 | + callback(WebViewJavascriptBridge) |
| 53 | + }, |
| 54 | + false |
| 55 | + ); |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (isiOS) { |
| 61 | + if (window.WebViewJavascriptBridge) { |
| 62 | + return callback(WebViewJavascriptBridge); |
| 63 | + } |
| 64 | + if (window.WVJBCallbacks) { |
| 65 | + return window.WVJBCallbacks.push(callback); |
| 66 | + } |
| 67 | + window.WVJBCallbacks = [callback]; |
| 68 | + var WVJBIframe = document.createElement('iframe'); |
| 69 | + WVJBIframe.style.display = 'none'; |
| 70 | + WVJBIframe.src = 'https://__bridge_loaded__'; |
| 71 | + document.documentElement.appendChild(WVJBIframe); |
| 72 | + setTimeout(function () { |
| 73 | + document.documentElement.removeChild(WVJBIframe) |
| 74 | + }, 0) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // 调用注册方法 |
| 79 | + connectWebViewJavascriptBridge(function (bridge) { |
| 80 | + if (isAndroid) { |
| 81 | + bridge.init(function (message, responseCallback) { |
| 82 | + console.log('JS got a message', message); |
| 83 | + responseCallback(data); |
| 84 | + }); |
| 85 | + } |
| 86 | + readyCallback(); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * 显示响应信息 |
| 92 | + * @param response 响应信心 |
| 93 | + */ |
| 94 | + function showResponse(response) { |
| 95 | + $('#response').text(response); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * jQuery |
| 100 | + */ |
| 101 | + $(function () { |
| 102 | + // 首先调用JSBridge初始化代码,完成后再设置其他 |
| 103 | + initJsBridge(function () { |
| 104 | + $("#getOS").click(function () { |
| 105 | + // 通过JsBridge调用原生方法,写法固定,第一个参数时方法名,第二个参数时传入参数,第三个参数时响应回调 |
| 106 | + window.WebViewJavascriptBridge.callHandler('getOS', null, function (response) { |
| 107 | + showResponse(response); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + $('#login').click(function () { |
| 112 | + // 通过JsBridge调用原生方法,写法固定,第一个参数时方法名,第二个参数时传入参数,第三个参数时响应回调 |
| 113 | + window.WebViewJavascriptBridge.callHandler('login', { |
| 114 | + "account": "18000000000", |
| 115 | + "password": "123456" |
| 116 | + }, function (response) { |
| 117 | + showResponse(response); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }) |
| 121 | + }); |
| 122 | +</script> |
| 123 | +</body> |
| 124 | +</html> |
0 commit comments