25

Is there any good tool out there that allows developers to correctly debug messages sent between windows with postMessage?

Or maybe a plugin for Firebug?

alex
  • 438,662
  • 188
  • 837
  • 957
Pierre Duplouy
  • 2,325
  • 5
  • 22
  • 24
  • 1
    What is there to debug? As long as the sending and receiving code is correct then it works. If not, then that's what you need to debug – Sean Kinsey Jun 16 '10 at 14:13
  • Indeed. I'd say, just check the value of the variables you're sending. E.g., in the linked example, look at the value of `myMessage.value` or `evt.data`. – Marcel Korpel Jun 16 '10 at 14:38
  • 3
    Well, to be fair it might be nice if (e.g.) Firebug could show you messages posted to a particular frame, regardless of what handler code exists, sort-of like how it shows XMLHttpRequest details. – Pointy Jun 16 '10 at 14:45
  • What Pointy described is exactly what I meant: what Firebug does with XHR is pretty cool, and having the same thing for postMessage would be sweet. – Pierre Duplouy Jun 17 '10 at 08:36
  • Agreed. Perhaps you can file a feature request? They have a [Google discussion group](http://groups.google.com/group/firebug) (not to confuse with the Firebug-FDGirls group, which is a totally different one ;), I think that's the appropriate place. – Marcel Korpel Jun 17 '10 at 14:46
  • Alright, thanks for your help! http://groups.google.com/group/firebug/browse_thread/thread/89f9f025a920244c – Pierre Duplouy Jun 18 '10 at 07:00

2 Answers2

23

Firebug (as of 1.11 beta 1) supports this with monitorEvents(). You can do something like this:

$("iframe").each(function(i, e) {
    console.log("Monitoring messages sent to: iframe(" + i + ")#" + $(this).attr("id"));
    monitorEvents(this.contentWindow, "message"); 
    
    // Send a test message to this iframe
    this.contentWindow.postMessage("Hi iframe - " + i, "*"); 
});

console.log("Monitoring messages sent to window");
monitorEvents(window, "message"); 
// Send a test message to the window
window.postMessage("Hi window", "*"); 

(@Pierre: thanks for mentioning that feature request)

EDIT: Also works in Chrome, though when I tried the above code I encountered a security error that the document.domain values were not the same, so the behavior of these two implementations may be slightly different.

UPDATE: I have submitted a feature request to the Chrome team asking that postMessage events appear in the timeline. Also, I found an extension called JScript Tricks that can inject arbitrary JavaScript code into a page when it is loaded. You can add the following code to it to monitor events once the page loads. It works pretty well, though it might miss events that occur immediately (e.g. before onload, if that's possible).

(function($) {
    var $window = $(window);
    $window.add("iframe").on("message", function(e) {
        console.log("Received messsage from " + e.originalEvent.origin + ", data = " + e.originalEvent.data);
    });
})(jQuery);
Community
  • 1
  • 1
jacobq
  • 9,812
  • 4
  • 34
  • 64
  • 1
    Any update on this in 2016? I'm not sure firebug still exists – Augustin Riedinger Oct 25 '16 at 17:13
  • 1
    Firebug definitely still exists (https://addons.mozilla.org/en-US/firefox/addon/firebug/) but I haven't had to debug `postMessage` in a long time, so I have not reviewed this answer recently. If you find something broken or have a better solution please share. – jacobq Oct 25 '16 at 18:06
  • FWIW, Chrome DevTools [documents `monitorEvents` here](https://developers.google.com/web/tools/chrome-devtools/console/events) – jacobq Nov 27 '18 at 13:00
  • 3
    Here's a vanilla JS version since I suspect some people might not use jQuery anymore: `monitorEvents(window, "message"); Array.from(document.getElementsByTagName('iframe')).forEach(iframe => monitorEvents(iframe.contentWindow, "message"))` – amoebe Feb 06 '19 at 17:25
  • On chrome dev tools => https://developers.google.com/web/updates/2015/05/quickly-monitor-events-from-the-console-panel – Sarath Feb 24 '21 at 13:18
-1

A firebug feature-request has been issued.

Pierre Duplouy
  • 2,325
  • 5
  • 22
  • 24