Mozilla加载项SDK-OS.File.read(系统找不到指定的文件。)

Mozilla加载项SDK-OS.File.read(系统找不到指定的文件。),sdk,firefox-addon,mozilla,Sdk,Firefox Addon,Mozilla,我根据Mozilla教程创建了一个附加组件cfx测试和cfx运行工作正常。我将文件夹放在桌面上,并使用cfx xpi和Ctrl+o(xpi文件)将插件安装到Firefox中 现在,加载项在控制台日志中给出了以下错误: 打开文件purls.txt时出现Win错误2(系统无法 查找指定的文件。) 以下是main.js中的相关代码: const {TextEncoder, TextDecoder, OS} = Cu.import("resource://gre/modules/osfile.jsm",

我根据Mozilla教程创建了一个附加组件
cfx测试
cfx运行
工作正常。我将文件夹放在桌面上,并使用
cfx xpi
和Ctrl+o(xpi文件)将插件安装到Firefox中

现在,加载项在控制台日志中给出了以下错误:

打开文件purls.txt时出现Win错误2(系统无法 查找指定的文件。)

以下是main.js中的相关代码:

const {TextEncoder, TextDecoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
let decoder = new TextDecoder();
let promise = OS.File.read("purls.txt").then(
    function onSuccess(array) {
        // Do stuff
    },
    function onReject(reason) {
        console.error("Couldn't read from purls.txt:\n"+reason);
    }
);

请帮我弄清楚发生了什么事。当我使用
cfx run
时,插件会找到purls.txt文件并打开它/从中读取,没有问题。

您没有给它一个指向purls.txt的路径,因此它可能在您的驱动器上查找purls.txt。它在桌面上吗?然后创建到桌面的路径。请参见
pathToPurls

const {TextEncoder, TextDecoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
let decoder = new TextDecoder();
let pathToPurls = OS.Path.join(OS.Constants.Path.desktopDir, "purls.txt");
console.log('pathToPurls:', pathToPurls);
let promise = OS.File.read(pathToPurls).then(
    function onSuccess(array) {
        // Do stuff
    },
    function onReject(reason) {
        console.error("Couldn't read from purls.txt:\n"+reason);
    }
);

我认为这是有效的,因为我没有收到错误,但我也没有在浏览器控制台中看到console.log输出。我在onSuccess函数中添加了console.log,但在浏览器控制台中也没有看到。同样,在我运行
cfx run
时,它们都会出现,而不是在安装之后。有修复方法吗?我收回-它显然在工作。谢谢我在文档中看到,安装后不显示console.log消息是出于设计。请尝试在onSuccess中使用console.error,您应该会看到它。我不知道为什么插件sdk有不同的日志级别。这可能是因为人们使用console发布代码。其中包含日志,这会在将其转储到日志时大大降低性能。