使用 Mozilla Add-on SDK 中的 XPCOM 组件

发布于 2024-12-09 12:33:33 字数 407 浏览 0 评论 0原文

我过去曾从事 Mozilla 插件开发(初级水平)。但当遇到 XPCOM 时,我真的很害怕,所以就放弃了。

最近我才遇到了Add-on SDK,发现它真的很酷,而且看到之前花费几个月的工作与SDK的工作时间不超过2天,这真的很令人着迷。现在我又陷入了 XPCOM 模块的困境。

现在我真的很想利用 XPCOM 的强大功能,但是我至少需要一周的时间才能在 SDK 的背景下熟悉 XPCOM。我需要什么代码才能获得我想要的功能?

  1. 对于每个用户会话,我想记录一些内容。我能够区分会话。我现在想要的是在用户计算机中创建文件的代码,从加载项打开它并在其中写入一些内容。

  2. 用于访问书签和下载并阅读它们的代码。

如果我必须从 SDK 切换回来,那真的会是一个令人心碎的时刻。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-12-16 12:33:34

chrome 为您提供完整的 XPCOM 访问权限。对于文件访问,最好使用 FileUtils 模块

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();

导入模块的语法有些不寻常,这是由于 bug 683217。请注意,FileUtils.openFileOutputStream() 仅从 Firefox 7 开始可用,如果您想附加到文件,则 FileUtils.openSafeFileOutputStream() 不可用。

对于书签访问,您可以使用常用的代码片段,开头为:

var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
              .getService(Ci.nsINavBookmarksService);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文