(flutter) how can I get the html data form the loaded page in webview?

Viewed 340

how to get html content form flutterWebViewPlugin ( webview ) in flutter

I am going to edit and use one of the answers on that link page. It was an old sauce, so I barely removed the red line and even ran it, but because I was a beginner, I hit a wall. Find the <meta name = " " ......> part of the page loaded in the webview and save the text in " " to the 'catchtext' list, How can I make it print when the FloatingActionButton is pressed? Sorry for the basic question. I'm learning from the beginning, but I'm so curious, so I ask in advance. The source code is attached below.

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> {
  late InAppWebViewController webView;
  String myurl = "https://github.com/flutter";
  static var catchtext = [];

  @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(
                    initialUrlRequest: URLRequest(
                        url: Uri.parse("https://flutter.dev/")
                    ),
                    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: (controller, url) {

                    },
                    onLoadStop: (controller, url) async {
                        var html = await controller.evaluateJavascript(
                          source: "window.document.getElementsByTagName('head')[0].outerHTML;");
                          //   source: "window.document.body.innerText;");
                        print("==========start================");
                        catchtext = html;
                        log(html);

                    },
                  ))
            ])),
        floatingActionButton: FloatingActionButton(
          onPressed: ()
          {
            print(catchtext);
          },
          // tooltip: 'Increment',

          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}


0 Answers
Related