-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Code from the injectedJavaScriptBeforeContentLoaded prop runs AFTER the web code. #1609
Comments
UPD: sometimes it works as expected, but like 50/50. |
i have the same issue on IOS 13.6.1, anyone has ideas? |
Same or similar issues here, however on Android the Javascript injected via injectedJavaScriptBeforeContentLoaded never runs, even when Javascript injected via injectedJavaScript does run. Device: Pixel 2 in Emulator |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
Dear bot, please don't close this issue because it's still an issue. |
Hi @Kreozot, did you find an away or workaround to solve this? |
I've avoided injecting. Instead I just put some async scripts loading in my HTML template: ...
<body>
<script>
// Function that runs some script and then calling the callback
function evalScript(filename, callback) {
var head=document.getElementsByTagName("head")[0];
var script=document.createElement('script');
script.src=filename;
script.type='text/javascript';
script.onload=callback;
head.appendChild(script);
}
evalScript("file:///android_asset/firstScript.js", function () { // will be executed before the second
evalScript("file:///android_asset/secondScript.js"); // will be executed after the first
});
</script>
</body> Yes, this looks very dirty - I'm not proud of it - but it works :) |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
I faced with it. |
I confirm it still exist (with the same device it sometimes execute before all script, sometime after the script in but before the script in etc. sounds like there's a missing /lock or sync issue causing the injection execution to be based purely on luck in android ) , @Titozzz @chiaramooney can this issue be reopened ? Also do you mind if I open a PR in the documentation to warn about this current issue waiting for it to solved? |
I also can confirm that this is still an issue, it seems that the |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
still searching for solution (a documentation warning could be enough ) |
My workaround as old as the world
|
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
if you are the same person (like me) running React native [email protected], and wondering why The idea is:
--- a/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
+++ b/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
@@ -396,6 +396,12 @@ 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);
@@ -747,6 +753,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
super.onPageStarted(webView, url, favicon);
mLastLoadFailed = false;
+ RNCWebView reactWebView = (RNCWebView) webView;
+ reactWebView.callInjectedJavaScriptBeforeContentLoaded();
+
dispatchEvent(
webView,
new TopLoadingStartEvent(
@@ -1000,6 +1009,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
String messagingModuleName;
@@ -1081,10 +1092,15 @@ 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);
}
+
protected void createCatalystInstance() {
ReactContext reactContext = (ReactContext) this.getContext();
@@ -1135,6 +1151,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
}
+ public void callInjectedJavaScriptBeforeContentLoaded() {
+ if (getSettings().getJavaScriptEnabled() &&
+ injectedJSBeforeContentLoaded != null &&
+ !TextUtils.isEmpty(injectedJSBeforeContentLoaded)) {
+ evaluateJavascriptWithFallback("(function() {\n" + injectedJSBeforeContentLoaded + ";\n})();");
+ }
+ }
+
public void onMessage(String message) {
ReactContext reactContext = (ReactContext) this.getContext();
RNCWebView mContext = this;
|
Reopening because:
That said, it's unclear to me if there's a possible "fix" here or if this is just an Android WebView limitation we need to warn folks about. I did see this comment (#1099 (comment)) suggesting an alternative approach, but that seems quite complex/hacky/risky for general use. Could be a useful workaround (via custom Android component) for folks who really need it though. |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
'sup bot. Still a thing. |
We are observing that this sometimes does not work for iOS as well. Has it been observed by anyone else? |
I've never seen nor heard of it failing on iOS |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
Similar issues here, On Android the Javascript injected via injectedJavaScriptBeforeContentLoaded/injectedJavaScript partly never runs. Device: Samsung Galaxy A34 5G |
OS version: android 13 |
Reopening for now, but I can't keep doing that forever, and it's worth noting that version @thenxkk I'm not aware of any issues with the normal |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
@TheAlmightyBob what could you say about this issue #3377 |
This is still an issue on 13.8.6. injectedJavaScriptBeforeContentLoaded only works 50% of the time on Android. |
Did anyone find a workaround? Android is 50\50... perfectly works on ios |
Not sure if related, but this seems to happen to me only when launching the app for the first time, after that it seems to work fine. |
this issue was happening to us only when launching the app for the first time in android after spending lot of time on it we realised that in android before fix eg: app code: this is what we were injecting in webview
web code: this is how we were reading it in web
for android the xyz was coming as empty in web while reading and in iOS xyz was coming as expected Solution
eg
Web code:
the web code handles for Js getting injected before/after reading on web contributors |
Bug description:
According to official docs of
react-native-webview
(The injectedJavaScriptBeforeContentLoaded prop):But we can see that the script on the page runs first, and only then runs the code from the
injectedJavaScriptBeforeContentLoaded
prop.The setup is:
index.html with test.js script: https://github.com/Kreozot/webview-scripts-test/tree/master/android/app/src/main/assets
The script has alert "script executed"
WebView with the injectedJavaScriptBeforeContentLoaded prop: https://github.com/Kreozot/webview-scripts-test/blob/master/App.js
The prop has alert "injectedJavaScriptBeforeContentLoaded"
To Reproduce:
git clone [email protected]:Kreozot/webview-scripts-test.git
cd webview-scripts-test
yarn
yarn run android
Expected behavior:
Alert "injectedJavaScriptBeforeContentLoaded" shows before "script executed".
Screenshots/Videos:
Alert "injectedJavaScriptBeforeContentLoaded" shows after "script executed".
Environment:
The text was updated successfully, but these errors were encountered: