<commit_before>/* global chrome */
var ipfsApi, isIpfs
const optionDefaults = {
  publicGateways: 'ipfs.io gateway.ipfs.io ipfs.pics global.upload',
  useCustomGateway: true,
  customGatewayUrl: 'http://127.0.0.1:8080',
  ipfsApiUrl: 'http://127.0.0.1:5001'
}

function init () {
  console.log('Initializing IPFS Support')
  // ipfs-api
  withOptions((options) => {
    // is-ipfs
    isIpfs = initIsIpfs()
    console.log('is-ipfs library test (should be true) --> ' + isIpfs.multihash('QmUqRvxzQyYWNY6cD1Hf168fXeqDTQWwZpyXjU5RUExciZ'))
    // ipfs-api
    ipfsApi = initIpfsApi(options.ipfsApiUrl)
    // execute test request :-)
    ipfsApi.id().then(function (id) {
      console.log('ipfs-api .id() test --> Node ID is: ', id)
    }).catch(function (err) {
      console.log('ipfs-api .id() test --> Failed to read Node info: ', err)
    })
    // persist any newly discovered defaults
    chrome.storage.local.set(options)
  })
}

function initIpfsApi (ipfsApiUrl) {
  const ipfsApi = window.frames.ipfsApiSandbox.IpfsApi
  let parsed = document.createElement('a') // oh goddess why
  parsed.href = ipfsApiUrl
  const apiHost = parsed.hostname
  const apiPort = parsed.port
  const apiProtocol = parsed.protocol.split(':')[0]
  parsed.remove()
  return ipfsApi({host: apiHost, port: apiPort, procotol: apiProtocol})
}

function initIsIpfs () {
  return window.frames.isIpfsSandbox.IsIpfs
}

function withOptions (callback) {
  chrome.storage.local.get(optionDefaults, (data) => {
    if (chrome.runtime.lastError) {
      console.log(chrome.runtime.lastError)
    } else {
      callback(data)
    }
  })
}

function onStorageChange (changes, area) {
  for (key in changes) {
    let change = changes[key]
    if (change.oldValue !== change.newValue) {
      if (key === 'ipfsApiUrl') {
        ipfsApi = initIpfsApi(change.newValue)
      }

      // debug
      console.log('Storage key "%s" in namespace "%s" changed. ' +
      'Old value was "%s", new value is "%s".',
        key,
        area,
        change.oldValue,
        change.newValue)
    }
  }
}

// init during addon startup
window.onload = init
// start tracking storage changes (user options etc)
chrome.storage.onChanged.addListener(onStorageChange)<commit_msg>Store default options if missing<commit_after>/* global chrome */
var ipfsApi, isIpfs
const optionDefaults = {
  publicGateways: 'ipfs.io gateway.ipfs.io ipfs.pics global.upload',
  useCustomGateway: true,
  customGatewayUrl: 'http://127.0.0.1:8080',
  ipfsApiUrl: 'http://127.0.0.1:5001'
}

function init () {
  console.log('Initializing IPFS Support')
  // ipfs-api
  withOptions((options) => {
    // is-ipfs
    isIpfs = initIsIpfs()
    console.log('is-ipfs library test (should be true) --> ' + isIpfs.multihash('QmUqRvxzQyYWNY6cD1Hf168fXeqDTQWwZpyXjU5RUExciZ'))
    // ipfs-api
    ipfsApi = initIpfsApi(options.ipfsApiUrl)
    // execute test request :-)
    ipfsApi.id().then(function (id) {
      console.log('ipfs-api .id() test --> Node ID is: ', id)
    }).catch(function (err) {
      console.log('ipfs-api .id() test --> Failed to read Node info: ', err)
    })
    storeDefaultOptionsIfMissing()
  })
}

function initIpfsApi (ipfsApiUrl) {
  const ipfsApi = window.frames.ipfsApiSandbox.IpfsApi
  let parsed = document.createElement('a') // oh goddess why
  parsed.href = ipfsApiUrl
  const apiHost = parsed.hostname
  const apiPort = parsed.port
  const apiProtocol = parsed.protocol.split(':')[0]
  parsed.remove()
  return ipfsApi({host: apiHost, port: apiPort, procotol: apiProtocol})
}

function initIsIpfs () {
  return window.frames.isIpfsSandbox.IsIpfs
}

function withOptions (callback) {
  chrome.storage.local.get(optionDefaults, (data) => {
    if (chrome.runtime.lastError) {
      console.log(chrome.runtime.lastError)
    } else {
      callback(data)
    }
  })
}

function storeDefaultOptionsIfMissing () {
  for (let key in optionDefaults) {
    chrome.storage.local.get(key, (data) => {
      if (!data[key]) {
        let option = {}
        option[key] = optionDefaults[key]
        chrome.storage.local.set(option)
      }
    })
  }
}

function onStorageChange (changes, area) {
  for (let key in changes) {
    let change = changes[key]
    if (change.oldValue !== change.newValue) {
      if (key === '<KEY>') {
        ipfsApi = initIpfsApi(change.newValue)
      }

      // debug
      console.log('Storage key "%s" in namespace "%s" changed. ' +
      'Old value was "%s", new value is "%s".',
        key,
        area,
        change.oldValue,
        change.newValue)
    }
  }
}

// init during addon startup
window.onload = init
// start tracking storage changes (user options etc)
chrome.storage.onChanged.addListener(onStorageChange)
{'fraction_non_alphanumeric': 0.13913595933926304, 'fraction_numerical': 0.011435832274459974, 'mean_word_length': 3.103388357949609, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1829473', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1612, 'n_words': 347}