Skip to content

Instantly share code, notes, and snippets.

@abd3lraouf
Created December 10, 2021 10:52
Show Gist options
  • Save abd3lraouf/16406ac4686bdf86cce43d69768654c4 to your computer and use it in GitHub Desktop.
Save abd3lraouf/16406ac4686bdf86cce43d69768654c4 to your computer and use it in GitHub Desktop.
Android WebView The Missing guide
binding.webview.apply {
settings.javaScriptEnabled = true
loadUrl("https://abd3lraouf.github.io/webview_code_injection_from_android")
}
/**
* called at onCreate to initiate code injection
*/
fun bind(context: Context, webView: WebView) {
webView.addJavascriptInterface(JavaScriptShareInterface(context), "JsMediator")
}
object CustomWebViewClient : WebViewClient() {
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
JavaScriptShareInterface.injectMediator(view)
}
}
binding.webView.apply {
settings.javaScriptEnabled = true
setWebContentsDebuggingEnabled(true)
JavaScriptShareInterface.bind(requireContext(), this)
webViewClient = CustomWebViewClient
loadUrl("https://abd3lraouf.github.io/webview_code_injection_from_android")
}
function messageMe(message){
document.getElementById("demo").innerHTML = message;
}
<body>
<h1>Code injection</h1>
<button>Launch Native Code</button>
<h2> Messages from Android</h2>
<p id="demo">Can you replace me?</h2>
</body>
messageMe("Message")
evaluateJavascript("messageMe(\"$message\")", null)
@JavascriptInterface
@Suppress("unused")
fun toast(message: String) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
Android = {}; Android.toast = function(message) { JsMediator.toast(message); }
/**
* called before start loading the page
*/
fun injectMediator(webView: WebView) {
webView.evaluateJavascript(
"Android = {}; Android.toast = function(message) { JsMediator.toast(message); }",
null
)
}
binding.webView.apply {
settings.javaScriptEnabled = true
setWebContentsDebuggingEnabled(true)
JavaScriptShareInterface.bind(requireContext(), this)
loadUrl("https://abd3lraouf.github.io/webview_code_injection_from_android")
}
<?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>
<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