logDocument method

T logDocument({
  1. String? id,
  2. bool compressed = false,
})

Implementation

T logDocument({String? id, bool compressed = false}) {
  id ??= '?';

  id = id
      .replaceAll(RegExp(r'\s+'), ' ')
      .trim()
      .replaceAll(RegExp(r'\s'), '_');

  var outerHtml = context.document.element.outerHtml ?? '';
  var timeMs = DateTime.now().millisecondsSinceEpoch;

  String? msg;

  if (compressed && outerHtml.length > 100) {
    var bytes = dart_convert.utf8.encode(outerHtml);

    var gZipEncoder = GZipEncoder();
    var compressed = gZipEncoder.encode(bytes);

    if (compressed != null) {
      var base64 = dart_convert.base64.encode(compressed);
      msg =
          '[$id]<<<<<<(GZIP: ${compressed.length}/${bytes.length})\n$base64\n>>>>>>$timeMs';
    }
  }

  msg ??= '[$id]<<<<<<\n$outerHtml\n>>>>>>$timeMs';

  logMessage('DOCUMENT', msg);

  return this as T;
}