【问题标题】:How to set window.onerror for a webview in android如何在android中为webview设置window.onerror
【发布时间】:2017-05-11 17:07:30
【问题描述】:

我有一个 webview,并且 javascript 界面也在那里。如何为其添加 window.onerror ?主要要求是,当我加载带有内容的 webview 时,otf 文件会引发一些无效标签错误(“OTS 解析错误:无效版本标签”),我需要捕获该错误。

【问题讨论】:

    标签: javascript android javascript-injection android-jsinterface


    【解决方案1】:

    您可以在收到错误时从 Java 中调用 JavaScript 函数:

    /**
     * Executes received Java Script call
     *
     * @param command - JS call to run
     */
    protected void executeJsCommand(final String command) {
            webView.evaluateJavascript(command, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(final String value) {}
            });
    }
    
    protected void callOnError(String error) {
        executeJsCommand("window.onerror( " + error +" )");
    }
    

    【讨论】:

    • 我们不知道究竟是什么时候抛出了错误。因此,为了处理它,我们首先需要知道错误何时被抛出,为此,我们必须首先捕获这样的错误。我该怎么做?
    • 我找到了一个链接,上面说我们可以覆盖 console.warn 但无法在用于替换警告的函数中获得任何警告。
    猜你喜欢
    • 1970-01-01
    • 2018-01-27
    • 2011-07-20
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2014-03-08
    相关资源
    最近更新 更多