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?
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.
Code to access the bookmarks and 开发者_Python百科downloads and to read them.
It would really be a heart-breaking moment if I would have to switch back from SDK.
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);
精彩评论