7

I have worked on Mozilla Add-on Development in the past (beginner level). But on encountering XPCOM, I got really scared and left it in the middle.

Recently only I encountered Add-on SDK and found it to be really cool, and it was really fascinating to see that the work that took months before was not more than 2 days work with the SDK. Now again I am stuck on the XPCOM module.

Now I really want to exploit the power of XPCOM but it will take me at least a week to get familiar with XPCOM in the context of SDK. What code do I need to obtain the functionality that I desire?

  1. For each user session, I want to log something. I am able to differentiate sessions. What I want now is the code to create a file in the user's machine, open it from the add-on and write something onto it.

  2. Code to access the bookmarks and downloads and to read them.

It would really be a heart-breaking moment if I would have to switch back from SDK.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shatu
  • 1,819
  • 3
  • 15
  • 27
  • How did you manage to get the _hello world_ work? Would you mind to share some references with me? What I always get is `Cc['my contract id'] is undefined`. You might get some rep if you kindly answer [here](http://stackoverflow.com/questions/8477794/firefox-xpcom-hello-world-typeerror-cc-is-undefined). – Dante May Code Dec 19 '11 at 13:02

1 Answers1

7

The chrome package gives you full XPCOM access. For file access it is best to use the FileUtils module:

var {Cc, Ci, Cu} = require("chrome");
var {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm");
var file = FileUtils.getFile("ProfD", ["mylogfile.txt"]);
var stream = FileUtils.openFileOutputStream(...);
stream.write(data, data.length);
stream.close();

The somewhat unusual syntax to import the module is due to bug 683217. Note that FileUtils.openFileOutputStream() is only available starting with Firefox 7 and FileUtils.openSafeFileOutputStream() isn't usable if you want to append to a file.

For bookmark access you use the usual code snippets, starting with:

var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
              .getService(Ci.nsINavBookmarksService);
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • I get this exception on running your code.. **TypeError: FileUtils.getFile is not a function** Does it require anything else to be included in the directory structure of Add-on? I just simple copy pasted your code with **file** variable being passed to **openFileOutputStream** function. – Shatu Oct 11 '11 at 17:59
  • @Shatu: Sorry, my bad - so much about posting code without testing it. `Cu.import()` doesn't return the `FileUtils` object but rather an object with `FileUtils` as a property. Fixed my code in the answer, also the parameters of `FileUtils.getFile()`. – Wladimir Palant Oct 11 '11 at 18:09