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.

Android `injectJavascript` must return `undefined` or it replaces the contents of the WebView

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment: OS: macOS Sierra 10.12.6 Node: 7.10.0 Yarn: Not Found npm: 4.5.0 Watchman: 4.7.0 Xcode: Xcode 9.1 Build version 9B55 Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed) react: 16.0.0-beta.5 => 16.0.0-beta.5 react-native: 0.49.3 => 0.49.3

Target Platform: Android (broken) and iOS (works)

Steps to Reproduce

  1. Create an app with a WebView. Add a ref for the WebView.
  2. Use `ref.injectJavaScript(‘x = “test”;’) to inject some JavaScript into the WebView.

Expected Behavior

The JavaScript is executed on the page, without replacing the page.

Actual Behavior

The result of the JavaScript expression replaces the contents of the page.

Reproducible Demo

https://snack.expo.io/Hke6dJFAW (EDIT: fixed URL)

Problem Code

I’m reasonably sure this line is the issue.

Elsewhere in that file, the injected JavaScript is wrapped in an IIFE so that the javascript: URL always evaluates to undefined.

Is the IIFE the complete solution? If so, I can submit a PR. Or are there other considerations (such as the changing scope) that need to be considered?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:9

github_iconTop GitHub Comments

8reactions
treypcommented, Feb 2, 2018

@ryan-granlund: sure, here’s an example of code i have working in both platforms:

this.webview.injectJavaScript(`
    (function () {
        document.getElementById('exampleTextInput').value = '${inputValue}';
    })();
`);

where this.webview is a ref to a webview (<WebView ref={(el) => {this.webview = el;}} source={{uri: webviewUrl}} />)

2reactions
react-native-botcommented, Feb 24, 2018

Thanks for posting this! It looks like you may not be using the latest version of React Native, v0.53.0, released on January 2018. Can you make sure this issue can still be reproduced in the latest version?

I am going to close this, but please feel free to open a new issue if you are able to confirm that this is still a problem in v0.53.0 or newer.

How to ContributeWhat to Expect from Maintainers

Read more comments on GitHub >

github_iconTop Results From Across the Web

injectedJavaScript is not working in Webview of react native
Well, you have more than one issue here. First one is your web view needs to be wrapped in a containing View component...
Read more >
WebView - Android Developers
Transportation object for returning WebView across thread boundaries. ... Whether the View's Outline should be used to clip the contents of the View....
Read more >
React native webview: The comprehensive guide you need to ...
React native webview is a component that lets your React Native app load webpages. Now, we will show you the comprehensive guide about...
Read more >
React Native WebView: A complete guide - LogRocket Blog
React Native WebView renders web content in a native view. ... Communicating between JavaScript and Native; The injectJavaScript method.
Read more >
react-native-webview - npm
React Native WebView component for iOS, Android, macOS, and Windows. Latest version: 11.26.0, last published: 19 days ago.
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