Skip to main content
Question

postMessage to UI does not work and only sends undefined

  • March 20, 2025
  • 1 reply
  • 12 views

Matthias_Viranyi

Working on a document traversing plugin, I was required to build up a progress indicator in the UI. Basically traversing 

  • all pages
  • all componentSets
  • all components

in this order.

I had communication working between code and ui, but from one day to another, this changed. Now I can get only message from UI to code, but not back. Code is sending, no matter what I am handing it for sending, “undefined” .. 

On UI side, this is awaiting the messages:

window.addEventListener("message", (event) => {
    console.log("Received " + event.data.postMessage)
    if (event === undefined) {
        console.error("undefined event sent, why?")
        return;
    }
    if (event.data === undefined) {
        console.error("undefined event.data sent, why?")
        return;
    }
    if (event.data.postMessage === undefined) {
        console.error("undefined event.data.postMessage sent, why?") 
        // ^ it breaks here, since postMessage is undefined
        return;
    }
    // .. actual code comes here, but not required, fails above already
});

On Code (backend) side I send it during a async execution .. 

async function GoAndGetThemTiger () {
  cancelExecution = false;
  let pageCurrentNumber = 0;
  for (const page of figma.root.children) {
    figma.ui.postMessage("TEST!!!!!"); // usually this worked
    await page.loadAsync();
  }
}
figma.showUI(__html__);
figma.ui.resize(500, 800);
// .. does not work
figma.ui.postMessage("Yes, let's kick of creation!");
// .. doesn't work neither
GoAndGetThemTiger();

What am I doing wrong here?

Any hints and remarks are welcome and happy to receive (unlike this message receiver :D )

Many thanks,

M

1 reply

tank666
  • 4858 replies
  • March 20, 2025

The postMessage property doesn't exist in the event.data path, that's why you get undefined. You need to check event.data.pluginMessage.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings