Skip to content

Commit 325a0bd

Browse files
authored
Merge pull request facebook#6 from sghiassy/apitea/webview-fix
WebView: Injected JS is not returning a value on Android
2 parents 920e2ef + d709873 commit 325a0bd

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.graphics.Bitmap;
1919
import android.os.Build;
2020
import android.text.TextUtils;
21+
import android.webkit.JavascriptInterface;
2122
import android.webkit.WebView;
2223
import android.webkit.WebViewClient;
2324
import android.webkit.WebChromeClient;
@@ -180,6 +181,9 @@ private WritableMap createWebViewEvent(WebView webView, String url) {
180181
* to call {@link WebView#destroy} on activty destroy event and also to clear the client
181182
*/
182183
private static class ReactWebView extends WebView implements LifecycleEventListener {
184+
public static final String JAVASCRIPT_INTERFACE_NAME = "JS_INTERFACE";
185+
private static final String JAVASCRIPT_INTERFACE_METHOD = "processInjectedJavaScript";
186+
183187
private @Nullable String injectedJS;
184188

185189
/**
@@ -216,14 +220,41 @@ public void callInjectedJavaScript() {
216220
if (getSettings().getJavaScriptEnabled() &&
217221
injectedJS != null &&
218222
!TextUtils.isEmpty(injectedJS)) {
219-
loadUrl("javascript:(function() {\n" + injectedJS + ";\n})();");
223+
loadUrl("javascript:window."
224+
+ JAVASCRIPT_INTERFACE_NAME + "." + JAVASCRIPT_INTERFACE_METHOD
225+
+ "(" + injectedJS + ");");
220226
}
221227
}
222228

223229
private void cleanupCallbacksAndDestroy() {
224230
setWebViewClient(null);
225231
destroy();
226232
}
233+
234+
@JavascriptInterface
235+
@SuppressWarnings("unused")
236+
public void processInjectedJavaScript(final String jsEvaluationValue) {
237+
dispatchEvent(
238+
this,
239+
new TopLoadingFinishEvent(
240+
getId(),
241+
SystemClock.nanoTime(),
242+
createWebViewEvent(this, jsEvaluationValue)));
243+
}
244+
245+
private static void dispatchEvent(WebView webView, Event event) {
246+
ReactContext reactContext = (ReactContext) webView.getContext();
247+
EventDispatcher eventDispatcher =
248+
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
249+
eventDispatcher.dispatchEvent(event);
250+
}
251+
252+
private WritableMap createWebViewEvent(WebView webView, String jsEvaluationValue) {
253+
WritableMap event = Arguments.createMap();
254+
event.putDouble("target", webView.getId());
255+
event.putString("jsEvaluationValue", jsEvaluationValue);
256+
return event;
257+
}
227258
}
228259

229260
public ReactWebViewManager() {
@@ -246,6 +277,7 @@ public String getName() {
246277
protected WebView createViewInstance(ThemedReactContext reactContext) {
247278
ReactWebView webView = new ReactWebView(reactContext);
248279
webView.setWebChromeClient(new WebChromeClient());
280+
webView.addJavascriptInterface(webView, ReactWebView.JAVASCRIPT_INTERFACE_NAME);
249281
reactContext.addLifecycleEventListener(webView);
250282
mWebViewConfig.configWebView(webView);
251283

0 commit comments

Comments
 (0)