diff --git a/client/mobile/src/lib/inject/index.ts b/client/mobile/src/lib/inject/index.ts index 2a508774ff8..6994363a28b 100644 --- a/client/mobile/src/lib/inject/index.ts +++ b/client/mobile/src/lib/inject/index.ts @@ -27,6 +27,32 @@ export function generateInstallPluginScript() { return raw; } +export function generateDebugScript() { + const inner = `function main() { + console.error = (...args) => { + window.ReactNativeWebView.postMessage( + JSON.stringify({ + _isTailchat: true, + type: 'console:error', + payload: args, + }) + ); + }; + console.log = (...args) => { + window.ReactNativeWebView.postMessage( + JSON.stringify({ + _isTailchat: true, + type: 'console:log', + payload: JSON.stringify(args), + }) + ); + }; + }`; + + const raw = `(${inner})()`; + return raw; +} + export function generateInjectedScript(): string { return [generateDeviceInfo(), generatePostMessageScript()].join(';'); } diff --git a/client/mobile/src/lib/inject/message-handler.ts b/client/mobile/src/lib/inject/message-handler.ts index d0fcb4eaad9..5103f3a4ec4 100644 --- a/client/mobile/src/lib/inject/message-handler.ts +++ b/client/mobile/src/lib/inject/message-handler.ts @@ -1,10 +1,12 @@ import type WebView from 'react-native-webview'; -import { generateInstallPluginScript } from '.'; +import { generateDebugScript, generateInstallPluginScript } from '.'; import { useUIStore } from '../../store/ui'; import type { UserBaseInfo } from '../../types'; import { initNotificationEnv, showNotification } from '../notifications'; // import { bindSocketEvent, createSocket } from '../socket'; import { AppState } from 'react-native'; +import { ensureWebRTCPermission } from '../permissions'; +import { isDev } from '../utils'; export function handleTailchatMessage( type: string, @@ -15,6 +17,11 @@ export function handleTailchatMessage( if (type === 'init') { webview.injectJavaScript(generateInstallPluginScript()); + + if (isDev()) { + webview.injectJavaScript(generateDebugScript()); + } + return; } @@ -36,6 +43,10 @@ export function handleTailchatMessage( return; } + if (type === 'ensureWebRTCPermission') { + ensureWebRTCPermission(); + } + if (type === 'bindWebsocket') { const userInfo = payload.userInfo as UserBaseInfo;