我需要在我的应用程序中解析webview中的内容,这是可能的吗?现在我只能得到url页面。
我尝试在LoadFinished对象中搜索内容,但未找到
WebView组件:
<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>我希望从pageview获得所有的html内容。
发布于 2019-04-08 22:38:18
尝尝这个,
<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的功能的问题。您可能需要编写一个Android库项目来实现@JavascriptInterface,并按照here的解释使用它来跟踪内容。
https://stackoverflow.com/questions/55532596
复制相似问题