Skip to content

Commit 0753dd5

Browse files
committed
Fix upvote WebView JS injection: use injectJavaScript() on onLoadEnd instead of always-running injectedJavaScript prop to prevent infinite redirect loop
1 parent 282d137 commit 0753dd5

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

screens/WebViewScreen.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { WebView } from 'react-native-webview';
77
import * as Application from 'expo-application';
88

99
import useStore from '../hooks/useStore';
10+
import useTheme from '../hooks/useTheme';
1011

1112
export default function WebViewScreen() {
1213
const { url: paramUrl, jsKey } = useLocalSearchParams();
@@ -17,11 +18,15 @@ export default function WebViewScreen() {
1718

1819
const [navState, setNavState] = useState({});
1920
const webViewRef = useRef(null);
21+
const hasInjected = useRef(false);
22+
23+
const { colors } = useTheme();
2024

2125
return (
2226
<>
2327
<WebView
2428
ref={webViewRef}
29+
style={{ flex: 1, backgroundColor: colors.background }}
2530
applicationNameForUserAgent={`${Application.applicationName}/${Application.nativeApplicationVersion}`}
2631
source={{ uri: url }}
2732
originWhitelist={['http://*', 'https://*', 'data:*', 'about:*']}
@@ -31,8 +36,18 @@ export default function WebViewScreen() {
3136
onNavigationStateChange={(navState) => {
3237
setNavState(navState);
3338
}}
39+
onLoadEnd={() => {
40+
if (
41+
jsKey &&
42+
injectedJavaScript &&
43+
!hasInjected.current &&
44+
navState.url?.includes('item?id=')
45+
) {
46+
webViewRef.current?.injectJavaScript(injectedJavaScript);
47+
hasInjected.current = true;
48+
}
49+
}}
3450
onMessage={() => {}}
35-
injectedJavaScript={injectedJavaScript}
3651
/>
3752
<Stack.Toolbar placement="right">
3853
<Stack.Toolbar.Menu icon="ellipsis">

0 commit comments

Comments
 (0)