Addon shared file between apps

Hello everyone,

I have done an addon for FirefoxOS with all my code into one file. I don’t really like that because the addon is injected into two different apps and there is a test to know which code apply to which app.

The manifest is really well structured because you can apply different js files between distinct apps. So I made a manifest like this one:

“content_scripts”: [{
“matches”: [“app://verticalhome.gaiamobile.org/index.html”],
“css”: [],
“js”: [“js/common.js”, “js/homescreen.js”]
},
{
“matches”: [“app://settings.gaiamobile.org/index.html”],
“css”: [],
“js”: [“js/common.js”, “js/settings.js”]
}],

The JS files are correctly injected into the correct apps. Cool! :smile:
But I have some code in common for the two apps and that’s why there is a common.js file into the manifest. The problem is that the functions defined into the common.js file are not existing for the other file (homescreen.js or settings.js).

Someone has an idea on how to resolve this problem. Thank you so much for your help.
The addon works without that but the code could be so much clearer with that and it will help people who will review this addon after submitting it into the marketplace.

I’m not sure you can share JS objects between separate injected files.
First, did you try exposing the objects on the window.wrappedJSObject
explicity?

js/common.js

window.wrappedJSObject.myObject = {...}



If that doesn't work, I can cc someone who knows more about the JS
sandboxes and x-rays who might be able to clarify the situation.

But also, if it's just cleaner code you are looking for, you could use a
build step that concatenates/minifies the different js files into one
target js file for each app.
1 Like

Thank you so much for your answer!
I don’t really see the reason why window.wrappedJSObject should work. But I will read the entire page to have a better opinion on that. I have not tested yet.

Your second solution makes sense to me and this solution seems to be the best one. I think I will do that but I’m not sure that the people who will be in charge of the validation will love minified JS… I don’t really know the process, I will give the github in comment when submitting it.

Thank you again for your help!