Add Android support for beforeContentLoaded

This commit is contained in:
Vitaliy Vlasov 2020-02-11 15:06:06 +02:00
parent 31d015badf
commit 027eb8d803
No known key found for this signature in database
GPG Key ID: A7D57C347F2B2964
1 changed files with 24 additions and 0 deletions

View File

@ -392,6 +392,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
((RNCWebView) view).setInjectedJavaScript(injectedJavaScript);
}
@ReactProp(name = "injectedJavaScriptBeforeContentLoaded")
public void setInjectedJavaScriptBeforeContentLoaded(WebView view, @Nullable String injectedJavaScriptBeforeContentLoaded) {
((RNCWebView) view).setInjectedJavaScriptBeforeContentLoaded(injectedJavaScriptBeforeContentLoaded);
}
@ReactProp(name = "messagingEnabled")
public void setMessagingEnabled(WebView view, boolean enabled) {
((RNCWebView) view).setMessagingEnabled(enabled);
@ -733,6 +738,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
super.onPageStarted(webView, url, favicon);
mLastLoadFailed = false;
RNCWebView reactWebView = (RNCWebView) webView;
reactWebView.callInjectedJavaScriptBeforeContentLoaded();
dispatchEvent(
webView,
new TopLoadingStartEvent(
@ -971,6 +979,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected static class RNCWebView extends WebView implements LifecycleEventListener {
protected @Nullable
String injectedJS;
protected @Nullable
String injectedJSBeforeContentLoaded;
protected boolean messagingEnabled = false;
protected @Nullable
RNCWebViewClient mRNCWebViewClient;
@ -1044,6 +1054,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
injectedJS = js;
}
public void setInjectedJavaScriptBeforeContentLoaded(@Nullable String js) {
injectedJSBeforeContentLoaded = js;
}
protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
return new RNCWebViewBridge(webView);
}
@ -1082,9 +1096,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
injectedJS != null &&
!TextUtils.isEmpty(injectedJS)) {
evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");
//evaluateJavascriptWithFallback(injectedJS);
}
}
public void callInjectedJavaScriptBeforeContentLoaded() {
if (getSettings().getJavaScriptEnabled() &&
injectedJSBeforeContentLoaded != null &&
!TextUtils.isEmpty(injectedJSBeforeContentLoaded)) {
evaluateJavascriptWithFallback("(function() {\n" + injectedJSBeforeContentLoaded + ";\n})();");
//evaluateJavascriptWithFallback(injectedJSBeforeContentLoaded);
}
}
public void onMessage(String message) {
if (mRNCWebViewClient != null) {
WebView webView = this;