getMetaTags method

Future<List<MetaTag>> getMetaTags()

Returns the list of <meta> tags of the current WebView.

NOTE: It is implemented using JavaScript.

Implementation

Future<List<MetaTag>> getMetaTags() async {
  List<MetaTag> metaTags = [];

  List<Map<dynamic, dynamic>>? metaTagList =
      (await evaluateJavascript(source: """
(function() {
var metaTags = [];
var metaTagNodes = document.head.getElementsByTagName('meta');
for (var i = 0; i < metaTagNodes.length; i++) {
  var metaTagNode = metaTagNodes[i];

  var otherAttributes = metaTagNode.getAttributeNames();
  var nameIndex = otherAttributes.indexOf("name");
  if (nameIndex !== -1) otherAttributes.splice(nameIndex, 1);
  var contentIndex = otherAttributes.indexOf("content");
  if (contentIndex !== -1) otherAttributes.splice(contentIndex, 1);

  var attrs = [];
  for (var j = 0; j < otherAttributes.length; j++) {
    var otherAttribute = otherAttributes[j];
    attrs.push(
      {
        name: otherAttribute,
        value: metaTagNode.getAttribute(otherAttribute)
      }
    );
  }

  metaTags.push(
    {
      name: metaTagNode.name,
      content: metaTagNode.content,
      attrs: attrs
    }
  );
}
return metaTags;
})();
  """))?.cast<Map<dynamic, dynamic>>();

  if (metaTagList == null) {
    return metaTags;
  }

  for (var metaTag in metaTagList) {
    var attrs = <MetaTagAttribute>[];

    for (var metaTagAttr in metaTag["attrs"]) {
      attrs.add(MetaTagAttribute(
          name: metaTagAttr["name"], value: metaTagAttr["value"]));
    }

    metaTags.add(MetaTag(
        name: metaTag["name"], content: metaTag["content"], attrs: attrs));
  }

  return metaTags;
}