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=6012952/ 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>
...