question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

window.postMessage does not set event.origin

See original GitHub issue

Basic info:

  • Node.js version: 10.16.0
  • jsdom version: 15.2.1

When calling window.postMessage() in JSDOM, registered event listeners will receive the event, however the origin property will be set to an empty string.

post-message.js, specifically calls out event.origin (and a few others) in a TODO which I believe (not 100% sure) is why I’m having problems with this

Minimal reproduction case

const { JSDOM } = require("jsdom");
const assert = require('assert');

const dom = new JSDOM(`<!DOCTYPE html>`);

dom.window.addEventListener("message", evt => {

    // Expected: evt.origin === "https://sending-domain.com"
    //   Actual: evt.origin === ""
    assert(evt.origin === 'https://sending-domain.com')
})

dom.window.location = "https://sending-domain.com"
dom.window.postMessage('The message being posted', '*')

How does similar code behave in browsers?

In the snippet below, evt.origin inside the event listener will be equal to window.location from the frame that called window.postMessage

window.addEventListener("message", evt => { /* handle evt */ })

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:15
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
spamshakercommented, Aug 7, 2022

Did you find any alternative?

I managed to workaround it. Instead of postMessage i just dispatch a message however would be good to have it properly working

//dom.window.postMessage('TheMessage', '*');
dom.window.dispatchEvent(new MessageEvent('message', { source: window, origin: window.location.origin, data: 'TheMessage'}))
8reactions
Bloodsuckercommented, Jul 9, 2021

Apparently jsdom also doesn’t set the window event’s source property to the source’s window var. Following the OC test case to reproduce this:

const { JSDOM } = require("jsdom");
const assert = require('assert');

const dom = new JSDOM(`<!DOCTYPE html>`);

dom.window.addEventListener("message", evt => {

    // Expected: evt.origin === "https://sending-domain.com"
    //   Actual: evt.origin === ""
    assert(evt.origin === 'https://sending-domain.com')

    // It must be the dom.window, but it's null
    assert(evt.source !== null);
})

dom.window.location = "https://sending-domain.com"
dom.window.postMessage('The message being posted', '*')

Does it make sense to also create a new bug issue in here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window.postMessage() - Web APIs | MDN
Specifies what the origin of this window must be for the event to be dispatched, either as the literal string "*" (indicating no...
Read more >
window.top.postMessage sends "null" as its event.origin
I'm trying to post a message from an iframe to the parent window. I of course want to check the origin of the...
Read more >
postMessage(): common issues and how you can mitigate ...
postMessage () method to allow cross-origin communication between different window objects. This method provides a way to circumvent the Same  ...
Read more >
Using JavaScript and window.postMessage() - In Plain English
Cross-Domain communication (also called Cross-origin) can be ... pop-up window, which we will use, but the recipient window does not have to ...
Read more >
8 Communication — HTML 5 - W3C
The following interface is defined for this event: ... If the origin of the target window doesn't match the given origin, the message...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found