My question is pretty simple. how can I get the html data form the loaded page in webview. The data is in JSON format. for a reason I cannot use a post request for any get request here so I need to get the data with webview only. how to achive this ?
flutterWebViewPlugin.onUrlChanged.listen((String url) {
if (url.contains('/recon?')) {
print('printing url : $url');
// need to get data ( html content ) on this url
flutterWebViewPlugin.close();
Navigator.of(context).pop(url);
}
});
});
WebviewScaffold(
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0",
url: myurl,
)
Firstly, we will use about:black as the initial URL to load an empty page. Then, we will set javascriptMode to unrestricted to allow the execution of Javascript code, and lastly, when the web view is created, we will load the content of the index. html and load it to the web view.
First, create an object of WebViewController and Completer class, here Completer class is used for a future task. Then handle the callback of completer inside build method and then . complete method is to be called on the completer after the webview is created.
With the WebView Flutter plugin you can add a WebView widget to your Android or iOS Flutter app. On iOS the WebView widget is backed by a WKWebView, while on Android the WebView widget is backed by a WebView. The plugin can render Flutter widgets over the web view.
You can do this with official webview library:
WebView(
initialUrl: model.luxtrustUrl,
onWebViewCreated: (controller) {
_controller = controller;
},
javascriptMode: JavascriptMode.unrestricted,
gestureNavigationEnabled: true,
onPageFinished: (_) {
readJS();
model.hideProgress();
},
)
void readJS() async{
String html = await _controller.evaluateJavascript("window.document.getElementsByTagName('html')[0].outerHTML;");
print(html);
}
You can use my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews.
To get HTML data, you can simply use var html = await controller.evaluateJavascript(source: "window.document.getElementsByTagName('html')[0].outerHTML;");
inside the onLoadStop
event.
Here is an example using your userAgent
:
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
InAppWebViewController webView;
String myurl = "https://github.com/flutter";
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('InAppWebView Example'),
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: myurl,
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true,
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0"
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) async {
if (url.contains('/recon?')) {
// if JavaScript is enabled, you can use
var html = await controller.evaluateJavascript(
source: "window.document.getElementsByTagName('html')[0].outerHTML;");
log(html);
}
},
))
])),
),
);
}
}
Just set the myurl
variable to your url.
Through window.document.getElementsByTagName('html')[0].outerHTML;
you will get all the HTML string. Instead, if you need only the body text, you can change it with window.document.body.innerText;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With