Skip to content

Commit aa537f5

Browse files
authored
Merge pull request #2261 from didi/fix-webview-error
解决调用bridge加载js异常时,有未捕获的异常被抛出
2 parents a5d01ec + 9761900 commit aa537f5

7 files changed

Lines changed: 87 additions & 9 deletions

File tree

examples/mpx-webview/H5/webviewbridge.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webview-bridge/dist/webviewbridge.esm.browser.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* mpxjs webview bridge v2.10.3
2+
* mpxjs webview bridge v2.10.6
33
* (c) 2025 @mpxjs team
44
* @license Apache
55
*/
@@ -44,6 +44,7 @@ function loadScript (url, { time = 5000, crossOrigin = false } = {}) {
4444
}
4545

4646
let sdkReady;
47+
const loadErrorCallbacks = [];
4748
const SDK_URL_MAP = Object.assign({
4849
wx: {
4950
url: 'https://res.wx.qq.com/open/js/jweixin-1.3.2.js'
@@ -120,6 +121,13 @@ if (systemUA.indexOf('AlipayClient') > -1 && systemUA.indexOf('MiniProgram') > -
120121

121122
const initWebviewBridge = () => {
122123
sdkReady = (env !== 'web' && env !== 'rn') ? SDK_URL_MAP[env].url ? loadScript(SDK_URL_MAP[env].url) : Promise.reject(new Error('未找到对应的sdk')) : Promise.resolve();
124+
sdkReady.catch((err = {}) => {
125+
loadErrorCallbacks.forEach((callback) => {
126+
if (typeof callback === 'function') {
127+
callback(err.message || err);
128+
}
129+
});
130+
});
123131
getWebviewApi();
124132
};
125133

@@ -146,6 +154,17 @@ const webviewBridge = {
146154
window.wx.config(config);
147155
}
148156
});
157+
},
158+
onLoadScriptError (callback) {
159+
if (loadErrorCallbacks.indexOf(callback) === -1) {
160+
loadErrorCallbacks.push(callback);
161+
}
162+
},
163+
offLoadScriptError (callback) {
164+
const index = loadErrorCallbacks.indexOf(callback);
165+
if (index > -1) {
166+
loadErrorCallbacks.splice(index, 1);
167+
}
149168
}
150169
};
151170

0 commit comments

Comments
 (0)