the message passing odyssey...

This commit is contained in:
Marcell Mars 2021-03-27 17:01:17 +01:00
parent 7606f779fd
commit 0aec7aafde
4 changed files with 32 additions and 17 deletions

View File

@ -16,6 +16,7 @@ function captureFrontMatterAndMarkdown() {
} else if (k == "relpath") {
filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}`
gitpath = `${location.pathname.split('/public')[0]}`
publishpath = `${gitpath}/public/${formData.get(k).replace(".md", "")}/index.html`
} else if (["protocol", "offline"].includes(k)) {
continue
} else {
@ -25,10 +26,10 @@ function captureFrontMatterAndMarkdown() {
}
frontmatter += "---\n"
const content = document.querySelector('textarea').value
let doc = frontmatter + content
let doc = frontmatter + "\n" + 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)}
return {"hugopage": doc, "filepath": filepath, "gitpath": gitpath, "publishpath": publishpath, "publish": publish, "offline": offline, "protocol": document.location.protocol.substr(0,4)}
}
let sb = document.getElementById('sandpointsButton')
@ -36,8 +37,11 @@ if (sb) {
sb.addEventListener(
"click", (e)=> {
e.preventDefault()
chrome.runtime.sendMessage(
captureFrontMatterAndMarkdown()
);
pageDict = captureFrontMatterAndMarkdown()
chrome.runtime.sendMessage(pageDict)
});
}
chrome.runtime.onMessage.addListener((m, n)=> {
window.location.replace(m.msg)
})

View File

@ -10,6 +10,16 @@ chrome.runtime.onMessage.addListener((m, n)=> {
nh.onMessage.addListener((m, n)=> {
console.log("native host message:", m)
console.log("native host sender:", n)
if (m.Response != "false") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { msg: m.response }, (response) => {
if (response) {
console.log(response)
}
});
})
}
})
})

View File

@ -38,12 +38,13 @@ var bufferSize = 8192
// IncomingMessage represents a message sent to the native host.
type IncomingMessage struct {
Hugopage string `json:"hugopage"`
Filepath string `json:"filepath"`
Gitpath string `json:"gitpath"`
Publish bool `json:"publish"`
Offline bool `json:"offline"`
Protocol string `json:"protocol"`
Hugopage string `json:"hugopage"`
Filepath string `json:"filepath"`
Gitpath string `json:"gitpath"`
Publishpath string `json:"publishpath"`
Publish bool `json:"publish"`
Offline bool `json:"offline"`
Protocol string `json:"protocol"`
}
// OutgoingMessage respresents a response to an incoming message query.
@ -207,14 +208,14 @@ func parseMessage(msg []byte) {
triggerGitHook(iMsg.Gitpath, iMsg.Publish, iMsg.Offline, iMsg.Protocol)
// start building outgoing json message
oMsg := OutgoingMessage{
Query: iMsg.Hugopage,
Query: iMsg.Protocol,
}
switch iMsg.Hugopage {
case "foo":
oMsg.Response = "bar"
switch iMsg.Protocol {
case "file":
oMsg.Response = iMsg.Publishpath
default:
oMsg.Response = "native host says yeah!"
oMsg.Response = "false"
}
send(oMsg)
@ -230,7 +231,7 @@ func decodeMessage(msg []byte) IncomingMessage {
return iMsg
}
// send sends an OutgoingMessage to os.Stdout.
// sends an OutgoingMessage to os.Stdout.
func send(msg OutgoingMessage) {
byteMsg := dataToBytes(msg)
writeMessageLength(byteMsg)

Binary file not shown.