function captureFrontMatterAndMarkdown() {
  const formData = new FormData(document.querySelector("form"))

  var formDataJSON = {};
  formData.forEach((value, key) => {
    if(key.endsWith('[]')){
      let kie = key.slice(0, -2);
      (kie in formDataJSON) || (formDataJSON[kie] = [])
      formDataJSON[kie].push(value);
    } else if (['draft', 'iscjklanguage'].includes(key) && value!="true") {
      return
    } else if (key == "relpath"){
      filepath = `${location.pathname.split('/public')[0]}/content/${value}`;
      gitpath = `${location.pathname.split('/public')[0]}`;
    } else if (key == "relpermalink" ){
      publishpath = `${location.pathname.split('/public')[0]}/public${value}index.html`;
    } else if (["protocol", "offline", "ignore"].includes(key)) {
      return
    } else {
      formDataJSON[key] = value
    }
  });
  
  const content = document.querySelector('textarea').value
  let publish = formData.get("publish") == "on" ? true : false
  let offline = formData.get("offline") == "on" ? true : false
  return {
    "hugofrontmatter": JSON.stringify(formDataJSON),
    "hugocontent": content,
    "filepath": filepath,
    "gitpath": gitpath,
    "publishpath": publishpath,
    "publish": publish,
    "offline": offline,
    "protocol": document.location.protocol.substr(0,4)
  }
}

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

chrome.runtime.onMessage.addListener((m, n)=> {
  window.location.replace(m.msg)
})
