Using IndexedDB in chrome

The indexedDB API is typically used to store data in the user's browser from content JavaScript.  (See Using IndexedDB for an overview.)  However, the APIs can be also be accessed from system-privileged JavaScript using the Components.utils.importGlobalProperties() function:


// From here on, it's like using IndexedDB from content
var req ="my-database");
// ...

If you are creating a sandbox, and want indexedDB to be available in it, use the wantGlobalProperties option in the Sandbox constructor:

var options = {
  "wantGlobalProperties": ["indexedDB"]           
var principal = Cc[";1"].createInstance(Ci.nsIPrincipal);
var sandbox = Components.utils.Sandbox(principal, options);

// The sandbox will have access to indexedDB
var sandboxScript = 'var req ="my-database");';
Components.utils.evalInSandbox(sandboxScript, sandbox);

Before Firefox 33, you would access indexedDB from chrome code using the initWindowless method of the nsIIndexedDatabaseManager service. This method was removed in Firefox 33.


© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.