<commit_before>'use strict';

var url = require('url');
var request = require('then-request');
var he = require('he');

var showingRealNames = true;
var realNames = {
  'AndrewHunterRedGate': '<NAME>',
  'andrewdenty': '<NAME>'
};

function loadRealName(username, path) {
  if (realNames[username]) return;
  realNames[username] = username;
  request('GET', url.resolve(location.href, path)).getBody().done(function (res) {
    var name = /\<div class\=\"vcard-fullname\" itemprop\=\"name\">([^<]+)\<\/div\>/.exec(res);
    name = name && he.decode(name[1]);
    realNames[username] = name || username;
    update();
  });
}

chrome.runtime.sendMessage({action: "get-showingRealNames"}, function(response) {
  showingRealNames = response.showingRealNames;
  update();
  setInterval(update, 1000);
});

function updateList(list, filter, getUsername, getHref, shouldAt) {
  for (var i = 0; i < list.length; i++) {
    if (filter(list[i])) {
      var username =getUsername(list[i]);
      var href = getHref ? getHref(list[i]) : '/' + username;
      loadRealName(username, href);
      if (showingRealNames) {
        list[i].textContent = (shouldAt && realNames[username] === username ? '@' : '') + realNames[username];
      } else {
        list[i].textContent = (shouldAt ? '@' : '') + username;
      }
    }
  }
}
function update() {
  updateList(document.getElementsByClassName('author'), function (author) {
    return author.hasAttribute('href');
  }, function (author) {
    return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
  }, function (author) {
    return author.getAttribute('href');
  });
  updateList(document.querySelectorAll("[data-ga-click*='target:actor']"), function (author) {
    return author.hasAttribute('href');
  }, function (author) {
    return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
  }, function (author) {
    return author.getAttribute('href');
  });
  updateList(document.getElementsByClassName('user-mention'), function (mention) {
    return mention.hasAttribute('href');
  }, function (mention) {
    return /\/([^\/]+)$/.exec(mention.getAttribute('href'))[1];
  }, function (mention) {
    return mention.getAttribute('href');
  }, true);
  updateList(document.getElementsByClassName('commit-author'), function (author) {
    return true;
  }, function (author) {
    if (author.hasAttribute('data-user-name')) {
      return author.getAttribute('data-user-name');
    } else {
      var username = author.textContent;
      if (username.indexOf('author=') !== -1) {
        username = username.split('author=').pop();
      }
      author.setAttribute('data-user-name', username);
      return username;
    }
  }, function (author) {
    return '/' + author.getAttribute('data-user-name');
  });
  updateList(document.querySelectorAll('.opened-by a.tooltipped.tooltipped-s'), function (author) {
    return true;
  }, function (author) {
    if (author.hasAttribute('data-user-name')) {
      return author.getAttribute('data-user-name');
    } else {
      var username = author.textContent;
      author.setAttribute('data-user-name', username);
      return username;
    }
  }, function (author) {
    return '/' + author.getAttribute('data-user-name');
  });
  updateList(document.querySelectorAll('.author-name a[rel="author"], .author a[rel="author"]'), function (author) {
    return author.hasAttribute('href');
  }, function (author) {
    return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
  }, function (author) {
    return author.getAttribute('href');
  });
}

update();
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if (message.action === 'toggle') {
    showingRealNames = message.showingRealNames;
    update();
  }
});<commit_msg>Fix github-real-names to use API, work with more fields. (#10)<commit_after>'use strict';

var url = require('url');
var request = require('then-request');

var showingRealNames = true;
var realNames = {
};

function loadRealName(username) {
  if (realNames[username]) return;
  realNames[username] = username;
  request('GET', url.resolve('https://api.github.com/users/', username)).getBody().done(function (res) {
    res = JSON.parse(res)
    if (res.name) {
      realNames[username] = res.name
    }
    update();
  });
}

chrome.runtime.sendMessage({action: "get-showingRealNames"}, function(response) {
  showingRealNames = response.showingRealNames;
  update();
  setInterval(update, 1000);
});

function updateList(list, filter, getUsername, shouldAt) {
  for (var i = 0; i < list.length; i++) {
    if (filter(list[i])) {
      var username = getUsername(list[i]);
      loadRealName(username);
      if (showingRealNames && realNames[username] && realNames[username] !== username) {
        list[i].textContent = realNames[username];
      } else {
        list[i].textContent = (shouldAt ? '@' : '') + username;
      }
    }
  }
}
function update() {
  updateList(document.getElementsByClassName('author'), function (author) {
    return author.hasAttribute('href');
  }, function (author) {
    return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
  });
  updateList(document.querySelectorAll("[data-ga-click*='target:actor']"), function (author) {
    return author.hasAttribute('href');
  }, function (author) {
    return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
  });
  updateList(document.getElementsByClassName('user-mention'), function (mention) {
    return mention.hasAttribute('href');
  }, function (mention) {
    return /\/([^\/]+)$/.exec(mention.getAttribute('href'))[1];
  }, true);
  updateList(document.getElementsByClassName('commit-author'), function (author) {
    return true;
  }, function (author) {
    if (author.hasAttribute('data-user-name')) {
      return author.getAttribute('data-user-name');
    } else {
      var username = author.textContent;
      if (username.indexOf('author=') !== -1) {
        username = username.split('author=').pop();
      }
      author.setAttribute('data-user-name', username);
      return username;
    }
  });
  updateList(document.querySelectorAll('.opened-by a.tooltipped.tooltipped-s'), function (author) {
    return true;
  }, function (author) {
    if (author.hasAttribute('data-user-name')) {
      return author.getAttribute('data-user-name');
    } else {
      var username = author.textContent;
      author.setAttribute('data-user-name', username);
      return username;
    }
  });
  updateList(document.querySelectorAll('.author-name a[rel="author"], .author a[rel="author"]'), function (author) {
    return author.hasAttribute('href');
  }, function (author) {
    return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
  });
  updateList(document.querySelectorAll('.opened-by a.muted-link'), function (author) {
    return true;
  }, function (author) {
    if (author.hasAttribute('data-user-name')) {
      return author.getAttribute('data-user-name');
    } else {
      var username = author.textContent;
      author.setAttribute('data-user-name', username);
      return username;
    }
  });
  updateList(document.querySelectorAll('.sidebar-assignee p span.text-bold'), function (author) {
    return true;
  }, function (author) {
    if (author.hasAttribute('data-user-name')) {
      return author.getAttribute('data-user-name');
    } else {
      var username = author.textContent;
      author.setAttribute('data-user-name', username);
      return username;
    }
  });
}

update();
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if (message.action === 'toggle') {
    showingRealNames = message.showingRealNames;
    update();
  }
});
{'fraction_non_alphanumeric': 0.18477409245233742, 'fraction_numerical': 0.003003395142334813, 'mean_word_length': 3.813953488372093, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '28254', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2346, 'n_words': 422}