import { Ionicons } from '@expo/vector-icons' import { Block, Text } from '@share/components' import { router, useLocalSearchParams } from 'expo-router' import { useRef, useState } from 'react' import { ActivityIndicator, Alert, Share } from 'react-native' import { useSafeAreaInsets } from 'react-native-safe-area-context' import { WebView, type WebViewNavigation } from 'react-native-webview' export default function WebViewPage() { const { url, title } = useLocalSearchParams<{ url: string title?: string }>() const insets = useSafeAreaInsets() const webViewRef = useRef(null) const [loading, setLoading] = useState(true) const [canGoBack, setCanGoBack] = useState(false) const [canGoForward, setCanGoForward] = useState(false) const [currentUrl, setCurrentUrl] = useState(url || '') const [progress, setProgress] = useState(0) if (!url) { return ( URL参数缺失 router.back()}> 返回 ) } const handleNavigationStateChange = (navState: WebViewNavigation) => { setCanGoBack(navState.canGoBack) setCanGoForward(navState.canGoForward) setCurrentUrl(navState.url) } const handleError = () => { Alert.alert('加载错误', '页面加载失败,请检查网络连接', [ { text: '重试', onPress: () => reload() }, { text: '返回', onPress: () => router.back() }, ]) } const handleLoadProgress = ({ nativeEvent }: any) => { setProgress(nativeEvent.progress) } const goBack = () => { if (canGoBack) { webViewRef.current?.goBack() } else { router.back() } } const goForward = () => { webViewRef.current?.goForward() } const reload = () => { webViewRef.current?.reload() } const shareUrl = async () => { try { await Share.share({ message: currentUrl, title: title || '分享网页', }) } catch (error) { console.error('分享失败:', error) } } // 注入的JavaScript代码 const injectedJavaScript = ` // 禁用长按选择 document.addEventListener('contextmenu', function(e) { e.preventDefault(); }); // 添加样式优化 const style = document.createElement('style'); style.innerHTML = \` body { -webkit-user-select: none; -webkit-touch-callout: none; } * { -webkit-tap-highlight-color: transparent; } \`; document.head.appendChild(style); // 向RN发送页面信息 window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'pageInfo', title: document.title, url: window.location.href })); true; // 必须返回true ` const handleMessage = (event: any) => { try { const data = JSON.parse(event.nativeEvent.data) if (data.type === 'pageInfo') { console.log('页面信息:', data) // 可以根据页面信息执行相应操作 } } catch (error) { console.log('收到消息:', event.nativeEvent.data) } } return ( {/* Header */} {title || '网页浏览'} {currentUrl} {/* Progress Bar */} {loading && progress < 1 && ( )} {/* Loading Indicator */} {loading && ( )} {/* WebView */} setLoading(false)} onLoadStart={() => setLoading(true)} onLoadEnd={() => setLoading(false)} onLoadProgress={handleLoadProgress} onError={handleError} onNavigationStateChange={handleNavigationStateChange} onMessage={handleMessage} // 功能配置 allowsBackForwardNavigationGestures={true} startInLoadingState={true} mixedContentMode="compatibility" javaScriptEnabled={true} domStorageEnabled={true} allowsInlineMediaPlayback={true} mediaPlaybackRequiresUserAction={false} // 注入JavaScript injectedJavaScript={injectedJavaScript} // 样式配置 bounces={false} scrollEnabled={true} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={true} // 用户代理 userAgent="Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1 DuoomApp/1.0" // 其他配置 cacheEnabled={true} thirdPartyCookiesEnabled={true} sharedCookiesEnabled={true} /> ) }