Labs/JS Modules: Difference between revisions

not out of date => now out of date
(document the new JSON module)
(not out of date => now out of date)
 
(7 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 14: Line 14:
== JSON ==
== JSON ==


The [http://hg.mozdev.org/jsmodules/file/tip/JSON.js JSON module] wraps the incompatible Firefox 3.0 and 3.5 JSON APIs, presenting the 3.5 API on both versions, for extensions that support both versions of Firefox.  Import this module to parse and stringify JSON in both 3.0 and 3.5 without checking the application's version each time.
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.


Note: don't import this into the global namespace!  If you do, you'll hork native Firefox 3.0 code that expects the 3.0 API.  Instead, import it into your own object like this:
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 = {
  let MyExtension = {
Line 23: Line 25:
  };
  };
  Components.utils.import("chrome://myextension/modules/JSON.js", MyExtension);
  Components.utils.import("chrome://myextension/modules/JSON.js", MyExtension);
  // Now MyExtension.JSON is an object implementing the Firefox 3.5 JSON API.
  // Now MyExtension.JSON is an object implementing the Gecko 1.9.1 JSON API.


This module also works in Thunderbird 3.0, which uses Firefox 3.5's 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].
 
The 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 72: 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 313: 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