|
1 | 1 | package com.github.alinz.reactnativewebviewbridge;
|
2 | 2 |
|
3 |
| -import javax.annotation.Nullable; |
4 |
| -import java.util.Map; |
5 |
| - |
6 | 3 | import android.webkit.WebView;
|
7 | 4 |
|
| 5 | +import com.facebook.react.bridge.ReactContext; |
| 6 | +import com.facebook.react.bridge.ReadableArray; |
8 | 7 | import com.facebook.react.views.webview.ReactWebViewManager;
|
9 | 8 | import com.facebook.react.views.webview.WebViewConfig;
|
10 |
| -import com.facebook.react.bridge.ReadableArray; |
11 |
| -import com.facebook.react.bridge.ReactContext; |
12 |
| -import com.facebook.react.bridge.WritableMap; |
13 |
| -import com.facebook.react.common.MapBuilder; |
| 9 | + |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import javax.annotation.Nullable; |
14 | 13 |
|
15 | 14 | public class WebViewBridgeManager extends ReactWebViewManager {
|
16 | 15 | private static final String REACT_CLASS = "RCTWebViewBridge";
|
@@ -64,29 +63,37 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
|
64 | 63 | private void sendToBridge(WebView root, String message) {
|
65 | 64 | //root.loadUrl("javascript:(function() {\n" + script + ";\n})();");
|
66 | 65 | String script = "WebViewBridge.onMessage('" + message + "');";
|
67 |
| - root.evaluateJavascript(script, null); |
| 66 | + WebViewBridgeManager.evaluateJavascript(root, script); |
68 | 67 | }
|
69 | 68 |
|
70 | 69 | private void injectBridgeScript(WebView root) {
|
71 | 70 | //this code needs to be called once per context
|
72 | 71 | if (!initializedBridge) {
|
73 |
| - root.addJavascriptInterface(new JavascriptBridge((ReactContext)root.getContext()), "WebViewBridgeAndroid"); |
| 72 | + root.addJavascriptInterface(new JavascriptBridge((ReactContext) root.getContext()), "WebViewBridgeAndroid"); |
74 | 73 | initializedBridge = true;
|
75 | 74 | root.reload();
|
76 | 75 | }
|
77 | 76 |
|
78 |
| - //this code needs to be executed everytime a url changes. |
79 |
| - root.evaluateJavascript("" |
80 |
| - + "(function() {" |
81 |
| - + "if (window.WebViewBridge) return;" |
82 |
| - + "var customEvent = document.createEvent('Event');" |
83 |
| - + "var WebViewBridge = {" |
84 |
| - + "send: function(message) { WebViewBridgeAndroid.send(message); }," |
85 |
| - + "onMessage: function() {}" |
86 |
| - + "};" |
87 |
| - + "window.WebViewBridge = WebViewBridge;" |
88 |
| - + "customEvent.initEvent('WebViewBridge', true, true);" |
89 |
| - + "document.dispatchEvent(customEvent);" |
90 |
| - +"}());", null); |
| 77 | + // this code needs to be executed everytime a url changes. |
| 78 | + WebViewBridgeManager.evaluateJavascript(root, "" |
| 79 | + + "(function() {" |
| 80 | + + "if (window.WebViewBridge) return;" |
| 81 | + + "var customEvent = document.createEvent('Event');" |
| 82 | + + "var WebViewBridge = {" |
| 83 | + + "send: function(message) { WebViewBridgeAndroid.send(message); }," |
| 84 | + + "onMessage: function() {}" |
| 85 | + + "};" |
| 86 | + + "window.WebViewBridge = WebViewBridge;" |
| 87 | + + "customEvent.initEvent('WebViewBridge', true, true);" |
| 88 | + + "document.dispatchEvent(customEvent);" |
| 89 | + + "}());"); |
| 90 | + } |
| 91 | + |
| 92 | + static private void evaluateJavascript(WebView root, String javascript) { |
| 93 | + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { |
| 94 | + root.evaluateJavascript(javascript, null); |
| 95 | + } else { |
| 96 | + root.loadUrl("javascript:" + javascript); |
| 97 | + } |
91 | 98 | }
|
92 | 99 | }
|
0 commit comments