getHtml method

Future<String?> getHtml()

Gets the content html of the page. It first tries to get the content through javascript. If this doesn't work, it tries to get the content reading the file:

  • checking if it is an asset (file:///) or
  • downloading it using an HttpClient through the WebView's current url.

Returns null if it was unable to get it.

Implementation

Future<String?> getHtml() async {
  String? html;

  InAppWebViewGroupOptions? options = await getOptions();
  if (options != null && options.crossPlatform.javaScriptEnabled == true) {
    html = await evaluateJavascript(
        source: "window.document.getElementsByTagName('html')[0].outerHTML;");
    if (html != null && html.isNotEmpty) return html;
  }

  var webviewUrl = await getUrl();
  if (webviewUrl == null) {
    return html;
  }

  if (webviewUrl.isScheme("file")) {
    var assetPathSplitted = webviewUrl.toString().split("/flutter_assets/");
    var assetPath = assetPathSplitted[assetPathSplitted.length - 1];
    try {
      var bytes = await rootBundle.load(assetPath);
      html = utf8.decode(bytes.buffer.asUint8List());
    } catch (e) {}
  } else {
    HttpClient client = new HttpClient();
    try {
      var htmlRequest = await client.getUrl(webviewUrl);
      html =
          await (await htmlRequest.close()).transform(Utf8Decoder()).join();
    } catch (e) {
      print(e);
    }
  }

  return html;
}