<commit_before>var apiURL = 'https://api.put.io/v2'
var storageKey = 'putio-webextension'

browser
  .storage.local.get()
  .then(storage => {
    var extensionStorage = storage.storageKey

    if (!extensionStorage || !extensionStorage.token) {
      authorize()
    }
  })

function authorize() {
  var redirectURL = browser.identity.getRedirectURL()
  var clientID = '2939'

  var authURL = apiURL + '/oauth2/authenticate'

  authURL += ('?client_id=' + clientID)
  authURL += ('&response_type=token')
  authURL += ('&redirect_uri=' + encodeURIComponent(redirectURL))

  return browser.identity.launchWebAuthFlow({
    interactive: true,
    url: authURL,
  }, validate)
}

function validate(redirectURL) {
  var token = redirectURL.split('#access_token=')[1]

  browser
    .storage.local.set({
      [storageKey]: { token: token },
    })
    .then(() => initialize(token))
}

function initialize(token) {
  var notificationIcon = browser.extension.getURL('icon-notify.png')

  function onDownload(info, tab) {
    var url = apiURL + '/transfers/add'

    var data = JSON.stringify({
      url: info.linkUrl,
    })

    var xhr = new XMLHttpRequest()

    xhr.open('POST', url, true)
    xhr.setRequestHeader('Content-type','application/json; charset=utf-8')
    xhr.setRequestHeader('authorization', 'token ' + token)

    xhr.onload = function () {
      if (xhr.readyState == 4) {
        const response = JSON.parse(xhr.responseText)

        browser.notifications.create('transfer-start-success', {
          type: 'basic',
          iconUrl: notificationIcon,
          title: 'Transfer Started!',
          message: 'We will let you know when ' + response.transfer.name + ' is downloaded!',
        })
      } else {
        const response = JSON.parse(xhr.responseText)

        browser.notifications.create('transfer-start-failure', {
          type: 'basic',
          iconUrl: notificationIcon,
          title: 'Oops!',
          message: 'We are unable to download that :/',
        })
      }
    }

    xhr.send(data)
  }

  browser.contextMenus.create({
    title: browser.i18n.getMessage('downloadMenuItem'),
    contexts: ['link'],
    onclick: onDownload,
  })

  browser.notifications.onClicked.addListener(function(notificationId) {
    console.log(notificationId)

    switch (notificationId) {
      case 'transfer-start-success':
      case 'transfer-start-failure':
        browser.notifications.clear(notificationId)
        break

      default:
        break
    }
  })
}
<commit_msg>poll started transfers, notify when finished<commit_after>var apiURL = 'https://api.put.io/v2'
var appURL = 'https://app.put.io'
var storageKey = 'putio-webextension'

browser
  .storage.local.get()
  .then((storage) => {
    var extensionStorage = storage.storageKey

    if (!extensionStorage || !extensionStorage.token) {
      authorize()
    }
  })

function authorize() {
  var redirectURL = browser.identity.getRedirectURL()
  var clientID = '2939'

  var authURL = apiURL + '/oauth2/authenticate'

  authURL += ('?client_id=' + clientID)
  authURL += ('&response_type=token')
  authURL += ('&redirect_uri=' + encodeURIComponent(redirectURL))

  return browser.identity.launchWebAuthFlow({
    interactive: true,
    url: authURL,
  }, validate)
}

function validate(redirectURL) {
  var token = redirectURL.split('#access_token=')[1]

  browser
    .storage.local.set({
      [storageKey]: { token: token },
    })
    .then(() => initialize(token))
}

function initialize(token) {
  var notificationIcon = browser.extension.getURL('icon-notify.png')

  function checkTransferStatus(transfer) {
    console.log(transfer)

    var url = apiURL + '/transfers/' + transfer.id

    var xhr = new XMLHttpRequest()

    xhr.open('GET', url, true)
    xhr.setRequestHeader('authorization', 'token ' + token)

    xhr.onload = function () {
      if (xhr.readyState == 4) {
        const response = JSON.parse(xhr.responseText)

        if (response.transfer.file_id) {
          browser.notifications.create('transfer-finished-' + response.transfer.id + '-' + response.transfer.file_id, {
            type: 'basic',
            iconUrl: notificationIcon,
            title: 'Transfer Finished!',
            message: response.transfer.name + ' is downloaded!',
            buttons: [
              {
                title: 'Go To File',
              }
            ],
          })
        } else {
          setTimeout(checkTransferStatus.bind(null, response.transfer), 10000)
        }
      }
    }

    xhr.send(null)
  }

  function onDownload(info, tab) {
    var url = apiURL + '/transfers/add'

    var data = JSON.stringify({
      url: info.linkUrl,
    })

    var xhr = new XMLHttpRequest()

    xhr.open('POST', url, true)
    xhr.setRequestHeader('Content-type','application/json; charset=utf-8')
    xhr.setRequestHeader('authorization', 'token ' + token)

    xhr.onload = function () {
      if (xhr.readyState == 4) {
        const response = JSON.parse(xhr.responseText)

        browser.notifications.create('transfer-start-success-' + response.transfer.id, {
          type: 'basic',
          iconUrl: notificationIcon,
          title: 'Transfer Started!',
          message: 'We will let you know when ' + response.transfer.name + ' is downloaded!',
        })

        setTimeout(checkTransferStatus.bind(null, response.transfer), 3000)
      } else {
        const response = JSON.parse(xhr.responseText)

        browser.notifications.create('transfer-start-failure', {
          type: 'basic',
          iconUrl: notificationIcon,
          title: 'Oops!',
          message: 'We are unable to download that :/',
        })
      }
    }

    xhr.send(data)
  }

  browser.contextMenus.create({
    title: browser.i18n.getMessage('downloadMenuItem'),
    contexts: ['link'],
    onclick: onDownload,
  })

  browser.notifications.onClicked.addListener(function(notificationId) {
    browser.notifications.clear(notificationId)
  })

  browser.notifications.onButtonClicked.addListener(function(notificationId) {
    var transferId = notificationId.split('-')[2]
    var fileId = notificationId.split('-')[3]
    var url = appURL + '/files/' + fileId

    browser.notifications.clear('transfer-start-success-' + transferId)
    browser.notifications.clear(notificationId)

    browser.tabs.create({
      active: true,
      url: url,
    })
  })
}

{'fraction_non_alphanumeric': 0.13575226775101656, 'fraction_numerical': 0.005317485142320926, 'mean_word_length': 2.781785925487877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4507834', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1914, 'n_words': 370}