Advertisement
VichanecA

Untitled

Nov 30th, 2024 (edited)
18
0
168 days
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 1.28 KB | Source Code | 0 0
  1. import React, { useRef } from 'react';
  2. import { WebView } from 'react-native-webview';
  3. import axios from 'axios';
  4.  
  5. const MyWebComponent = () => {
  6.     const webViewRef = useRef(null);
  7.  
  8.     const handleMessage = (event) => {
  9.         const { data } = event.nativeEvent;
  10.         const parsedData = JSON.parse(data);
  11.  
  12.         if (parsedData.type === 'scrollPosition') {
  13.             saveScrollPosition(parsedData.position);
  14.         }
  15.     };
  16.  
  17.     const saveScrollPosition = async (position) => {
  18.         try {
  19.             await axios.post('https://your-api.com/save-position', { position });
  20.         } catch (error) {
  21.             console.error('Error saving position:', error);
  22.         }
  23.     };
  24.  
  25.     const injectedJavaScript = `
  26.         document.addEventListener('scroll', () => {
  27.             const scrollY = window.scrollY || document.documentElement.scrollTop;
  28.             window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'scrollPosition', position: scrollY }));
  29.         });
  30.         true;
  31.     `;
  32.  
  33.     return (
  34.         <WebView
  35.             ref={webViewRef}
  36.             source={{ uri: 'https://your-web-content.com' }}
  37.             onMessage={handleMessage}
  38.             injectedJavaScript={injectedJavaScript}
  39.         />
  40.     );
  41. };
  42.  
  43. export default MyWebComponent;
Advertisement
Comments
  • VichanecA
    11 days
    # text 0.25 KB | 0 0
    1. add a backend service to store the user's reading position in a db. Save the current loc when the user navigates away. When opening the ebk on a different dvc, fetch this saved loc and set it as the starting point, enabling seamless transitions across
Add Comment
Please, Sign In to add comment
Advertisement