Skip to content

Is it possible to override the onJsAlert method of WebChromeClient? If so, how should I adjust my code? #13049

@heatwavesgrover

Description

@heatwavesgrover
package com.tauri.test

import  android.content.Context
import android.content.DialogInterface
import android.view.KeyEvent
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.JsResult
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.appcompat.app.AlertDialog

class MainActivity : TauriActivity() {

  override fun onWebViewCreate(webView: WebView) {
    webView.webChromeClient = customWebChromeClient
    // webView.webChromeClient = CustomWebChromeClient(this)
    super.onWebViewCreate(webView)
  }

  private val customWebChromeClient: WebChromeClient = object : WebChromeClient() {
    override fun onJsAlert(
      webview: WebView,
      url: String,
      message: String,
      result: JsResult
    ): Boolean {
      AlertDialog.Builder(this@MainActivity).setTitle(R.string.app_name)
        .setMessage(message)
        .setPositiveButton(
          "OK"
        ) { _: DialogInterface?, _: Int -> result.confirm() }
        .setCancelable(false)
        .show()
      return true
    }
  }
}

class CustomWebChromeClient(private val context: Context) : WebChromeClient() {
  override fun onJsAlert(
    webview: WebView,
    url: String,
    message: String,
    result: JsResult
  ): Boolean {
    AlertDialog.Builder(context).setTitle(R.string.app_name)
      .setMessage(message)
      .setPositiveButton(
        "ok"
      ) { _: DialogInterface?, _: Int -> result.confirm() }
      .setCancelable(false)
      .show()
    return true
  }
}

I would be truly grateful for your expert advice. As someone with limited professional knowledge, I often struggle to translate theory into practice. Any insights to help me bridge this gap would be invaluable. Thank you so much for your time and wisdom!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions