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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying textZoom prop disables injectedJavaScriptObject prop functionality on Android #3393

Open
oleg-am opened this issue Apr 10, 2024 · 0 comments

Comments

@oleg-am
Copy link

oleg-am commented Apr 10, 2024

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

I'm using react-native-webview@13.8.4 and found an issue with the injectedJavaScriptObject prop when used together with the textZoom prop. If both props are set, injectedJavaScriptObject stops working. But, when textZoom is not used, injectedJavaScriptObject works just fine.

 <WebView
      source={{
        uri: url,
      }}
      onNavigationStateChange={handleWebViewNavigationStateChange}
      onMessage={onMessage}
      textZoom={100}
      injectedJavaScript={injectedJavaScript} // works
      injectedJavaScriptObject={{ key: 'value' }} // not working with textZoom prop
    />

On the website side, I'm getting undefined as a result when calling window.ReactNativeWebView.injectedObjectJson().

I started looking into the library's code and found out that when calling the setInjectedJavaScriptObject method, the expression getSettings().getJavaScriptEnabled() returns false if we pass textZoom prop. That's why the logic does not execute. However, if we don't pass textZoom, it returns true and everything works as expected.

  // android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
  public void setInjectedJavaScriptObject(String obj) {
        if (getSettings().getJavaScriptEnabled()) {
            RNCWebViewBridge b = createRNCWebViewBridge(this);
            b.setInjectedObjectJson(obj);
        }
    }

I solved the problem by explicitly setting settings.javaScriptEnabled = true in the default settings:

diff --git a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt
index 6a48a81..cee263c 100644
--- a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt
+++ b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt
@@ -72,6 +72,8 @@ class RNCWebViewManagerImpl {
         context.addLifecycleEventListener(webView)
         mWebViewConfig.configWebView(webView)
         val settings = webView.settings
+        // Fixes working injectedJavaScriptObject prop together with textZoom prop in WebView component
+        settings.javaScriptEnabled = true
         settings.builtInZoomControls = true
         settings.displayZoomControls = false
         settings.domStorageEnabled = true

This issue body was partially generated by patch-package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant