-
-
Save abd3lraouf/16406ac4686bdf86cce43d69768654c4 to your computer and use it in GitHub Desktop.
Android WebView The Missing guide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
binding.webview.apply { | |
settings.javaScriptEnabled = true | |
loadUrl("https://abd3lraouf.github.io/webview_code_injection_from_android") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* called at onCreate to initiate code injection | |
*/ | |
fun bind(context: Context, webView: WebView) { | |
webView.addJavascriptInterface(JavaScriptShareInterface(context), "JsMediator") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CustomWebViewClient : WebViewClient() { | |
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) { | |
super.onPageStarted(view, url, favicon) | |
JavaScriptShareInterface.injectMediator(view) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
binding.webView.apply { | |
settings.javaScriptEnabled = true | |
setWebContentsDebuggingEnabled(true) | |
JavaScriptShareInterface.bind(requireContext(), this) | |
webViewClient = CustomWebViewClient | |
loadUrl("https://abd3lraouf.github.io/webview_code_injection_from_android") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function messageMe(message){ | |
document.getElementById("demo").innerHTML = message; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<h1>Code injection</h1> | |
<button>Launch Native Code</button> | |
<h2> Messages from Android</h2> | |
<p id="demo">Can you replace me?</h2> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
messageMe("Message") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
evaluateJavascript("messageMe(\"$message\")", null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JavascriptInterface | |
@Suppress("unused") | |
fun toast(message: String) { | |
Toast.makeText(context, message, Toast.LENGTH_LONG).show() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android = {}; Android.toast = function(message) { JsMediator.toast(message); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* called before start loading the page | |
*/ | |
fun injectMediator(webView: WebView) { | |
webView.evaluateJavascript( | |
"Android = {}; Android.toast = function(message) { JsMediator.toast(message); }", | |
null | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
binding.webView.apply { | |
settings.javaScriptEnabled = true | |
setWebContentsDebuggingEnabled(true) | |
JavaScriptShareInterface.bind(requireContext(), this) | |
loadUrl("https://abd3lraouf.github.io/webview_code_injection_from_android") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".HomeFragment"> | |
<WebView | |
android:id="@+id/web_view" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:text="@string/home_fragment_label" | |
app:layout_constraintBottom_toTopOf="@+id/guideline" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<androidx.constraintlayout.widget.Guideline | |
android:id="@+id/guideline" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
app:layout_constraintGuide_percent=".65" /> | |
<TextView | |
android:id="@+id/tvTitle" | |
style="@style/TextAppearance.AppCompat.Title" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:layout_marginTop="24dp" | |
android:layout_marginEnd="16dp" | |
android:text="@string/we_can_send_data_to_the_web_app" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/web_view" /> | |
<EditText | |
android:id="@+id/etMessage" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_marginStart="16dp" | |
android:layout_marginTop="16dp" | |
android:layout_marginEnd="16dp" | |
android:autofillHints="name" | |
android:hint="@string/message_to_be_sent" | |
android:inputType="textAutoComplete" | |
android:textColorHint="#757575" | |
app:layout_constraintEnd_toStartOf="@+id/btnSend" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/tvTitle" /> | |
<Button | |
android:id="@+id/btnSend" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="16dp" | |
android:text="@string/send" | |
app:layout_constraintBottom_toBottomOf="@+id/etMessage" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="@+id/etMessage" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.INTERNET" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment