SandpointsChromeExtension/app/c.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-03-25 15:03:41 +01:00
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
2021-03-25 15:03:41 +01:00
} else {
formDataJSON[key] = value
2021-03-25 15:03:41 +01:00
}
});
2021-03-25 15:03:41 +01:00
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)
}
2021-03-25 15:03:41 +01:00
}
let sb = document.getElementById('sandpointsButton')
if (sb) {
sb.addEventListener(
"click", (e)=> {
e.preventDefault()
2021-03-27 17:01:17 +01:00
pageDict = captureFrontMatterAndMarkdown()
chrome.runtime.sendMessage(pageDict)
2021-03-25 15:03:41 +01:00
});
}
2021-03-27 17:01:17 +01:00
chrome.runtime.onMessage.addListener((m, n)=> {
window.location.replace(m.msg)
})