I've been attempting to make sure that the listener has processed all messages before proceeding with console.log("Done")
using await
, but it doesn't seem to be working. What could I possibly be overlooking?
const f = async (leftPaneRowEle,index) => {
leftPaneRowEle.scrollIntoView()
leftPaneRowEle.children[0].click()
console.log('downloadBtn clicked for', leftPaneRowEle)
const listener = (msg: any) => {
console.log('frontend msg:', msg)
}
const port = Browser.runtime.connect()
port.onMessage.addListener(async (msg, sender) => {
console.log("BG page received message", msg, "from", sender);
listener(msg)
});
await port.postMessage({ question: "Can you " + allTFBs[leftpaneindexes[index]] + ":" + desc })
return async () => {
await port.onMessage.removeListener(listener)
await port.disconnect()
}
}
const en: HTMLElement = document.querySelectorAll('#extensionListTable tbody')[0] as HTMLElement
const leftPaneRowEle0 = en.children[leftpaneindexes[0]]
await f(leftPaneRowEle0,0)
console.log("Done")
PS: The inspiration behind my approach comes from this answer