react-native-webview
中是否可以区分用户引起的导航和脚本引起的导航?
我看到在两种情况下都触发了onLoad[Start,End]
和onNavigationStateChange
事件。另外,如果我将日志添加到WebViewClient.shouldOverrideUrlLoading()
或WebViewClient.shouldInterceptRequest()
,则如果在脚本中更改了window.location
或用户单击了链接,则会同时调用两个fns。那么如何区分这两个呢?
谢谢!
编辑:必须澄清,我无法控制要加载的脚本及其在页面上的工作。
您可以在用户引起的每个window.location中传递查询参数isUserCause=true
在网站上,而onNavigationStateChange则可以检测到isUserCause参数。>
您可以这样传递网址
http://www.example.com/index.php?isUserCause=true
并且在onNavigationStateChange中,您可以通过QS pacakge解析查询参数
import qs from 'qs';
getQueryParams = (url) => {
var regex = /\?.+/g;
const result = url.match(regex);
if (result && result.length>0) {
const parsed = qs.parse(result[0], {ignoreQueryPrefix: true});
return parsed;
} else {
return {};
}
};
onNavigationStateChange = (navState) => {
const queryParams=this.getQueryParams(navState.url);
if(queryParams.isUserCause)
{
console.log("user-caused");
}
else
{
console.log("script-caused");
}
};