// adapter.js —— 壳层适配:同一套聊天前端跑在 Electron / Tauri / 浏览器(mock) 三种宿主 // 统一暴露 window.GHB = { send(to, text), on(event, cb) } // 事件:connected{node_id} / message{from,text,msg_id} / sent{msg_id,to} / receipt{msg_id,verified} / error{message} (() => { const handlers = {}; const on = (ev, cb) => { (handlers[ev] = handlers[ev] || []).push(cb); }; const dispatch = (ev, payload) => (handlers[ev] || []).forEach((cb) => cb(payload)); if (window.ghbElectron) { // Electron:preload 已注入 ipc 通道 window.ghbElectron.onEvent((ev) => dispatch(ev.event === 'message' ? 'message' : ev.event, ev)); window.GHB = { kind: 'electron', on, send: (to, text) => window.ghbElectron.send(to, text), }; } else if (window.__TAURI__ && window.__TAURI__.event && window.__TAURI__.core) { // Tauri v2(withGlobalTauri):listen('ghb:event') + invoke('bridge_send') window.__TAURI__.event.listen('ghb:event', (e) => dispatch(e.payload.event, e.payload)); window.GHB = { kind: 'tauri', on, send: (to, text) => window.__TAURI__.core.invoke('bridge_send', { to, text }), }; } else { // 浏览器直开(无壳):只提示,不阻塞渲染 window.GHB = { kind: 'mock', on, send: () => { dispatch('error', { message: '未在壳层中运行:请通过 Electron / Tauri 打开' }); }, }; dispatch('error', { message: '桥接未接入(裸浏览器模式)' }); } })();