Import shared code between a Firefox addon and a Chrome addon in Firefox.

I am developing a Firefox addon that has a Chrome partner. To eliminate code duplication, there are some JavaScript libraries that they will share. Libraries are not written with the CommonJS design that Firefox's Jetpack modules expect. Is there a good way to import and use generic JavaScript in my Firefox modules?

Preferably, the shared code could be in a directory in my root directory, since the shared library has its own directory structure, and I don’t want to fill it all in the data directory or lib.

+4
source share
2 answers

It is easy to make the JS file downloadable via commonjs, you just need to add the functions needed as properties for the export object, for example:

function foo() {} function bar() {} if (exports !== void 0) { exports.foo = foo; exports.bar = bar; } 
+1
source

If you are creating an xul-based addon and you have a library that you want to use in it, you just need to put it on your content directory and a link to the library files in the xul files.

  - content\ - yourAddon.xul - yourAddon.js - yourLibDirectory\ - libfile1.js 

Then, on yourAddon.xul you include it in the header:

 <script type="application/x-javascript" src="chrome://yourExtension/content/yourAddon.js"/> <script type="application/x-javascript" src="chrome://yourExtension/content/yourLibDirectory/libfile1.js"/> 

If you do this with addon-sdk, I will not help much. :)

+1
source

Source: https://habr.com/ru/post/1492596/


All Articles