From 7d68b87a7e2f37a99dabdfabc6225d614a540146 Mon Sep 17 00:00:00 2001 From: Mani <12841290+infinyte7@users.noreply.github.com> Date: Sat, 20 Jun 2020 22:36:06 +0530 Subject: [PATCH] Show Toast using JavaScript function in WebView * Added function ankiShowToast card.js * Added url decoding of msg * Added encode toast msg, card.js --- AnkiDroid/src/main/assets/scripts/card.js | 6 ++++++ .../main/java/com/ichi2/anki/AbstractFlashcardViewer.java | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/AnkiDroid/src/main/assets/scripts/card.js b/AnkiDroid/src/main/assets/scripts/card.js index f102e7d03e36..269db891ed4c 100644 --- a/AnkiDroid/src/main/assets/scripts/card.js +++ b/AnkiDroid/src/main/assets/scripts/card.js @@ -134,6 +134,12 @@ function ankiToggleFlag(flag) { } } +// Show toast using js +function ankiShowToast(message) { + var msg = encodeURI(message); + window.location.href = "signal:anki_show_toast:" + msg; +} + /* Tell the app the text in the input box when it loses focus */ function taBlur(itag) { //#5944 - percent wasn't encoded, but Mandarin was. diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java b/AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java index 97a84ead5499..95a1396f3290 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java @@ -3181,6 +3181,14 @@ private boolean filterUrl(String url) { } } + // Show toast using JS + if (url.startsWith("signal:anki_show_toast:")) { + String msg = url.replaceFirst("signal:anki_show_toast:", ""); + String msgDecode = decodeUrl(msg); + UIUtils.showThemedToast(AbstractFlashcardViewer.this, msgDecode, true); + return true; + } + int signalOrdinal = WebViewSignalParserUtils.getSignalFromUrl(url); switch (signalOrdinal) { case WebViewSignalParserUtils.SIGNAL_UNHANDLED: