Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webviewbridge #1775

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -92,7 +92,8 @@ function showActionSheet (options = {}) {
}
return (
<TouchableHighlight underlayColor="rgba(0,0,0,0.6)" activeOpacity={1} onPress={cancelAction} style={styles.actionActionMask}>
<Animated.View style={[styles.actionSheetContent, animatedStyles]} >
{/* pointerEvents="none" 解决安卓下选项需要点两次被触发的问题 */}
<Animated.View style={[styles.actionSheetContent, animatedStyles]} pointerEvents="none">
{ alertText ? <View style={ styles.itemStyle }><Text style={[styles.itemTextStyle, { color: '#666666' }]}>{alertText}</Text></View> : null }
{ itemList.map((item, index) => <TouchableHighlight key={index} underlayColor="#ececec" onPress={() => selectAction(index)} style={ [styles.itemStyle, itemList.length -1 === index ? {
borderBottomWidth: 6,
Expand Down
67 changes: 44 additions & 23 deletions packages/webview-bridge/dist/webviewbridge.esm.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ let env = null;
let callbackId = 0;
const clientUid = getMpxWebViewId();
const callbacks = {};
const eventListener = (event) => {
// 接收web-view的回调
const data = event.data;
let msgData = data;
try {
if (typeof data === 'string') {
msgData = JSON.parse(data);
}
} 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];
}
};

// 环境判断逻辑
const systemUA = navigator.userAgent;
if (systemUA.indexOf('AlipayClient') > -1 && systemUA.indexOf('MiniProgram') > -1) {
Expand All @@ -85,32 +106,20 @@ if (systemUA.indexOf('AlipayClient') > -1 && systemUA.indexOf('MiniProgram') > -
env = 'swan';
} else if (systemUA.indexOf('toutiao') > -1) {
env = 'tt';
} if (window.ReactNativeWebView) {
env = 'rn';
if (systemUA.toLowerCase().indexOf('ios') > -1) {
window.addEventListener('message', eventListener, false);
} else {
document.addEventListener('message', eventListener, false); // 安卓机接收消息
}
} else {
env = 'web';
window.addEventListener('message', (event) => {
// 接收web-view的回调
const data = event.data;
let msgData = data;
try {
if (typeof data === 'string') {
msgData = JSON.parse(data);
}
} 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];
}
}, false);
window.addEventListener('message', eventListener, false);
}

const initWebviewBridge = () => {
sdkReady = env !== 'web' ? SDK_URL_MAP[env].url ? loadScript(SDK_URL_MAP[env].url) : Promise.reject(new Error('未找到对应的sdk')) : Promise.resolve();
sdkReady = (env !== 'web' && env !== 'rn') ? SDK_URL_MAP[env].url ? loadScript(SDK_URL_MAP[env].url) : Promise.reject(new Error('未找到对应的sdk')) : Promise.resolve();
getWebviewApi();
};

Expand Down Expand Up @@ -145,7 +154,7 @@ function postMessage (type, ...extraData) {
type = extraData[0];
extraData = extraData.slice(1);
}
const data = extraData[0];
const data = extraData[0] || {};
if (type !== 'getEnv') {
const currentCallbackId = ++callbackId;
callbacks[currentCallbackId] = (err, res) => {
Expand Down Expand Up @@ -311,6 +320,18 @@ const getWebviewApi = () => {
'getLocation',
'invoke'
],
rn: [
'navigateTo',
'navigateBack',
'switchTab',
'reLaunch',
'redirectTo',
'getEnv',
'postMessage',
'getLoadError',
'getLocation',
'invoke'
],
tt: []
};
const multiApi = multiApiMap[env] || {};
Expand All @@ -325,7 +346,7 @@ const getWebviewApi = () => {
});
singleApi.forEach((item) => {
webviewBridge[item] = (...args) => {
if (env === 'web') {
if (env === 'web' || env === 'rn') {
postMessage(item, ...args);
} else if (env === 'wx') {
runWebviewApiMethod(() => {
Expand Down
Loading
Loading