Tuesday, June 11, 2013

webmessaging, postMessage and naive properties access

Sometimes I tend to put debug codes to dump method arguments. My favorite construction is
console.debug("dumpEvent:", evt); 
instead of often seen:
console.debug("dumpEvent:"+evt);
However, using this code is not a good idea with cross domain integration code:
win.global.addEventListener("message", dumpEvent);
function dumpEvent(evt) {
 // this will raise some errors in console
 // Blocked a frame with origin "http://a" from accessing 
 // a frame with origin "http://b". 
 // Protocols, domains, and ports must match.
 console.debug("dumpEvent", evt); // not a good idea
}

win.global.addEventListener("message", dumpEventProperties);
function dumpEventProperties(evt) {
 // this shall work, dump only properies specified in: 
 // http://www.w3.org/TR/webmessaging/
 console.debug("dumpEvent", {
  data : evt.data,
  origin : evt.origin,
  lastEventId : evt.lastEventId,
  //source : evt.source, //and this is the cause of problem ;-)
  ports : evt.ports
 });
}
So be aware, sometimes your logging statements can produce errors as well ;-)

No comments:

Post a Comment