Skip to content

Commit

Permalink
Merge branch 'fix-drn' of github.com:didi/mpx into fix-drn
Browse files Browse the repository at this point in the history
  • Loading branch information
wenwenhua committed Dec 24, 2024
2 parents 8a0119c + a145bb2 commit 5770661
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 69 deletions.
2 changes: 1 addition & 1 deletion examples/mpx-webview/H5/webviewbridge.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
}
true;
`
const sendMessage = function(params: string) {
return `
window.mpxWebviewMessageCallback(${params})
true;
`
}
const _changeUrl = function (navState: WebViewNavigation) {
if (navState.navigationType) { // navigationType这个事件在页面开始加载时和页面加载完成时都会被触发所以判断这个避免其他无效触发执行该逻辑
currentPage.__webViewUrl = navState.url
Expand Down Expand Up @@ -182,21 +188,21 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr

asyncCallback && asyncCallback.then((res: any) => {
if (webViewRef.current?.postMessage) {
const test = JSON.stringify({
const result = JSON.stringify({
type,
callbackId: data.callbackId,
result: res
})
webViewRef.current.postMessage(test)
webViewRef.current.injectJavaScript(sendMessage(result))
}
}).catch((error: any) => {
if (webViewRef.current?.postMessage) {
const test = JSON.stringify({
const result = JSON.stringify({
type,
callbackId: data.callbackId,
error
})
webViewRef.current.postMessage(test)
webViewRef.current.injectJavaScript(sendMessage(result))
}
})
}
Expand Down
29 changes: 15 additions & 14 deletions packages/webview-bridge/dist/webviewbridge.esm.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ let env = null;
let callbackId = 0;
const clientUid = getMpxWebViewId();
const callbacks = {};

const runCallback = (msgData) => {
const { callbackId, error, result } = msgData;
if (callbackId !== undefined && callbacks[callbackId]) {
if (error) {
callbacks[callbackId](error);
} else {
callbacks[callbackId](null, result);
}
delete callbacks[callbackId];
}
};

const eventListener = (event) => {
// 接收web-view的回调
const data = event.data;
Expand All @@ -85,15 +98,7 @@ const eventListener = (event) => {
}
} catch (e) {
}
const { callbackId, error, result } = msgData;
if (callbackId !== undefined && callbacks[callbackId]) {
if (error) {
callbacks[callbackId](error);
} else {
callbacks[callbackId](null, result);
}
delete callbacks[callbackId];
}
runCallback(msgData);
};

// 环境判断逻辑
Expand All @@ -108,11 +113,7 @@ if (systemUA.indexOf('AlipayClient') > -1 && systemUA.indexOf('MiniProgram') > -
env = 'tt';
} if (window.ReactNativeWebView) {
env = 'rn';
if (systemUA.toLowerCase().indexOf('ios') > -1) {
window.addEventListener('message', eventListener, false);
} else {
document.addEventListener('message', eventListener, false); // 安卓机接收消息
}
window.mpxWebviewMessageCallback = runCallback;
} else {
env = 'web';
window.addEventListener('message', eventListener, false);
Expand Down
Loading

0 comments on commit 5770661

Please sign in to comment.