Labs/JS Modules: Difference between revisions

not out of date => now out of date
(document the observe/ignore API of the Preferences module)
(not out of date => now out of date)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Back to [[Labs]].
'''The modules described here have been superseded by the [https://addons.mozilla.org/en-US/developers/docs/sdk/latest/ Addon-SDK]. What is described on this page is now out of date.'''


Here you'll find a collection of modules which you can import into your extension.  The goal is to make extension development easier by implementing common functionality as reusable libraries.  If you would like to contribute a new module, get in touch with us at #labs!
Here you'll find a collection of modules which you can import into your extension.  The goal is to make extension development easier by implementing common functionality as reusable libraries.  If you would like to contribute a new module, get in touch with us at #labs!
Line 11: Line 11:


= Modules =
= Modules =
== JSON ==
The [http://hg.mozdev.org/jsmodules/file/tip/JSON.js JSON module] wraps the incompatible Gecko 1.9.0 (Firefox 3.0) and Gecko 1.9.1 (Firefox 3.5) JSON APIs, presenting the Gecko 1.9.1 API on both versions, for extensions that support multiple versions of Gecko-based applications.
Import this module into your extension to parse and stringify JSON in both Firefox 3.0 and 3.5 (and other Gecko-based applications, like Thunderbird) without checking the application's version each time.
Note: don't import this into the global namespace!  If you do, you'll hork native application code that expects the Gecko 1.9.0 API.  Instead, import it into your own object like this:
let MyExtension = {
  JSON: null,
  ...
};
Components.utils.import("chrome://myextension/modules/JSON.js", MyExtension);
// Now MyExtension.JSON is an object implementing the Gecko 1.9.1 JSON API.
The Gecko 1.9.1 (Firefox 3.5) JSON API is documented in the article [https://developer.mozilla.org/En/Using_JSON_in_Firefox Using JSON in Firefox].


== Logging ==
== Logging ==
Line 55: Line 72:


Get it here:
Get it here:
* [http://hg.mozilla.org/labs/weave/index.cgi/file/tip/modules/log4moz.js log4moz.js] - Adorned
* [http://mxr.mozilla.org/services-central/source/fx-sync/services/sync/modules/log4moz.js log4moz.js] - Adorned
* [http://hg.mozilla.org/labs/weave/index.cgi/raw-file/tip/modules/log4moz.js log4moz.js] - Raw (right-click to save)
* [http://mxr.mozilla.org/services-central/source/fx-sync/services/sync/modules/log4moz.js?raw=1 log4moz.js] - Raw (right-click to save)


== Observers ==
== Observers ==
Line 210: Line 227:
  let hasFoo = Preferences.has("extensions.test.foo");
  let hasFoo = Preferences.has("extensions.test.foo");
  // hasFoo == true, if the pref has a default value
  // hasFoo == true, if the pref has a default value
To reset an entire branch of preferences, call <code>resetBranch</code>:
Preferences.resetBranch("extensions.test.");
// Any pref starting with "extensions.test." is now reset.


=== Locking and Unlocking ===
=== Locking and Unlocking ===
Line 291: Line 313:
   dump(string.key + " = " + string.value + "\n");
   dump(string.key + " = " + string.value + "\n");
  }
  }
== Sync ==
The [http://hg.mozdev.org/jsmodules/file/tip/Sync.js Sync module] provides a simple way to write asynchronous code in a sequential, callback-free fashion that looks like synchronous code. It also provides a sleep function written using its functionality.
Using the sleep function is really easy. Just import the module, which adds the Sync symbol to your namespace, then call its Sync.sleep method:
Components.utils.import("resource://path/to/Sync.js");
Sync.sleep(1000); // sleep 1000 milliseconds
You can create your own such "synchronous asynchronous" function by adding the Sync.sync method to the Function prototype and calling it wherever you would normally call your function. Your function must then accept a callback as its first argument that it calls once it is done with its asynchronous work.
Here's how to implement and then call your own sleep function:
Components.utils.import("resource://path/to/Sync.js");
Function.prototype.sync = Sync.sync;
function sleep(callback, milliseconds) {
  setTimeout(callback, milliseconds);
}
sleep.sync(1000); // sleep 1000 milliseconds


== URI ==
== URI ==
Confirmed users
52

edits