<commit_before>/**
 * Checks if the URL can be found in the supplied set of redirection rules.
 * @param {string} url - The url to check.
 * @param {array} rules - Array of 'rules' objects to check against. {src:"Source", dest:"Destination"}
 * @return {string|null} String containing the newly redirected URL. Null if a redirection doesn't exist.
 */
function isRedirectRule(url, rules) {
  var replaced = null;
  var i = 0;
  while(!replaced && i < rules.length) {
    /* Make sure that the destination isn't already in the URL. (ex amazon.com -> smile.amazon.com) */
    if(url.search(rules[i].dest) < 0) {
      var attempt = url.replace(rules[i].src, rules[i].dest);
      if(url !== attempt) {
        replaced = attempt;
      }
    }
    i++;
  }
  return replaced;
}

/**
 * Checks if the URL is of the 'm.' style. (ex. en.m.wikipedia.org)
 * @param {string} url - The url to check.
 * @return {string|null} String containing the now non-mobile URL. Null if it's not a mobile URL.
 */
function isMobile(url) {
  var match = /([^a-z ]m\.)/i.exec(url);
  if(match) {
    var nonMobileUrl = url.split('');
    nonMobileUrl.splice(match.index + 1, 2);
    return nonMobileUrl.join('');
  }
  return null;
}

/* Listener Init */
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'loading' && tab.active) {
    chrome.storage.sync.get({
      redirection: 'enable'   // Default to redirection enabled.
      }, function(items) {
        /* Make sure that redirection is enabled */
        if(items.redirection === 1) {
          chrome.storage.sync.get("redirectionRules", function(result) {
            var rules = result.redirectionRules;

            /* Check and see if the url exists in the list of redirection rules */<commit_msg>Improved the mobile website checker<commit_after>/**
 * Checks if the URL can be found in the supplied set of redirection rules.
 * @param {string} url - The url to check.
 * @param {array} rules - Array of 'rules' objects to check against. {src:"Source", dest:"Destination"}
 * @return {string|null} String containing the newly redirected URL. Null if a redirection doesn't exist.
 */
function isRedirectRule(url, rules) {
  var replaced = null;
  var i = 0;
  while(!replaced && i < rules.length) {
    /* Make sure that the destination isn't already in the URL. (ex amazon.com -> smile.amazon.com) */
    if(url.search(rules[i].dest) < 0) {
      var attempt = url.replace(rules[i].src, rules[i].dest);
      if(url !== attempt) {
        replaced = attempt;
      }
    }
    i++;
  }
  return replaced;
}

/**
 * Checks if the URL is a mobile URL. (ex. en.m.wikipedia.org or mobile.twitter.com)
 * @param {string} url - The url to check.
 * @return {string|null} String containing the now non-mobile URL. Null if it's not a mobile URL.
 */
function isMobile(url) {
  var mobileSubdomains = ['m', 'mobile'];

  var domain = /\/\/(\S+?)\//.exec(url)[1];
  var domainTokens = domain.split('.');

  /* Basically, just loop through the array of domain tokens and check each mobile subdomain against it.
     If a match is found, stop the loop, and replace the url's domain with the new non-mobile one. Theres probaby a better way to do this. */
  var domainIndex = 0;
  var mobileIndex = 0;
  var match = null;
  while(!match && domainIndex < domainTokens.length) {
    while(!match && mobileIndex < mobileSubdomains.length) {
      if(domainTokens[domainIndex] === mobileSubdomains[mobileIndex]) {
        domainTokens.splice(domainIndex, 1);
        match = url.replace(domain, domainTokens.join('.'));
      }
      mobileIndex++;
    }
    domainIndex++;
  }
  return match;
}

/* Listener Init */
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'loading' && tab.active) {
    chrome.storage.sync.get({
      redirection: 'enable'   // Default to redirection enabled.
      }, function(items) {
        /* Make sure that redirection is enabled */
        if(items.redirection === 1) {
          chrome.storage.sync.get("redirectionRules", function(result) {
            var rules = result.redirectionRules;

            /* Check and see if the url exists in the list of redirection rules */
{'fraction_non_alphanumeric': 0.1344835736232569, 'fraction_numerical': 0.0028362089340581422, 'mean_word_length': 2.9699812382739212, 'pattern_counts': {'":': 0, '<': 9, '<?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': '2091151', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1274, 'n_words': 430}