我如何解析 webview 内容?

How i can parse webview content?

我需要在我的应用程序中解析来自 webview 的内容, 可能吗?现在我只能得到 url 页。

我尝试在 LoadFinished 对象中搜索内容,但没有找到

Web 视图组件:

<template>
    <Page class="webview-page">
        <ScrollView>
            <WebView height="100%" src="https://www.fl.ru/login/"
                @loadFinished="parsePage" />
        </ScrollView>
    </Page>
</template>
<script>
    export default {
        methods: {
            parsePage(webargs) {
                //I need gets content here
                console.log(webargs.url);
            }
        },

        data() {
            return {};
        }
    };
</script>

我希望从网页浏览中获取所有 html 内容

试试这个,

<template>
    <Page class="page">
        <ActionBar title="Home" class="action-bar" />
        <GridLayout>
            <WebView src="https://www.nativescript.org/" @loadFinished="onLoadFinished"
                :visibility="visibility"></WebView>
            <ScrollView v-if="html">
                <Label class="h3" :text="html" textWrap="true"></Label>
            </ScrollView>
        </GridLayout>
    </Page>
</template>

<script>
    import {
        device
    } from "platform";

    export default {
        data() {
            return {
                html: ""
            };
        },
        computed: {
            visibility: function() {
                return this.html ? "hidden" : "visible";
            }
        },
        methods: {
            onLoadFinished: function(args) {
                const that = this,
                    webView = args.object,
                    jsStr = "document.body.innerHTML";

                if (webView.ios) {
                    webView.ios.evaluateJavaScriptCompletionHandler(jsStr,
                        function(
                            result,
                            error
                        ) {
                            if (error) {
                                console.log("error...");
                            } else if (result) {
                                that.html = result;
                            }
                        });
                } else if (webView.android) {
                    // Works only on Android 19 and above
                    webView.android.evaluateJavascript(
                        jsStr,
                        new android.webkit.ValueCallback({
                            onReceiveValue: function(html) {
                                that.html = html;
                            }
                        })
                    );
                }
            }
        }
    };
</script>

如果您希望支持较早的 Android 版本(API 级别 17 和 18),实施起来会有些困难。您将必须实现一个只能在 Java 中编写的 @JavascriptInterface 接口。关于启用访问 Java Annotation form JavaScript. You may have to write an Android library project where you implement the @JavascriptInterface and utilise it to track content as explained here.

的能力,已经报告了一个问题

Playground Sample