function captureFrontMatterAndMarkdown() {
  console.log(location.pathname.split('/public')[0])
  const formData = new FormData(document.querySelector("form"))
  let frontmatter = "---\n"
  for (const k of formData.keys()) {
    // draft, iscjklanguage are default implicit (false) params in hugo
    if (["draft", "iscjklanguage"].includes(k)){
      console.log(formData.get(k))
      if (formData.get(k) === "true") {
        frontmatter += `${k}: true\n`
      }
    } else if (k.endsWith('[]')) {
      formData.set(k, formData.getAll(k))
      var s = JSON.stringify(formData.getAll(k)[0].split(","))
      frontmatter += `${k.slice(0, -2)}: ${s}\n`
    } else if (k == "relpath") {
      filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}`
      gitpath = `${location.pathname.split('/public')[0]}`
    } else if (["protocol", "offline"].includes(k)) {
      continue 
    } else {
      var s = JSON.stringify(formData.get(k))
      frontmatter += `${k}: ${s}\n`
    }
  }
  frontmatter += "---\n"
  const content = document.querySelector('textarea').value
  let doc = frontmatter + content
  let publish = formData.get("publish") == "on" ? true : false
  let offline = formData.get("offline") == "on" ? true : false
  return {"hugopage": doc, "filepath": filepath, "gitpath": gitpath, "publish": publish, "offline": offline, "protocol": document.location.protocol.substr(0,4)}
}

let sb = document.getElementById('sandpointsButton')
if (sb) {
  sb.addEventListener(
    "click", (e)=> {
      e.preventDefault()
      chrome.runtime.sendMessage(
        captureFrontMatterAndMarkdown()
      );
    });
}
