<commit_before>/**
 * To avoid the roundtrip to Switzerland whenever we need an alias as we 
 * maintain a buffer config['max_aliases'] aliases. It also allows us to get
 * around the rate limit on the service when we want to several addresses in
 * quick succession.
 */
if( !localStorage.config ){
  localStorage.config = JSON.stringify({
    days: 7,
    uses: 10,
    max_aliases: 5,
    host: "tempalias.com",
    shortcut: {
      shiftKey: true,
      ctrlKey: true,
      keyIdentifier: "U+0045" //e
    }
  });
  chrome.tabs.create( { url: "/options.html" } );
}
var aliases = [];

/**
 * Every 6 hours remove aliases older than 6 hours.
 */
setInterval( function(){
  aliases = aliases.filter( function( alias ){
      return 6*60*60*1000 > new Date() - new Date( alias.currentDate );
  });
  fillAliases();
}, 6*60*60*1000 );

/**
 * Fills up the aliases array by requesting an alias every minute (due to the
 * 30 second rate limit on the service).
 */
function fillAliases(){
  var config = JSON.parse( localStorage.config );
  if( !config.email || config.max_aliases === aliases.length )
    return;
  fetchAlias( config );

  var intervalId = setInterval( function(){
    var config = JSON.parse( localStorage.config );
    if( !config.email || config.max_aliases === aliases.length )
      return clearInterval( intervalId );
    fetchAlias( config );
  }, 60*1000 );
  
  function fetchAlias( config ){
    if( !config.email ) return;
    getAlias( config, function( response ){
      if( response.aid ) aliases.push( response );
    });
  }
}

function getAlias( config, success, error ){
  $.ajax({
    url: "http://" + config.host + "/aliases",
    type: "POST",
    processData: false,
    dataType: 'json',
    contentType: "application/json",
    data: JSON.stringify({
      "target": config.email,
      "max-usage": config.uses,
      "days": config.days }),
    success: success,
    error: error
  });
}

function showNotification( title, msg, icon, delay ){
  icon = icon || 'icons/icon48.png';
  delay = delay || 8000;
  var notification = webkitNotifications.createNotification(
    icon,
    title,
    msg);<commit_msg>getAlias will back off when unable to get email aliases 5 times to prevent dosing the server in the unlikely event the extension gets lots of users.<commit_after>/**
 * To avoid the roundtrip to Switzerland whenever we need an alias as we 
 * maintain a buffer config['max_aliases'] aliases. It also allows us to get
 * around the rate limit on the service when we want to several addresses in
 * quick succession.
 */
if( !localStorage.config ){
  localStorage.config = JSON.stringify({
    days: 7,
    uses: 10,
    max_aliases: 5,
    retry_attempts: 5,
    host: "tempalias.com",
    shortcut: {
      shiftKey: true,
      ctrlKey: true,
      keyIdentifier: "U+0045" //e
    }
  });
  chrome.tabs.create( { url: "/options.html" } );
}
var aliases = [];

/**
 * Every 6 hours remove aliases older than 6 hours.
 */
setInterval( function(){
  aliases = aliases.filter( function( alias ){
      return 6*60*60*1000 > new Date() - new Date( alias.currentDate );
  });
  fillAliases();
}, 6*60*60*1000 );

/**
 * Fills up the aliases array by requesting an alias every minute (due to the
 * 30 second rate limit on the service).
 */
function fillAliases(){
  var config = JSON.parse( localStorage.config );
  var retryAttempts = config.retry_attempts;
  if( !config.email || config.max_aliases === aliases.length )
    return;
  fetchAlias( config );

  var intervalId = setInterval( function(){
    var config = JSON.parse( localStorage.config );
    if( !config.email ||
        config.max_aliases === aliases.length ||
        retryAttempts < 1 ){
      return clearInterval( intervalId );
    }
    fetchAlias( config );
  }, 60*1000 );
  
  function fetchAlias( config ){
    if( !config.email ) return;
    getAlias( config, function( response ){
      if( response.aid ){
        aliases.push( response );
        retryAttempts = config.retry_attempts;
      }
    }, function( response ){
      --retryAttempts;
    });
  }
}

function getAlias( config, success, error ){
  $.ajax({
    url: "http://" + config.host + "/aliases",
    type: "POST",
    processData: false,
    dataType: 'json',
    contentType: "application/json",
    data: JSON.stringify({
      "target": config.email,
      "max-usage": config.uses,
      "days": config.days }),
    success: success,
    error: error
  });
}

function showNotification( title, msg, icon, delay ){
  icon = icon || 'icons/icon48.png';
  delay = delay || 8000;
  var notification = webkitNotifications.createNotification(
    icon,
    title,
    msg);
{'fraction_non_alphanumeric': 0.13199057210199272, 'fraction_numerical': 0.018641525605313906, 'mean_word_length': 3.024137931034483, 'pattern_counts': {'":': 6, '<': 4, '<?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': '2847208', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1412, 'n_words': 445}