question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variables and functions from injectedJavaScript not available on Android

See original GitHub issue

Bug description: On Android, variables and functions declared in injectedJavaScript are not available in code run by injectJavaScript, whereas on iOS they are. Please note that I’m using source={{ html: ... }}, not loading an external URI. I believe the behaviour on iOS is correct, that on Android is not.

To Reproduce: I created a repo that demonstrates this issue. I was careful to create a descriptive git history, so I hope it is pretty self-explanatory: https://github.com/svdo/VarIssue. This repo contains a complete react native project that you should be able to run yourself (make sure Android emulator is running):

yarn
yarn react-native start --reset-cache
yarn react-native run-ios
yarn react-native run-android

Environment:

  • OS: Android 9 and iOS 11
  • react-native-webview version: 5.8.1
  • react-native info:
    React Native Environment Info:
      System:
        OS: macOS 10.14.4
        CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
        Memory: 18.87 MB / 16.00 GB
        Shell: 5.3 - /bin/zsh
      Binaries:
        Node: 11.14.0 - /var/folders/0d/5_t6qk7s06qf94jnypzwh6yh0000gp/T/yarn--1557086445011-0.06815149805556375/node
        Yarn: 1.15.2 - /var/folders/0d/5_t6qk7s06qf94jnypzwh6yh0000gp/T/yarn--1557086445011-0.06815149805556375/yarn
        npm: 6.9.0 - ~/.homebrew/bin/npm
      SDKs:
        iOS SDK:
          Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
      IDEs:
        Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
      npmPackages:
        react: 16.8.3 => 16.8.3
        react-native: 0.59.5 => 0.59.5
    

Note

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

6reactions
its-wufucommented, Oct 14, 2019

I solved this by using the onLoad prop. I load the following function into onLoad:

  const injectJSFileFromWeb = () => {
    webViewRef.current.injectJavaScript(
      `var corescript = document.createElement('script');
        corescript.type = 'text/javascript';
        corescript.src = "https://url/to/code.js";
        var parent = document.getElementsByTagName('head').item(0);
        parent.appendChild(corescript);
        console.log('loaded...')`,
    );
  };

I red that you should care about the linebreaks.

Using this method I can access variables when calling the injectJavaScript method.

0reactions
ccfzcommented, Apr 15, 2020

This is still a problem on version: 9.1.4. I could not try 9.2.0 because of another known build error. Once the fix for 9.2.0 is released I try it as well. But for the moment the workaround that @svdo posted above worked for me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Webview - injectJavascript not working
Hi, how do i pass a value from my react native app to my webview. eg. i have a session id created when...
Read more >
Need help with react-native Webview, injecting ...
I figured out the answer. It's really nuanced. Essentially you have to create a global window variable before the injectedJavascript() method is ...
Read more >
Inject JS into Android's WebView. ...
Exposing JS function calls out of WebView. There is a way to bridge JavaScript methods called in WebView and your Java/Kotlin code.
Read more >
@sishuguojixuefu/react-native-echarts-wrapper NPM | npm.io
On the other hand it is not easy to make a two way data communication ... Variables and functions from injectedJavaScript not available...
Read more >
WebView
... if not specified. On Android, the only supported methods are GET and POST. ... Function that returns a view to show if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found