> Essentially we would introduce a messageHandler parameter. This
> parameter would be the place you declare your page mod's chrome-side
> functions. Behind the scenes, the SDK would take your functions, wrap
> them in the message protocol automatically, and allow you to then call
> these functions in the content page by the name you declared in the
> messageHandler param.
var pageMod = require("page-mod");
pageMod.PageMod({
include: "*.org",
contentScriptWhen: 'ready',
contentScript: 'reportLocation(window.location.href);'
messageHandlers:{
reportLocation: function(location){
require("notifications").notify({
title: "Page Location Notification",
text: "You are on the following page: " + location
});
}
}
});
Neat! I kind of like it. My main concern is whether it might trick new
developers into believing that the content script gets synchronous
direct access to the main addon process, when in fact it is limited to
doing postMessage with JSON data. The current approach, in which you can
see "postMessage()" all over the content script, makes this fairly
obvious. Adding some sugar to abstract out the setup and dispatch code,
while making things a lot easier to read overall, would happen to lose
this educational feature. For example, a newcomer might try to do this:
contentScript: 'if (isItInsecure(window.location.href)) {'+
'document.body.innerHTML="OMG! INSECURE!";}',
messageHandlers:{
isItInsecure: function(location){
require("notifications").notify({
title: "Page Location Notification",
text: "Insure Page Loaded: " + location
});
if (location.match(/^http:/)) return true; else return false;
}
}
I'm not sure how to balance the two desires. Maybe just some clear docs
and counter-examples (for instance, the above code with a lot of "THIS
DOESN'T WORK (and why)" labels on it).
Just for an experiment, what if we abstracted out the receive-side
dispatcher but didn't try to wrap the sender? I think it'd then look
like this:
contentScript: 'postMessage("reportLocation", window.location.href);'
messageHandlers:{
reportLocation: function(location){
require("notifications").notify({
title: "Page Location Notification",
text: "You are on the following page: " + location
});
}
}
Another thought is to put code into the "messageHandlers"-dispatcher
that notices whenever a handler returns a value, since this is never
useful in the handler for a send-and-forget postMessage (there'll be
no-one around to see the response). The dispatcher should throw an
exception or something, with an error message to explain to the
developer what they're doing wrong.
One other thought is how to handle misspelled messages, or messages
coming from other raw postMessage() calls in the content script. I
suppose that trying to call e.g. reportPosition(), when messageHandlers
only contains "reportLocation", will cause an error in the content
script, because it's trying to call an undefined function, so that's
pretty direct feedback.
Should the messageHandlers dispatcher's onListen() callback ignore
messages that it doesn't recognize? Probably yes.. otherwise it could be
hard to let it coexist with other messaging schemes.
thoughts?
-Brian
--
You received this message because you are subscribed to the Google Groups "mozilla-labs-jetpack" group.
To post to this group, send email to mozilla-la...@googlegroups.com.
To unsubscribe from this group, send email to mozilla-labs-jet...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mozilla-labs-jetpack?hl=en.
Is there a way we can provide a "stopgap" API now and deprecate it later? While it's cool that folks can use third-party APIs, it's also not great that things are so hard to use out-of-the-box for such common use cases.
- contentScript: 'reportLocation(window.location.href);'
+ contentScript: 'postMessage({ handler: "reportLocation", data: window.location.href });',
+ onMessage: function({ handler, data }) { this.messageHandlers[handler](data); },