test_jsbridge.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NOMO JS Bridge 测试</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  15. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  16. color: #333;
  17. /* 使用原生属性设置安全边距 */
  18. padding-top: calc(var(--status-bar-height, 0px) + 10px);
  19. padding-bottom: calc(var(--bottom-bar-height, 0px) + 10px);
  20. min-height: 100vh;
  21. }
  22. .container {
  23. max-width: 600px;
  24. margin: 0 auto;
  25. padding: 15px;
  26. }
  27. .header {
  28. background: white;
  29. border-radius: 15px;
  30. padding: 20px;
  31. margin-bottom: 15px;
  32. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  33. }
  34. .header h1 {
  35. font-size: 24px;
  36. margin-bottom: 15px;
  37. color: #667eea;
  38. }
  39. .info-grid {
  40. display: grid;
  41. grid-template-columns: 1fr 1fr;
  42. gap: 10px;
  43. font-size: 13px;
  44. }
  45. .info-item {
  46. background: #f8f9fa;
  47. padding: 10px;
  48. border-radius: 8px;
  49. }
  50. .info-label {
  51. color: #666;
  52. font-size: 11px;
  53. margin-bottom: 5px;
  54. }
  55. .info-value {
  56. color: #333;
  57. font-weight: 600;
  58. word-break: break-all;
  59. }
  60. .card {
  61. background: white;
  62. border-radius: 15px;
  63. padding: 20px;
  64. margin-bottom: 15px;
  65. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  66. }
  67. .card h2 {
  68. font-size: 18px;
  69. margin-bottom: 15px;
  70. color: #667eea;
  71. }
  72. .vpn-status {
  73. display: flex;
  74. justify-content: space-between;
  75. align-items: center;
  76. padding: 15px;
  77. background: #f8f9fa;
  78. border-radius: 10px;
  79. margin-bottom: 15px;
  80. }
  81. .status-indicator {
  82. display: flex;
  83. align-items: center;
  84. gap: 10px;
  85. }
  86. .status-dot {
  87. width: 12px;
  88. height: 12px;
  89. border-radius: 50%;
  90. background-color: #ccc;
  91. animation: pulse 2s infinite;
  92. }
  93. .status-dot.connected {
  94. background-color: #10b981;
  95. }
  96. .status-dot.connecting {
  97. background-color: #f59e0b;
  98. }
  99. .status-dot.disconnected {
  100. background-color: #ef4444;
  101. }
  102. @keyframes pulse {
  103. 0%, 100% { opacity: 1; }
  104. 50% { opacity: 0.5; }
  105. }
  106. .btn-grid {
  107. display: grid;
  108. grid-template-columns: 1fr 1fr;
  109. gap: 10px;
  110. }
  111. .btn {
  112. padding: 12px 20px;
  113. border: none;
  114. border-radius: 10px;
  115. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  116. color: white;
  117. font-size: 14px;
  118. font-weight: 600;
  119. cursor: pointer;
  120. transition: transform 0.2s, box-shadow 0.2s;
  121. }
  122. .btn:active {
  123. transform: scale(0.95);
  124. }
  125. .btn-full {
  126. grid-column: 1 / -1;
  127. }
  128. .btn-danger {
  129. background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  130. }
  131. .btn-success {
  132. background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  133. }
  134. .log {
  135. background: #1e293b;
  136. color: #94a3b8;
  137. border-radius: 10px;
  138. padding: 15px;
  139. max-height: 300px;
  140. overflow-y: auto;
  141. font-size: 12px;
  142. font-family: 'Courier New', monospace;
  143. }
  144. .log-item {
  145. margin-bottom: 8px;
  146. line-height: 1.5;
  147. }
  148. .log-time {
  149. color: #64748b;
  150. }
  151. .log-success {
  152. color: #10b981;
  153. }
  154. .log-error {
  155. color: #ef4444;
  156. }
  157. .log-info {
  158. color: #3b82f6;
  159. }
  160. </style>
  161. </head>
  162. <body>
  163. <div class="container">
  164. <!-- 设备信息 -->
  165. <div class="header">
  166. <h1>🚀 NOMO JS Bridge</h1>
  167. <div class="info-grid">
  168. <div class="info-item">
  169. <div class="info-label">状态栏高度</div>
  170. <div class="info-value" id="statusBarHeight">-</div>
  171. </div>
  172. <div class="info-item">
  173. <div class="info-label">底部安全区</div>
  174. <div class="info-value" id="bottomBarHeight">-</div>
  175. </div>
  176. <div class="info-item">
  177. <div class="info-label">平台</div>
  178. <div class="info-value" id="platform">-</div>
  179. </div>
  180. <div class="info-item">
  181. <div class="info-label">版本</div>
  182. <div class="info-value" id="version">-</div>
  183. </div>
  184. </div>
  185. </div>
  186. <!-- VPN 控制 -->
  187. <div class="card">
  188. <h2>🔐 VPN 控制</h2>
  189. <div class="vpn-status">
  190. <div class="status-indicator">
  191. <div class="status-dot" id="statusDot"></div>
  192. <div>
  193. <div style="font-weight: 600;" id="vpnStatus">未知</div>
  194. <div style="font-size: 12px; color: #666;" id="vpnTimer">00:00:00</div>
  195. </div>
  196. </div>
  197. </div>
  198. <div class="btn-grid">
  199. <button class="btn btn-success" onclick="handleConnectVPN()">
  200. ⚡ 连接
  201. </button>
  202. <button class="btn btn-danger" onclick="handleDisconnectVPN()">
  203. ⏸️ 断开
  204. </button>
  205. <button class="btn btn-full" onclick="handleGetVPNStatus()">
  206. 🔍 查询状态
  207. </button>
  208. </div>
  209. </div>
  210. <!-- 其他功能 -->
  211. <div class="card">
  212. <h2>🛠️ 其他功能</h2>
  213. <div class="btn-grid">
  214. <button class="btn" onclick="handleSetStatusBarColor()">
  215. 🎨 切换状态栏
  216. </button>
  217. <button class="btn" onclick="handleOpenApp()">
  218. 📱 打开微信
  219. </button>
  220. <button class="btn btn-danger btn-full" onclick="handleExit()">
  221. ❌ 退出 WebView
  222. </button>
  223. </div>
  224. </div>
  225. <!-- 日志 -->
  226. <div class="card">
  227. <h2>📋 日志</h2>
  228. <div class="log" id="logContainer"></div>
  229. </div>
  230. </div>
  231. <script>
  232. let logCount = 0;
  233. const maxLogs = 50;
  234. // 日志函数
  235. function addLog(message, type = 'info') {
  236. const logContainer = document.getElementById('logContainer');
  237. const time = new Date().toLocaleTimeString('zh-CN', { hour12: false });
  238. const logClass = `log-${type}`;
  239. const logItem = document.createElement('div');
  240. logItem.className = 'log-item';
  241. logItem.innerHTML = `<span class="log-time">[${time}]</span> <span class="${logClass}">${message}</span>`;
  242. logContainer.insertBefore(logItem, logContainer.firstChild);
  243. logCount++;
  244. if (logCount > maxLogs) {
  245. logContainer.removeChild(logContainer.lastChild);
  246. logCount--;
  247. }
  248. }
  249. // 更新 VPN 状态显示
  250. function updateVPNStatusUI(status) {
  251. const statusDot = document.getElementById('statusDot');
  252. const statusText = document.getElementById('vpnStatus');
  253. statusDot.className = 'status-dot ' + status;
  254. const statusMap = {
  255. 'connected': '已连接',
  256. 'connecting': '连接中',
  257. 'disconnected': '未连接',
  258. 'unknown': '未知'
  259. };
  260. statusText.textContent = statusMap[status] || status;
  261. }
  262. // 页面加载完成后初始化
  263. window.onload = function() {
  264. addLog('页面加载完成,等待原生注入...', 'info');
  265. setTimeout(initNativeAttrs, 200);
  266. };
  267. // 初始化原生属性
  268. function initNativeAttrs() {
  269. if (window.nativeAttrs) {
  270. document.getElementById('statusBarHeight').textContent = window.nativeAttrs.statusBarHeight + 'px';
  271. document.getElementById('bottomBarHeight').textContent = window.nativeAttrs.bottomBarHeight + 'px';
  272. document.getElementById('platform').textContent = window.nativeAttrs.platform;
  273. document.getElementById('version').textContent = window.nativeAttrs.version;
  274. // 设置 CSS 变量
  275. document.documentElement.style.setProperty('--status-bar-height', window.nativeAttrs.statusBarHeight + 'px');
  276. document.documentElement.style.setProperty('--bottom-bar-height', window.nativeAttrs.bottomBarHeight + 'px');
  277. addLog('✅ 原生属性注入成功', 'success');
  278. addLog(`📱 平台: ${window.nativeAttrs.platform}, 版本: ${window.nativeAttrs.version}`, 'info');
  279. // 初始化 VPN 状态
  280. handleGetVPNStatus();
  281. } else {
  282. addLog('❌ 原生属性注入失败', 'error');
  283. }
  284. }
  285. // 设置 VPN 状态回调
  286. window.nativeCallbacks = {
  287. onVPNStatusChanged: function(status) {
  288. addLog(`📡 VPN 状态变化: ${status}`, 'info');
  289. updateVPNStatusUI(status);
  290. },
  291. onVPNConnected: function() {
  292. addLog('✅ VPN 连接成功!', 'success');
  293. updateVPNStatusUI('connected');
  294. },
  295. onVPNConnecting: function() {
  296. addLog('⏳ VPN 连接中...', 'info');
  297. updateVPNStatusUI('connecting');
  298. },
  299. onVPNDisconnected: function() {
  300. addLog('⏸️ VPN 已断开', 'info');
  301. updateVPNStatusUI('disconnected');
  302. }
  303. };
  304. // 功能函数
  305. async function handleConnectVPN() {
  306. try {
  307. addLog('🚀 正在连接 VPN...', 'info');
  308. const result = await window.native.connectVPN(26, 'ca');
  309. if (result.success) {
  310. addLog('✅ VPN 连接请求已发送', 'success');
  311. } else {
  312. addLog(`❌ 连接失败: ${result.error}`, 'error');
  313. }
  314. } catch (error) {
  315. addLog(`❌ 发生错误: ${error}`, 'error');
  316. }
  317. }
  318. async function handleDisconnectVPN() {
  319. try {
  320. addLog('⏸️ 正在断开 VPN...', 'info');
  321. const result = await window.native.disconnectVPN();
  322. if (result.success) {
  323. addLog('✅ VPN 断开请求已发送', 'success');
  324. } else {
  325. addLog(`❌ 断开失败: ${result.error}`, 'error');
  326. }
  327. } catch (error) {
  328. addLog(`❌ 发生错误: ${error}`, 'error');
  329. }
  330. }
  331. async function handleGetVPNStatus() {
  332. try {
  333. const result = await window.native.getVPNStatus();
  334. if (result.success) {
  335. const status = result.data.status;
  336. const timer = result.data.timer;
  337. document.getElementById('vpnTimer').textContent = timer;
  338. updateVPNStatusUI(status);
  339. addLog(`📊 VPN 状态: ${status}, 时长: ${timer}`, 'info');
  340. } else {
  341. addLog(`❌ 查询失败: ${result.error}`, 'error');
  342. }
  343. } catch (error) {
  344. addLog(`❌ 发生错误: ${error}`, 'error');
  345. }
  346. }
  347. async function handleSetStatusBarColor() {
  348. // 只支持 dark 和 light 两种模式
  349. const currentMode = document.body.dataset.statusBarMode || 'light';
  350. const newMode = currentMode === 'dark' ? 'light' : 'dark';
  351. try {
  352. addLog(`🎨 切换状态栏模式: ${currentMode} -> ${newMode}`, 'info');
  353. const result = await window.native.setStatusBarColor(newMode);
  354. if (result.success) {
  355. document.body.dataset.statusBarMode = newMode;
  356. addLog(`✅ 状态栏模式切换成功: ${newMode} (${newMode === 'dark' ? '白色图标' : '黑色图标'})`, 'success');
  357. } else {
  358. addLog(`❌ 设置失败: ${result.error}`, 'error');
  359. }
  360. } catch (error) {
  361. addLog(`❌ 发生错误: ${error}`, 'error');
  362. }
  363. }
  364. async function handleOpenApp() {
  365. try {
  366. addLog('📱 正在打开微信...', 'info');
  367. const result = await window.native.openApp('com.tencent.mm', 'weixin://');
  368. if (result.success) {
  369. addLog('✅ 应用已打开', 'success');
  370. } else {
  371. addLog(`❌ 打开失败: ${result.error}`, 'error');
  372. }
  373. } catch (error) {
  374. addLog(`❌ 发生错误: ${error}`, 'error');
  375. }
  376. }
  377. async function handleExit() {
  378. try {
  379. addLog('👋 正在退出 WebView...', 'info');
  380. await window.native.exit();
  381. } catch (error) {
  382. addLog(`❌ 退出失败: ${error}`, 'error');
  383. }
  384. }
  385. // 监听 VPN 状态变化事件
  386. window.addEventListener('vpnStatusChanged', function(event) {
  387. console.log('收到 VPN 状态变化事件:', event.detail.status);
  388. });
  389. // 初始日志
  390. addLog('🎉 欢迎使用 NOMO JS Bridge 测试页面', 'success');
  391. </script>
  392. </body>
  393. </html>