Can I use third-party js libraries, such as underscores, in a Firefox add-in?

I am using the Firefox Add-On SDK to port the Chrome extension to Firefox. In Chrome, itโ€™s trivial to download third-party libraries like Underscore or Backbone. In my specific case, I use jQuery, Underscore, and Backbone to define models that interact with cross-domain REST APIs.

I donโ€™t understand how you can do something like this in Firefox. From what I see, main.js corresponds directly to the Chrome background pages, but there is no way to load js files there.

Am I missing something?

+4
source share
1 answer

The Add-on SDK supports Sysem modules for CommonJS, the same modules that are also used by nodejs https://github.com/mozilla/addon-sdk/tree/master/app-extension

Underscore supports the commonjs module format and can be easily downloaded https://github.com/documentcloud/underscore/blob/master/underscore.js#L54-L65

All you need to do is highlight the highlight next to main.js and load it like this:

var _ = require("./underscore") 

I really believe that the spine can also be loaded in the same way that people use it on nodes.

It will not work for jQuery, though, because the context where the optional SDK run modules differs from the typical context of a web page with the DOM is what jQuery designed to work with.

Now, if you want to perform cross-domain requests, the SDK comes with a module for this: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/addon-kit/request.html There is also another low-level XHR module that you can use instead: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/api-utils/xhr.html

So, if you just want to write models and talk with the REST API, this should be pretty trivial, I'm not sure what role jQuery is in your use case. This implies the DOM and UI you want the display to. If so, there are several modules in the SDK that will allow you to add a user interface for firefox, and you will probably find a useful guide on this subject: https://addons.mozilla.org/en-US/developers/docs/sdk /latest/dev-guide/tutorials/index.html

+2
source

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


All Articles