HTML5 postMessage is not good
so I propose a new interface.
User manual
To web designers:
you have to test whether your users have install this.
var installed=window.MSG!==undefined;
if false, then you will have to suggest your users to install this.
It's better not to take this addon to your name. If you do, I guess your users might suspect your accountability.
While I am the 3rd party to you & your users, so more trustable.
window.MSG.post(msgdata, arrayOfDomainTarget, arrayOfWindowIDTarget)
this 'window' can be omit, not similar to the one specified in HTML5
there you need an window object.
Here, you do not. this 'window' just indicate MSG is in the global space.
msgname is the the name the message category.
msgdata is a JSON object. it will be stringified before post
arrayOfDomainTarget, arrayOfWindowIDTarget
I use logical AND. originally it was OR
later I changed it to AND. more appropriate. I guess.
and I let "*" to be the wildcard for all windowID.
the caller's information does not present in the parameter at all
since the browser knows all the information.
so we can see that this method does not allow the sender to fool the receiver.
the sender just call as follows.
window.name="randomGeneratedWindowID";
MSG.post({what:'do thing abc',parameter:'the value of a parameter'},["jackiszhp.info"],[*]);
for the receiver, 2 things.
//#1 define the message handler
function messageHandler(e){
var obj=JSON.parse(e.detail);
obj.name is the msgname === 'msgpost' fixed
obj.data is the msgdata = {what:'fuck',who:'not to tell'};
obj.source is the sender
obj.source.href is the sender's window.location.href
obj.source.WID is the sender's window.name, i.e. 'randomGeneratedWindowID'
obj.target is the target of this event
obj.target.domains is the target domains of this event
obj.target.WIDs is the target WIDs of this event
....
}
//#2 register the message handler
window.addEventListener(msgname, messageHandler,false);
or
document.addEventListener(msgname, messageHandler,false);
while msgname==="msgpost" fixed.
//to respond
window.name="here another windowID";
var hostname=getHostname(obj.source.href);
MSG.post({what:'do thing defg',parameter:'the value of a parameter'},[hostname],[obj.source.WID]);
Additional note:
#A. window can be uniquely identified.
but here, I did not use it.
I use window.name instead.
https://developer.mozilla.org/en-US/docs/Code_snippets/Windows#Uniquely_identifying_DOM_windows
#B. I hope the mozilla can take my interface and include it into the firefox.
Feedback
jackiszhp@gmail.com
Written on 2013-02-10
Last update: 2013-02-10