<commit_before>/* global hcaptcha */

import { qs, qsa, docReady, toggleHidden, config } from './red_util.js'

docReady(function () {
  qsa('#request_form, form.link', function (form) {
    form.onsubmit = captchaLink
  })

  qs('#help').onclick = function () {
    const helpOn = []
    const helpOff = []
    document.querySelectorAll('.help').forEach(helpbox => {
      if (helpbox.classList.contains('hidden')) {
        helpOff.push(helpbox)
      } else {
        helpOn.push(helpbox)
      }
    })
    if (helpOn.length === 0) {
      helpOff.forEach(helpbox => {
        helpbox.classList.remove('hidden')
      })
    } else {
      helpOn.forEach(helpbox => {
        helpbox.classList.add('hidden')
      })
    }
  }

  qsa('.help', function (element) {
    element.onclick = function () {
      toggleHidden(element)
    }
  })
})

function captchaLink (e) {
  const form = e.target.closest('form')
  if (config.hcaptcha_sitekey && !document.cookie.split(';').some((item) =>
    item.trim().startsWith('human_hmac='))) {
    qs('#captcha_popup').style.display = 'block'
    const widgetId = hcaptcha.render('captcha_popup', {
      size: 'normal',
      sitekey: config.hcaptcha_sitekey,
      callback: function (token) {
        const tokenElement = document.createElement('input')
        tokenElement.type = 'hidden'
        tokenElement.name = 'captcha_token'
        tokenElement.value = token
        tokenElement.style.visibility = 'hidden'
        form.appendChild(tokenElement)
        submitForm(form)
      }
    })
    hcaptcha.execute(widgetId)
  } else {
    submitForm(form)
  }
  e.preventDefault()
}

function submitForm (form) {
  if (config.hcaptcha_sitekey) {
    qs('#captcha_popup').style.display = 'none'
  }
  const args = serializeForm(form)
  form.action = `?${args}`
  form.submit()
}

const secrets = ['captcha_token']

function serializeForm (form) {
  const q = []
  for (let i = form.elements.length - 1; i >= 0; i = i - 1) {
    const element = form.elements[i]
    if (element.nodeName === 'INPUT' && ['hidden', 'url'].includes(element.type)
    ) {
      if (!secrets.includes(element.name)) {
        q.unshift(`${element.name}=${encodeURIComponent(element.value)}`)
      }
    }
  }
  return q.join('&')
}<commit_msg>make URLs a bit prettier<commit_after>/* global hcaptcha */

import { qs, qsa, docReady, toggleHidden, config } from './red_util.js'

docReady(function () {
  qsa('#request_form, form.link', function (form) {
    form.onsubmit = captchaLink
  })

  qs('#help').onclick = function () {
    const helpOn = []
    const helpOff = []
    document.querySelectorAll('.help').forEach(helpbox => {
      if (helpbox.classList.contains('hidden')) {
        helpOff.push(helpbox)
      } else {
        helpOn.push(helpbox)
      }
    })
    if (helpOn.length === 0) {
      helpOff.forEach(helpbox => {
        helpbox.classList.remove('hidden')
      })
    } else {
      helpOn.forEach(helpbox => {
        helpbox.classList.add('hidden')
      })
    }
  }

  qsa('.help', function (element) {
    element.onclick = function () {
      toggleHidden(element)
    }
  })
})

function captchaLink (e) {
  const form = e.target.closest('form')
  if (config.hcaptcha_sitekey && !document.cookie.split(';').some((item) =>
    item.trim().startsWith('human_hmac='))) {
    qs('#captcha_popup').style.display = 'block'
    const widgetId = hcaptcha.render('captcha_popup', {
      size: 'normal',
      sitekey: config.hcaptcha_sitekey,
      callback: function (token) {
        const tokenElement = document.createElement('input')
        tokenElement.type = 'hidden'
        tokenElement.name = 'captcha_token'
        tokenElement.value = token
        tokenElement.style.visibility = 'hidden'
        form.appendChild(tokenElement)
        submitForm(form)
      }
    })
    hcaptcha.execute(widgetId)
  } else {
    submitForm(form)
  }
  e.preventDefault()
}

function submitForm (form) {
  if (config.hcaptcha_sitekey) {
    qs('#captcha_popup').style.display = 'none'
  }
  const args = serializeForm(form)
  form.action = `?${args}`
  form.submit()
}

const secrets = ['captcha_token']

function serializeForm (form) {
  const q = []
  for (let i = form.elements.length - 1; i >= 0; i = i - 1) {
    const element = form.elements[i]
    if (element.nodeName === 'INPUT' && ['hidden', 'url'].includes(element.type)
    ) {
      if (!secrets.includes(element.name)) {
        q.unshift(`${element.name}=${encodeURI(element.value).replace('&', '%26')}`)
      }
    }
  }
  return q.join('&')
}
{'fraction_non_alphanumeric': 0.16271929824561404, 'fraction_numerical': 0.0021929824561403508, 'mean_word_length': 2.8424599831508, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3908396', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1506, 'n_words': 259}