Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebView on Android 4.3 fails to execute any injected Javascript #21726

Closed
spaceye opened this issue Oct 11, 2018 · 2 comments
Closed

WebView on Android 4.3 fails to execute any injected Javascript #21726

spaceye opened this issue Oct 11, 2018 · 2 comments
Labels
Component: WebView Related to the WebView component. Platform: Android Android applications. Resolution: Locked This issue was locked by the bot.

Comments

@spaceye
Copy link

spaceye commented Oct 11, 2018

Environment

React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Memory: 26.41 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.7.0 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.2.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
Android SDK:
Build Tools: 23.0.1, 26.0.1, 27.0.3
API Levels: 23, 26, 27
IDEs:
Android Studio: 3.1 AI-173.4819257
Xcode: 10.0/10A255 - /usr/bin/xcodebuild
npmPackages:
@lingui/react: 2.7.0 => 2.7.0
react: 16.5.0 => 16.5.0
react-native: 0.57.0 => 0.57.0
npmGlobalPackages:
react-native-cli: 2.0.1

Description

Recent changes broke execution of any injected Javascript (either via injectedJavaScript prop or manually via injectJavaScript method of the WebView on devices running on Android prior to 4.4.

Looking through the linked issues of the above mentioned commit, say this one, javascript scheme RFC and Android implementation prior to 4.4 (see issue) we can see that the following action is the culprit:

loadUrl("javascript:" + URLEncoder.encode(script, "UTF-8"));

This line will encode passed Javascript code for Android <= 4.3 and due to the latter's implementation of loadUrl that doesn't decode the scheme's text we end up with a problem.

In our application we need to grab loaded page's title and set it into our header in RN. Prior to 0.57.0 and the commit it worked without any issues.

As any injected Javascript is wrapped into self-executing anonymous function here:

evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");

This is what goes to loadUrl:

%28function%28%29+%7B%0Awindow.postMessage%28document.title%29%3B%0A%7D%29%28%29%3B

Reproducible Demo

Setup your WebView with any correct value for injectedJavascript prop that would provide a feedback (console.log, data passed via onPostMessage, visible DOM manipulations, etc) on <=4.3 Android device / emulator.

So I suggest we remove the encoding bit from that line as it clearly messes up any passed Javascript.

@react-native-bot react-native-bot added Platform: Android Android applications. Component: WebView Related to the WebView component. 🔶Components labels Oct 11, 2018
@Titozzz
Copy link
Collaborator

Titozzz commented Oct 22, 2018

React-native Webview component is being extracted to https://github.com/react-native-community/react-native-webview. Please if that bug still exists with the extracted component, open an issue on the other repository, as it will probably be easier to get attention there.

Thank you !

@Titozzz Titozzz closed this as completed Oct 22, 2018
@spaceye
Copy link
Author

spaceye commented Oct 23, 2018

@Titozzz , thanks, I've done just that. The error causing code was carried over into this new repo without changes so the bug persists.

@facebook facebook locked as resolved and limited conversation to collaborators Oct 22, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Component: WebView Related to the WebView component. Platform: Android Android applications. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants