<commit_before>const mappingRegex = /mappings\[(\d*)\].(.*)/;
var mappings = [];

// Saves options to chrome.storage.sync.
function save_options() {
  updateMappingsFromFormData();

  chrome.storage.sync.set({
    mappings: mappings // only save if both key and type are set
  }, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Options saved.';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
    loadMappings();
  });
}

function updateMappingsFromFormData() {
  const elements = document.querySelectorAll('input, select');
  const data = [];
  for (let i = 0; i < elements.length; i++) {
    if (!elements[i].value) {
      continue;
    }
    const name = elements[i].name;
    let match;
    if ((match = mappingRegex.exec(name)) !== null) {
      const idx = match[1];
      const propName = match[2];
      if (!data[idx]) {
        data[idx] = {};
      }
      let value = elements[i].value;
      if (propName === 'key') {
        value = value.toLowerCase();
      }
      data[idx][propName] = value;
    }
  }

  mappings = data.filter(item => item.key && item.type);
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
  chrome.storage.sync.get({
    mappings: [{}]
  }, function(items) {
    mappings = items.mappings;
    loadMappings();
  });
}

var source = document.getElementById('mapping-template').innerHTML;
var template = Handlebars.compile(source);
Handlebars.registerHelper('select', function(selected, options) {
  return options.fn(this).replace(
    new RegExp(' value=\"' + selected + '\"'),
    '$& selected="selected"');
});
function loadMappings() {
  if (!mappings.length) {
    mappings.push({});
  }
  document.getElementById('mapping-container').innerHTML = template(mappings);

  var buttons = document.getElementsByClassName('delete');
  for (i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', deleteMapping);
  }
}
function addMapping() {
  updateMappingsFromFormData();
  mappings.push({});
  loadMappings();
}
function deleteMapping() {
  mappings.splice(this.value, 1);
  loadMappings();
}

document.addEventListener('DOMContentLoaded', restore_options);<commit_msg>focus last input when adding<commit_after>const mappingRegex = /mappings\[(\d*)\].(.*)/;
var mappings = [];

// Saves options to chrome.storage.sync.
function save_options() {
  updateMappingsFromFormData();

  chrome.storage.sync.set({
    mappings: mappings // only save if both key and type are set
  }, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Options saved.';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
    loadMappings();
  });
}

function updateMappingsFromFormData() {
  const elements = document.querySelectorAll('input, select');
  const data = [];
  for (let i = 0; i < elements.length; i++) {
    if (!elements[i].value) {
      continue;
    }
    const name = elements[i].name;
    let match;
    if ((match = mappingRegex.exec(name)) !== null) {
      const idx = match[1];
      const propName = match[2];
      if (!data[idx]) {
        data[idx] = {};
      }
      let value = elements[i].value;
      if (propName === 'key') {
        value = value.toLowerCase();
      }
      data[idx][propName] = value;
    }
  }

  mappings = data.filter(item => item.key && item.type);
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
  chrome.storage.sync.get({
    mappings: [{}]
  }, function(items) {
    mappings = items.mappings;
    loadMappings();
  });
}

var source = document.getElementById('mapping-template').innerHTML;
var template = Handlebars.compile(source);
Handlebars.registerHelper('select', function(selected, options) {
  return options.fn(this).replace(
    new RegExp(' value=\"' + selected + '\"'),
    '$& selected="selected"');
});
function loadMappings(focus) {
  if (!mappings.length) {
    mappings.push({});
  }
  var container = document.getElementById('mapping-container');
  container.innerHTML = template(mappings);

  var buttons = document.getElementsByClassName('delete');
  for (i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', deleteMapping);
  }

  if (focus) {
    // Give focus to last input
    const inputs = container.getElementsByTagName('input');
    inputs[inputs.length - 1].focus();
  }
}
function addMapping() {
  updateMappingsFromFormData();
  mappings.push({});
  loadMappings(true);
}
function deleteMapping() {
  mappings.splice(this.value, 1);
  loadMappings();
}

document.addEventListener('DOMContentLoaded', restore_options);
{'fraction_non_alphanumeric': 0.15851151315789475, 'fraction_numerical': 0.0034950657894736842, 'mean_word_length': 3.5004625346901017, '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': '3245060', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1444, 'n_words': 338}