<commit_before>(function () {
  var lightness = 0;

  function onExtensionMessage(request) {
    var isNestedDocument = (window != window.top);
    if (!request) return;
    var chroma = request.chroma;
    if (!isNestedDocument && lightness > 128) {
      chroma = 'original-background-and-colors';
    }
    document.documentElement.setAttribute('chroma', chroma);
    if (isNestedDocument) {
      document.documentElement.setAttribute('nested-document', 'true');
    }
  }

  chrome.extension.onRequest.addListener(onExtensionMessage);
  function init() {
    chrome.extension.sendRequest({
      'init': true
    }, onExtensionMessage);
  }

  init();

  document.addEventListener("DOMContentLoaded", function (event) {
    function getAverageTextColor() {
      var color, average = { red: 0, green: 0, blue: 0 }, incrementRed, incrementGreen, incrementBlue;
      var node, clientRects, iChild, rect, weight, colorScanNode,
        totalWeight = 0,
        walk = document.createTreeWalker(document, NodeFilter.SHOW_TEXT, null, false),
        scaledArea = function (rect) {
          if (!rect) return 0;
          var scale = 200;
          return rect.width / scale * rect.height / scale;
        };
      while (node = walk.nextNode()) {
        clientRects = node.parentElement.getBoundingClientRect();
        weight = scaledArea(rect);
        for (iChild = 0; iChild < node.parentElement.children.length; iChild++) {
          rect = node.parentElement.children[iChild].getBoundingClientRect();
          weight -= scaledArea(rect);
        }
        if (weight <= 0) continue;
        color = (window.getComputedStyle(node.parentElement).color).match(/\d+/g);
        incrementRed = parseInt(color[0]);
        incrementGreen = parseInt(color[1]);
        incrementBlue = parseInt(color[2]);
        average.red += incrementRed * weight;
        average.blue += incrementBlue * weight;
        average.green += incrementGreen * weight;
        var max = Math.max(incrementRed, incrementGreen, incrementBlue);
        totalWeight += weight;
      }
      average.red /= totalWeight; average.blue /= totalWeight; average.green /= totalWeight;
      return average;
    };

    var averageTextColor = getAverageTextColor();
    var max = Math.max(averageTextColor.red, averageTextColor.green, averageTextColor.blue),
        min = Math.min(averageTextColor.red, averageTextColor.green, averageTextColor.blue);
    lightness = (max + min) / 2;

    init();
  });
})();<commit_msg>#2 - cleaning up getAverageColor function<commit_after>(function () {
  function init() {
    chrome.extension.sendRequest({
      'init': true
    }, onExtensionMessage);
  };

  function onExtensionMessage(request) {
    var isNestedDocument = (window != window.top);
    if (!request) return;
    var chroma = request.chroma;
    if (!isNestedDocument && lightness > 128) {
      chroma = 'original-background-and-colors';
    }
    document.documentElement.setAttribute('chroma', chroma);
    if (isNestedDocument) {
      document.documentElement.setAttribute('nested-document', 'true');
    }
  };

  function getAverageTextColor() {
    function getScaledArea(element) {
      var rect, scale = 200;
      if (!element) return 0;
      rect = element.getBoundingClientRect();
      if (!rect) return 0;
      return rect.width / scale * rect.height / scale;
    };

    function getWeight(element) {
      var iChild;
      weight = getScaledArea(element);
      for (iChild = 0; iChild < element.children.length; iChild++) {
        weight -= getScaledArea(element.children[iChild]);
      }
    };

    var color,
      node, weight,
      totalWeight = 0,
      average = { red: 0, green: 0, blue: 0 }, red, green, blue,
      walk = document.createTreeWalker(document, NodeFilter.SHOW_TEXT, null, false);
    while (node = walk.nextNode()) {
      weight = getWeight(node.parentElement);
      if (weight <= 0) continue;
      color = (window.getComputedStyle(node.parentElement).color).match(/\d+/g);
      red = parseInt(color[0]);
      green = parseInt(color[1]);
      blue = parseInt(color[2]);
      average.red += red * weight;
      average.blue += blue * weight;
      average.green += green * weight;
      totalWeight += weight;
    }
    average.red /= totalWeight; average.blue /= totalWeight; average.green /= totalWeight;
    return average;
  };

  var lightness = 0;

  chrome.extension.onRequest.addListener(onExtensionMessage);

  init();

  document.addEventListener("DOMContentLoaded", function (event) {
    var averageTextColor = getAverageTextColor(),
      max = Math.max(averageTextColor.red, averageTextColor.green, averageTextColor.blue),
      min = Math.min(averageTextColor.red, averageTextColor.green, averageTextColor.blue);
    lightness = (max + min) / 2;

    init();
  });
})();
{'fraction_non_alphanumeric': 0.12990246939199004, 'fraction_numerical': 0.00788545341357128, 'mean_word_length': 3.0402347024308467, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6678884', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1382, 'n_words': 305}