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") { } else if (k == "relpath") {
filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}` filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}`
gitpath = `${location.pathname.split('/public')[0]}` gitpath = `${location.pathname.split('/public')[0]}`
publishpath = `${gitpath}/public/${formData.get(k).replace(".md", "")}/index.html`
} else if (["protocol", "offline"].includes(k)) { } else if (["protocol", "offline"].includes(k)) {
continue continue
} else { } else {
@ -25,10 +26,10 @@ function captureFrontMatterAndMarkdown() {
} }
frontmatter += "---\n" frontmatter += "---\n"
const content = document.querySelector('textarea').value const content = document.querySelector('textarea').value
let doc = frontmatter + content let doc = frontmatter + "\n" + content
let publish = formData.get("publish") == "on" ? true : false let publish = formData.get("publish") == "on" ? true : false
let offline = formData.get("offline") == "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') let sb = document.getElementById('sandpointsButton')
@ -36,8 +37,11 @@ if (sb) {
sb.addEventListener( sb.addEventListener(
"click", (e)=> { "click", (e)=> {
e.preventDefault() e.preventDefault()
chrome.runtime.sendMessage( pageDict = captureFrontMatterAndMarkdown()
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)=> { nh.onMessage.addListener((m, n)=> {
console.log("native host message:", m) console.log("native host message:", m)
console.log("native host sender:", n) 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. // IncomingMessage represents a message sent to the native host.
type IncomingMessage struct { type IncomingMessage struct {
Hugopage string `json:"hugopage"` Hugopage string `json:"hugopage"`
Filepath string `json:"filepath"` Filepath string `json:"filepath"`
Gitpath string `json:"gitpath"` Gitpath string `json:"gitpath"`
Publish bool `json:"publish"` Publishpath string `json:"publishpath"`
Offline bool `json:"offline"` Publish bool `json:"publish"`
Protocol string `json:"protocol"` Offline bool `json:"offline"`
Protocol string `json:"protocol"`
} }
// OutgoingMessage respresents a response to an incoming message query. // 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) triggerGitHook(iMsg.Gitpath, iMsg.Publish, iMsg.Offline, iMsg.Protocol)
// start building outgoing json message // start building outgoing json message
oMsg := OutgoingMessage{ oMsg := OutgoingMessage{
Query: iMsg.Hugopage, Query: iMsg.Protocol,
} }
switch iMsg.Hugopage { switch iMsg.Protocol {
case "foo": case "file":
oMsg.Response = "bar" oMsg.Response = iMsg.Publishpath
default: default:
oMsg.Response = "native host says yeah!" oMsg.Response = "false"
} }
send(oMsg) send(oMsg)
@ -230,7 +231,7 @@ func decodeMessage(msg []byte) IncomingMessage {
return iMsg return iMsg
} }
// send sends an OutgoingMessage to os.Stdout. // sends an OutgoingMessage to os.Stdout.
func send(msg OutgoingMessage) { func send(msg OutgoingMessage) {
byteMsg := dataToBytes(msg) byteMsg := dataToBytes(msg)
writeMessageLength(byteMsg) writeMessageLength(byteMsg)

Binary file not shown.