<commit_before>/**
 * Checks Wayback Machine API for url snapshot
 */
function wmAvailabilityCheck(url, onsuccess, onfail) {
  var xhr = new XMLHttpRequest();
  var requestUrl = 'https://gext-api.archive.org/wayback/available';
  var requestParams = 'url=' + encodeURI(url);
  xhr.open('POST', requestUrl, true);
  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.setRequestHeader('Wayback-Api-Version', 2);
  xhr.onload = function() {
    var response = JSON.parse(xhr.responseText);
    var wayback_url = getWaybackUrlFromResponse(response);
    if (wayback_url !== null) {
      onsuccess(wayback_url, url);
    } else if (onfail) {
      onfail();
    }
  };
  xhr.send(requestParams);
}

/**
 * Makes sure response is a valid URL to prevent code injection
 * @param url {string}
 * @return {bool}
 */
function isValidUrl(url) {
  return ((typeof url) === "string" &&
    (url.indexOf("http://") === 0 || url.indexOf("https://") === 0));
}

//List of excluded Urls
var excluded_urls = [
  "localhost",
  "0.0.0.0",
  "127.0.0.1",
  "chrome://",
  "web.gext-api.archive.org",
  "web-beta.archive.org",
  "gext-api.archive.org",
  'chrome.google.com/webstore'
];
// Function to check whether it is a valid URL or not
function isNotExcludedUrl(url) {
  for (var i = 0, len = excluded_urls.length; i < len; i++) {
    if (url.startsWith("http://" + excluded_urls[i]) || url.startsWith("https://" + excluded_urls[i]) || url.startsWith(excluded_urls[i])) {
      return false;
    }
  }
  return true;
}

/**
 * @param response {object}
 * @return {string or null}
 */
function getWaybackUrlFromResponse(response) {
  if (response.results &&
    response.results[0] &&
    response.results[0].archived_snapshots &&
    response.results[0].archived_snapshots.closest &&
    response.results[0].archived_snapshots.closest.available &&
    response.results[0].archived_snapshots.closest.available === true &&
    response.results[0].archived_snapshots.closest.status.indexOf("2") === 0 &&
    isValidUrl(response.results[0].archived_snapshots.closest.url)) {
    return response.results[0].archived_snapshots.closest.url.replace(/^http:/, 'https:');
  } else {
    return null;
  }
}

/**
 * Customizes error handling
 * @param status {string}
 * @return {string}
 */
function getErrorMessage(req){
  return "The requested service " + req.url + " failed: " + req.status + ", " + req.statusText
}

function getUrlByParameter (name) {
  const url = new URL(window.location.href)
  return url.searchParams.get(name)
}

function openByWindowSetting(url, option=null, callback) {
  if (option === null) {
    chrome.storage.sync.get(['show_context'], function (event) { 
      option = event.show_context; 
      if (option === 'tab' || option === undefined) {
        chrome.tabs.create({ url: url }, function (tab) {
          if (callback) { 
            callback(tab.id); 
          }
        });
      } else {
        chrome.system.display.getInfo(function (displayInfo) {
          let height = displayInfo[0].bounds.height
          let width = displayInfo[0].bounds.width
          chrome.windows.create({ url: url, width: width / 2, height, top: 0, left: width / 2, focused: true }, function (window) {
            if (callback) { callback(window.tabs[0].id); }
          });
        })
      }
    });
  }
}

function attachTooltip (anchor, tooltip, pos='right', time=200) {
  // Modified code from https://embed.plnkr.co/plunk/HLqrJ6 to get tooltip to stay
  return anchor.attr({
    'data-toggle': 'tooltip',
    'title': tooltip
  })
    .tooltip({
      animated: false,
      placement: `${pos} auto`,
      html: true,<commit_msg>fixed a bug in util.js<commit_after>/**
 * Checks Wayback Machine API for url snapshot
 */
function wmAvailabilityCheck(url, onsuccess, onfail) {
  var xhr = new XMLHttpRequest();
  var requestUrl = 'https://gext-api.archive.org/wayback/available';
  var requestParams = 'url=' + encodeURI(url);
  xhr.open('POST', requestUrl, true);
  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.setRequestHeader('Wayback-Api-Version', 2);
  xhr.onload = function() {
    var response = JSON.parse(xhr.responseText);
    var wayback_url = getWaybackUrlFromResponse(response);
    if (wayback_url !== null) {
      onsuccess(wayback_url, url);
    } else if (onfail) {
      onfail();
    }
  };
  xhr.send(requestParams);
}

/**
 * Makes sure response is a valid URL to prevent code injection
 * @param url {string}
 * @return {bool}
 */
function isValidUrl(url) {
  return ((typeof url) === "string" &&
    (url.indexOf("http://") === 0 || url.indexOf("https://") === 0));
}

//List of excluded Urls
var excluded_urls = [
  "localhost",
  "0.0.0.0",
  "127.0.0.1",
  "chrome://",
  "web.gext-api.archive.org",
  "web-beta.archive.org",
  "gext-api.archive.org",
  'chrome.google.com/webstore'
];
// Function to check whether it is a valid URL or not
function isNotExcludedUrl(url) {
  for (var i = 0, len = excluded_urls.length; i < len; i++) {
    if (url.startsWith("http://" + excluded_urls[i]) || url.startsWith("https://" + excluded_urls[i]) || url.startsWith(excluded_urls[i])) {
      return false;
    }
  }
  return true;
}

/**
 * @param response {object}
 * @return {string or null}
 */
function getWaybackUrlFromResponse(response) {
  if (response.results &&
    response.results[0] &&
    response.results[0].archived_snapshots &&
    response.results[0].archived_snapshots.closest &&
    response.results[0].archived_snapshots.closest.available &&
    response.results[0].archived_snapshots.closest.available === true &&
    response.results[0].archived_snapshots.closest.status.indexOf("2") === 0 &&
    isValidUrl(response.results[0].archived_snapshots.closest.url)) {
    return response.results[0].archived_snapshots.closest.url.replace(/^http:/, 'https:');
  } else {
    return null;
  }
}

/**
 * Customizes error handling
 * @param status {string}
 * @return {string}
 */
function getErrorMessage(req){
  return "The requested service " + req.url + " failed: " + req.status + ", " + req.statusText
}

function getUrlByParameter (name) {
  const url = new URL(window.location.href)
  return url.searchParams.get(name)
}

function openByWindowSetting(url, option=null, callback) {
  if (option === null) {
    chrome.storage.sync.get(['show_context'], function (event) { option = event.show_context; });
  }
  if (option === 'tab' || option === undefined) {
    chrome.tabs.create({ url: url }, function (tab) {
      if (callback) { 
        callback(tab.id); 
      }
    });
  } else {
    chrome.system.display.getInfo(function (displayInfo) {
      let height = displayInfo[0].bounds.height
      let width = displayInfo[0].bounds.width
      chrome.windows.create({ url: url, width: width / 2, height, top: 0, left: width / 2, focused: true }, function (window) {
        if (callback) { callback(window.tabs[0].id); }
      });
    })
  }
}

function attachTooltip (anchor, tooltip, pos='right', time=200) {
  // Modified code from https://embed.plnkr.co/plunk/HLqrJ6 to get tooltip to stay
  return anchor.attr({
    'data-toggle': 'tooltip',
    'title': tooltip
  })
    .tooltip({
      animated: false,
      placement: `${pos} auto`,
      html: true,
{'fraction_non_alphanumeric': 0.16398625429553265, 'fraction_numerical': 0.009347079037800687, 'mean_word_length': 3.388419782870929, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, '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': '4692166', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2406, 'n_words': 513}