简体   繁体   English

Webview 问题 Injecting javascript in React Native

[英]Webview issue Injecting javascript in React Native

I have this screen in a React Native using Expo app:我在使用 Expo 应用程序的 React Native 中有这个屏幕:

function DeviceVerification({navigation}) {
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
      flexDirection: 'column',
    }
  });

  const runFirst = `
    setTimeout(() => {
      window.sessionStorage.setItem('currentUser', ${userNameLogged});
      alert('Setting the values');
    }, 6000);
    window.localStorage.setItem('workgroup_code', ${authObj.workgroup_code});
    window.localStorage.setItem('accessor_id', ${authObj.mpv_device_id});
    window.document.cookie = ${loginCookie};
    true; // note: this is required, or you'll sometimes get silent failures
  `;

  return (
    <WebView 
      javaScriptEnabled={true}
      originWhitelist={['*']}
      style={styles.container}
      source={{ uri: 'htxxs://xxx.com/#/login/restriction/verify_device' }}
      sharedCookiesEnabled={true}
      injectedJavaScriptBeforeContentLoaded={runFirst}
      pullToRefreshEnabled={true}
    />
  );
}

I'm injecting some javascript as you can see injectedJavaScriptBeforeContentLoaded={runFirst} and the thing is that the localstorage part of the code works fine I can see the those being added correctly but the sessionStorage is not being set.我正在注入一些 javascript,如您所见, injectedJavaScriptBeforeContentLoaded={runFirst}并且代码的localstorage部分工作正常我可以看到那些被正确添加但sessionStorage没有被设置。

I never received an alert from this code:我从未收到来自此代码的警报:

setTimeout(() => {
          window.sessionStorage.setItem('currentUser', ${userNameLogged});
          alert('Setting the values');
        }, 6000);

I tried also window.sessionStorage.setItem('currentUser', ${userNameLogged});我也试过window.sessionStorage.setItem('currentUser', ${userNameLogged}); without the setTimeout and it does not creates the sessionStorage item.没有 setTimeout 并且它不会创建 sessionStorage 项目。

I will appreciate any help you can provide me.我将不胜感激您能为我提供的任何帮助。

@aleksandarZoric here is the evidence: @aleksandarZoric 这是证据:

价值测试

Try the following WebView props, notice the additional domStorageEnabled property.尝试以下 WebView 道具,注意额外的domStorageEnabled属性。

<WebView
ref={ref => (this.webview = ref)}
  originWhitelist={['*']}
  style={styles.container}
  source={{ uri: 'htxxs://xxx.com/#/login/restriction/verify_device' }}
  sharedCookiesEnabled={true}
  injectedJavaScriptBeforeContentLoaded={runFirst}
  pullToRefreshEnabled={true}
  javaScriptEnabled={true}
  domStorageEnabled={true}
/>

This is what I did to make it work这就是我所做的让它工作

const setSessionStorage = `(function() {
    window.sessionStorage.setItem('currentUser', '${userNameLogged}');
  })();`;

  handleWebViewNavigationStateChange = () => { 
    deviceVerificationWebView.injectJavaScript(setSessionStorage);
  }

  return (
    <WebView
      ref={ref => (deviceVerificationWebView = ref)}
      originWhitelist={['*']}
      style={styles.container}
      source={{ uri: 'htcps://xxx.com/#/login/restriction/verify_device' }}
      sharedCookiesEnabled={true}
      injectedJavaScriptBeforeContentLoaded={runFirst}
      javaScriptEnabled={true}
      pullToRefreshEnabled={true}
      domStorageEnabled={true}
      javaScriptCanOpenWindowsAutomatically={true}
      onNavigationStateChange={this.handleWebViewNavigationStateChange}
    />
  );

basically I need to use this listener onNavigationStateChange and then inject the code to change there the sessionStorage基本上我需要使用这个监听onNavigationStateChange然后注入代码来改变 sessionStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM