React Native Webview - injectJavascript 不工作
React Native Webview - injectJavascript not working
我正在尝试注入 JS 代码以在加载 React Native Webview 的内容之前提醒用户,但是,对我来说它只是转到 activity 指示器并加载 Webview 而没有注入任何 Javascript。我该如何解决?代码如下:
import React from 'react';
import { View, ActivityIndicator } from 'react-native';
import { WebView } from 'react-native-webview';
import styles from '../../styles';
export default function Links() {
function LoadingIndicatorView() {
return (
<View style={styles.hubLoading}>
<ActivityIndicator color='#009b88' size='large' />
</View>
)
}
const runFirst = `
setTimeout(function() { window.alert('hi') }, 2000);
true; // note: this is required, or you'll sometimes get silent failures
`
return <WebView source={{ uri: https://www.google.com/ }} renderLoading={LoadingIndicatorView} startInLoadingState={true} javaScriptEnabled={true} injectedJavaScript={runFirst} />;
}
顺便说一句,我是 运行 我 iOS 设备上的应用程序,如果它能帮助你回答的话。
您应该在 WebView
中包含 onMessage={(event) => {}}
https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md
An onMessage event is required as well to inject the JavaScript code into the WebView.
<WebView
source={{ uri: "https://www.google.com/" }}
renderLoading={LoadingIndicatorView}
startInLoadingState={true}
javaScriptEnabled={true}
injectedJavaScript={runFirst}
onMessage={(event) => {}}
/>
尝试
injectedJavaScriptBeforeContentLoaded={injectedJavascript}
我正在尝试注入 JS 代码以在加载 React Native Webview 的内容之前提醒用户,但是,对我来说它只是转到 activity 指示器并加载 Webview 而没有注入任何 Javascript。我该如何解决?代码如下:
import React from 'react';
import { View, ActivityIndicator } from 'react-native';
import { WebView } from 'react-native-webview';
import styles from '../../styles';
export default function Links() {
function LoadingIndicatorView() {
return (
<View style={styles.hubLoading}>
<ActivityIndicator color='#009b88' size='large' />
</View>
)
}
const runFirst = `
setTimeout(function() { window.alert('hi') }, 2000);
true; // note: this is required, or you'll sometimes get silent failures
`
return <WebView source={{ uri: https://www.google.com/ }} renderLoading={LoadingIndicatorView} startInLoadingState={true} javaScriptEnabled={true} injectedJavaScript={runFirst} />;
}
顺便说一句,我是 运行 我 iOS 设备上的应用程序,如果它能帮助你回答的话。
您应该在 WebView
onMessage={(event) => {}}
https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md
An onMessage event is required as well to inject the JavaScript code into the WebView.
<WebView
source={{ uri: "https://www.google.com/" }}
renderLoading={LoadingIndicatorView}
startInLoadingState={true}
javaScriptEnabled={true}
injectedJavaScript={runFirst}
onMessage={(event) => {}}
/>
尝试
injectedJavaScriptBeforeContentLoaded={injectedJavascript}