Closed helinjiang closed 4 years ago
使用 location.href
的场景下,可以监听 will-navigate
事件,在第二个参数即为跳转的地址, 详见 https://github.com/electron/electron/blob/v2.0.18/docs/api/web-contents.md#event-will-navigate
使用 location.href
之后,会有以下事件
will-navigate {} tnow://callByLocaiton
did-start-loading {}
did-stop-loading {}
使用 iframe 场景下,有以下事件:
did-start-loading {}
did-frame-finish-load {} false
did-stop-loading {}
did-start-loading {}
did-stop-loading {}
通过注入特定代码来监听iframe的创建和消除
var observe = new MutationObserver(function (mutations, observer) {
console.log('===MutationObserver====');
console.log('===mutations====', mutations);
console.log('===observer====', observer);
try {
// iframe
// tnow://callByIframe
console.log(mutations[0].removedNodes[0].src);
} catch (e) {
}
});
var el = document.querySelector('body');
var options = {
'childList': true,
'attributes': true
};
observe.observe(el, options);
注意,在 electron
v2.0.18
中没法获得addedNodes
,但在 8.3.0 以上是可以的
使用 DEBUG=nightmare* node xx.js
方式打印可以发现规律,可以使用 did-fail-provisional-load 事件来监听
使用 ifame
nightmare:log did-start-loading [{}] +22s
nightmare:log did-frame-finish-load [{},false] +2ms
nightmare:log did-stop-loading [{}] +0ms
nightmare:log did-start-loading [{}] +3ms
nightmare:log did-fail-provisional-load [{},-3,"ERR_ABORTED","tnow://callByIframe",false] +1ms
nightmare:log did-stop-loading [{}] +0ms
使用 location
nightmare:log will-navigate [{},"tnow://callByLocaiton"] +1m
nightmare:log did-start-loading [{}] +1ms
nightmare:log did-fail-provisional-load [{},-3,"ERR_ABORTED","tnow://callByLocaiton",true] +55ms
nightmare:log did-stop-loading [{}] +0ms
其中,上述的错误码 `-3` 含义可以参考 https://source.chromium.org/chromium/chromium/src/+/master:net/base/net_error_list.h;l=1?q=net_error_li&ss=chromium%2Fchromium%2Fsrc&originalUrl=https:%2F%2Fcs.chromium.org%2F
已支持,详见
matmanResult.isExistJSBridge('tnow://callByLocation')
升级 matman 到 5.0.7
及以上版本即可
在 Hybrid 应用中,经常会有调用 jsbridge,需要有场景支持
测试代码如下: