Firefox extension includes script in main.js file

I am writing an extension for firefox and have a problem including scripts in the main.js (background) file ...

In my case, I would like to include for example jquery.js and config.js, but I cannot see how to do it correctly.

In my chrome extension, I just do this in the manifest file, but I would like to get the equivalent in firefox.

Thanks for the help, jdmry

+3


source to share


1 answer


To import files, you use Components.utils.import .

You will need a URL from which to import the file. Usually this is either resource: // or chrome: // URL.

Some examples of the supplied Mozilla libraries:

Components.utils.import("resource://gre/modules/FileUtils.jsm"); //File Utilities
Components.utils.import("resource://gre/modules/NetUtil.jsm");   //Network utilities
Components.utils.import("resource://gre/modules/Services.jsm");  //Services

      

Some examples of loading from your own extension:

Components.utils.import("chrome://myExtensionModule/content/common.jsm");          //Common
Components.utils.import("chrome://myExtensionModule/content/classes.jsm");         //Classes

      

To download from your own extension, you will need to have the files in a folder that has been mapped to a chrome: // or resource: // url in your chrome.manifest. For example, using the line:



content       myExtensionModule                             modules/

      

It doesn't have to be separate from the folder you would normally type for content, but it often happens that JavaScript modules are in a separate folder just for organization. You don't need them to use the .jsm extension, just a convention again. Using .js is just fine and might be desirable if you have other tools that auto-configure based on the .js extension (like an editor).

Much more information can be found in the MDN documentation:

Note that Firefox code typically uses shorter variable mapping to access some commonly used Firefox properties. In particular, often used:

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

      

They are usually defined in a much more abbreviated manner. They are explicitly listed above to make the display more readable.

+2


source







All Articles