android webview javascript 调用缺少的函数

发布于 2025-01-01 05:05:07 字数 577 浏览 1 评论 0原文

我需要从 android 的 webview 调用 javascript 时捕获错误;如果缺少 javascript,我需要捕获一些事件并在 android 中执行其他操作。

例如,我有 webview.loadUrl("javascript:doSomething");

该页面缺少“doSomething”,因此我可以在 logcat 中看到以下错误:

ReferenceError: Can't find variable: doSomething ...

我需要捕获此错误并处理它,例如类似

public override void onJsErrorReceived(String errorDescription)
{
    if(errorDescription.contains("doSomething"))
       handleError();
}

I can't find any这样的事件在 webview 中,也不能在 webview 的客户端(WebChromeClient 和 WebViewClient)中重写。

有人有想法吗?

谢谢

I need to catch an error when invoking javascript from android's webview; if the javascript is missing I need to catch some event and do something else in android instead.

for instance, I have webview.loadUrl("javascript:doSomething");

the page is missing 'doSomething', so I can see the following error in logcat:

ReferenceError: Can't find variable: doSomething ...

I need to catch this error and handle it, for instance something like

public override void onJsErrorReceived(String errorDescription)
{
    if(errorDescription.contains("doSomething"))
       handleError();
}

I can't find any such event in webview, nor in the clients of webview (WebChromeClient and WebViewClient) to overridel.

Does anybody have an idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2025-01-08 05:05:07

根据这个问题,您需要重写的方法是:

WebChromeClient.onConsoleMessage(String message, int lineNumber, String sourceID)

WebChromeClient.onConsoleMessage(ConsoleMessage cm)

As per this question, the methods you need to over-ride are:

WebChromeClient.onConsoleMessage(String message, int lineNumber, String sourceID)

and

WebChromeClient.onConsoleMessage(ConsoleMessage cm),

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文