Skip to content

Commit

Permalink
feat: add debug script inject
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Dec 6, 2023
1 parent 3de7fe2 commit 4eedaf8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
26 changes: 26 additions & 0 deletions client/mobile/src/lib/inject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(';');
}
Expand Down
13 changes: 12 additions & 1 deletion client/mobile/src/lib/inject/message-handler.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,6 +17,11 @@ export function handleTailchatMessage(

if (type === 'init') {
webview.injectJavaScript(generateInstallPluginScript());

if (isDev()) {
webview.injectJavaScript(generateDebugScript());
}

return;
}

Expand All @@ -36,6 +43,10 @@ export function handleTailchatMessage(
return;
}

if (type === 'ensureWebRTCPermission') {
ensureWebRTCPermission();
}

if (type === 'bindWebsocket') {
const userInfo = payload.userInfo as UserBaseInfo;

Expand Down

0 comments on commit 4eedaf8

Please sign in to comment.