Components.utils.importGlobalProperties

Imports various objects into a system scope.

System scopes such as JSMs and frame scripts don't have certain objects, such as indexedDB and XMLHttpRequest, that are available to DOM window globals. Using this API you can import these objects into such a system scope.

This function is not intended for sandboxes but for system-privileged scopes. To import these objects into a sandbox, use the wantGlobalProperties option in the Sandbox constructor.

This function is passed an array of strings. Each string is the name of an object to import, and will be defined on the importing scope's global. The following strings are supported:

String/Object XPCOM Component
atob
Blob
btoa
crypto
CSS
fetch
File nsIDOMFile
indexedDB
NodeFilter Firefox 60 nsIDOMNodeFilter Obsolete since Gecko 60
rtcIdentityProvider
TextDecoder
TextEncoder
URL
URLSearchParams
XMLHttpRequest nsIXMLHttpRequest Obsolete since Gecko 60

For string/object in table without a minimum firefox version, it is not exactly known since when it was available, however it is guranteed available from Firefox 28 and up.

Syntax

void Components.utils.importGlobalProperties([string1, string2, ...]);

Parameters

[string1, string2, ...]
An array of strings. Each string is the name of an object to import.

Example

Components.utils.import("resource://gre/modules/devtools/Console.jsm");

Components.utils.importGlobalProperties(["atob", "btoa"]);
var encoded = btoa("Hello");
console.log(encoded);               // "SGVsbG8="
console.log(atob(encoded));         // "Hello"

Alternative Methods

If importGlobalProperties does not support the targeted Firefox version, here are some alternative methods to import these objects.

Import from JSM

This method worked until Blob and File were no longer apart of JSM modules. It is suspected this works up till Firefox 35.

This imports Blob, File, along with the regular Services. The reason this works is because JS Code Modules actually have Blob and File. Cu.import() will return the full global of a code module. Knowing that, we can just get a valid Blob by importing a known module that has the objects, such as Services.jsm

const {Blob, File, Services} = Cu.import("resource://gre/modules/Services.jsm", {});

Reference: Stackoverflow :: Use Blob on firefox add-on

hiddenDOMWindow

The hidden DOMWindow has all of these objects automatically imported. The downside of using hiddenDOMWindow is that on startup of Firefox, the hiddenDOMWindow objects cannot be accessed until it is fully loaded. Therefore readyState must be checked, if it is not complete, then a load listener must be attached. Once readyState is complete then the objects can be used.

For example this is how to ues the File object.

var domfile = Services.appShell.hiddenDOMWindow.File('/path/to/file');

XPCOM Components

Some objects have an XPCOM alternative, which typically allows more flexibility then the DOM version

Here is an example of how to use the DOM XMLHttpRequest through XPCOM interface of nsIXMLHttpRequest:

var oReq = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);