Skip to content

Commit

Permalink
fix custom js code not being executed
Browse files Browse the repository at this point in the history
  • Loading branch information
fm-sys committed Oct 5, 2022
1 parent 1f2b525 commit 7777da6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions app/src/main/java/com/fmsys/snapdrop/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
Expand Down Expand Up @@ -284,7 +285,7 @@ protected void onNewIntent(final Intent intent) {
binding.webview.clearFocus(); // remove potential text selections

final String clipText = getTextFromUploadIntent();
binding.webview.loadUrl(JavaScriptInterface.getSendTextDialogWithPreInsertedString(clipText));
binding.webview.evaluateJavascript(JavaScriptInterface.getSendTextDialogWithPreInsertedString(clipText), null);

final Snackbar snackbar = Snackbar
.make(binding.pullToRefresh, clipText.isEmpty() ? (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction()) ? R.string.intent_files : R.string.intent_file) : R.string.intent_content, Snackbar.LENGTH_INDEFINITE)
Expand Down Expand Up @@ -480,22 +481,35 @@ public boolean onCreateWindow(final WebView view, final boolean dialog, final bo
return false;
}

@Override
public boolean onConsoleMessage(final ConsoleMessage consoleMessage) {
if (consoleMessage.messageLevel().equals(ConsoleMessage.MessageLevel.ERROR)) {
Log.e("WebViewConsole", consoleMessage.message());
} else if (consoleMessage.messageLevel().equals(ConsoleMessage.MessageLevel.WARNING)) {
Log.w("WebViewConsole", consoleMessage.message());
} else {
Log.d("WebViewConsole", consoleMessage.message());
}
return true;
}
}

private class CustomWebViewClient extends WebViewClient {

@Override
public void onPageFinished(final WebView view, final String url) {
Log.w("SnapdropAndroid", "refresh finished");

state.setCurrentlyLoading(false);
binding.pullToRefresh.setRefreshing(false);

if (url.startsWith(baseURL)) {
Log.w("WebView", "refresh finished, init snapdrop...");
state.setCurrentlyOffline(false);
initSnapdrop();

} else {
Log.w("WebView", "finished loading " + url);
}

super.onPageFinished(view, url);
}

Expand All @@ -504,9 +518,12 @@ private void initSnapdrop() {
return; // too late to do anything at this point in time...
}
//website initialisation
binding.webview.loadUrl(JavaScriptInterface.getAssetsJS(MainActivity.this, "init.js"));
binding.webview.loadUrl(JavaScriptInterface.getSendTextDialogWithPreInsertedString(getTextFromUploadIntent()));
Log.w("WebView", "load init script...");
binding.webview.evaluateJavascript(JavaScriptInterface.getAssetsJS(MainActivity.this, "init.js"), null);
binding.webview.evaluateJavascript(JavaScriptInterface.getSendTextDialogWithPreInsertedString(getTextFromUploadIntent()), null);
WebsiteLocalizer.localize(binding.webview);
Log.w("WebView", "init end.");


// welcome dialog
if (prefs.getBoolean(getString(R.string.pref_first_use), true)) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/fmsys/snapdrop/WebsiteLocalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private WebsiteLocalizer() {

public static void localize(final WebView webView) {
for (TranslationElement element : TranslationElement.values()) {
webView.loadUrl(getTranslationJS(element, webView.getContext()));
webView.evaluateJavascript(getTranslationJS(element, webView.getContext()), null);
}
}

Expand Down

0 comments on commit 7777da6

Please sign in to comment.