Page <script> Calls page-mod Function

20 views
Skip to first unread message

Gabe

unread,
Mar 23, 2011, 4:48:48 PM3/23/11
to mozilla-labs-jetpack
Is it possible for a page-mod to define objects that are visible to
the code executing in a page's <script> elements? I saw a few mentions
about exporting JavaScript APIs using wrappedJSObject, etc.; but there
aren't any wrappedJSObjects (e.g. window.wrappedJSObject,
global.wrappedJSObject, etc.) in the context in which the page-mod
executes.

Hernan Rodriguez Colmeiro

unread,
Mar 23, 2011, 5:48:39 PM3/23/11
to mozilla-la...@googlegroups.com

Inside the page-mod you can get hold of the window global. Add
properties there and scripts running on the page will see them.

Hernán

Alexandre poirot

unread,
Mar 23, 2011, 5:52:00 PM3/23/11
to mozilla-la...@googlegroups.com
With the coming 1.0b4 version, you can do that!
I've attached a full working example in pagemod-example.zip
But :
1/ take care that there is still important bugs around content scripts scope, that may change these scripts behavior! I suggest you to follow this bug if you start using this:
  https://bugzilla.mozilla.org/show_bug.cgi?id=601295
2/ these pratices are easily subject to various security issues! So please don't do this if you don't really manage the subject.

Here is the main code used to expose a function to websites
### main.js
let pageMod = require("page-mod").PageMod({
    include: ["*"],
    contentScript: "new " + function ContentScriptScope() {
      window.myMethod = function (arg) {
        postMessage(arg);
      };
    },
    contentScriptWhen: "start", /* this is really important in order to be able to call this method before onload event */
    onAttach: function(worker) {
      worker.on("message", function (data) {
        // This will be called when the content script forward the call from the website
        // data is equal to the data given by the web page
        // here data="data-from-website"
       
      });
    }
  });

### website.html
...
<script>
myMethod("data-from-website");
</script>
...


2011/3/23 Gabe <grp...@gmail.com>

--
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.


Reply all
Reply to author
Forward
0 new messages