【问题标题】:How to read a local file in an add-on for Firefox version 45+如何在 Firefox 45+ 的附加组件中读取本地文件
【发布时间】:2016-09-05 20:15:36
【问题描述】:

我花了 4 个小时试图找到将文件加载到我的 Firefox 插件中的解决方案。但是,没有成功(((。

我的代码:

const {TextDecoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
var decoder = new TextDecoder();
var promise = OS.File.read("C:\\test.txt");
promise = promise.then(function onSuccess(array) {
    alert(decoder.decode(array));
});

上面的代码无法强制运行(((。我做错了什么?

【问题讨论】:

标签: javascript firefox-addon xul firefox-addon-overlay


【解决方案1】:

您拥有的代码基本上可以按照编写的方式工作。但是,这假定已定义 alert() 并且在您尝试读取文件时没有错误。但是,alert() 通常不会被定义,除非您在代码中的其他地方定义了它。如果您查看Browser ConsoleCtrl-Shift-J,或 Cmd-Shift-J 在 OSX 上)。很遗憾,您没有在问题中包含该信息。

参考资料:

下面是一个完整的 Firefox Add-on SDK 扩展,它两次读取并输出到控制台B:\testFile.txt 的文件。下面包含的示例文本文件在控制台中的输出是:

read-text-file:readTextFile: This is a text file line 1
Line 2
read-text-file:readUtf8File: This is a text file line 1
Line 2

index.js

var {Cu} = require("chrome");
//Open the Browser Console (Used in testing/developmenti to monitor errors/console)
var utils = require('sdk/window/utils');
activeWin = utils.getMostRecentBrowserWindow();
activeWin.document.getElementById('menu_browserConsole').doCommand();
var buttons     = require('sdk/ui/button/action');
var button = buttons.ActionButton({
    id: "doAction",
    label: "Do Action",
    icon: "./myIcon.png",
    onClick: doAction
});
//Above this line is specific to the Firefox Add-on SDK
//Below this line will also work for Overlay/XUL and bootstrap/restartless add-ons
//const Cu = Components.utils; //Uncomment this line for Overlay/XUL and bootstrap add-ons
const { TextDecoder, OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
function doAction(){
    var fileName = 'B:\\textFile.txt'
    //Read the file and log the contents to the console.
    readTextFile(fileName).then(console.log.bind(null,'readTextFile:'))
                          .catch(Cu.reportError);
    //Do it again, using the somewhat shorter syntax for a utf-8 encoded file
    readUtf8File(fileName).then(console.log.bind(null,'readUtf8File:'))
                          .catch(Cu.reportError);
}
function readTextFile(fileName){
    var decoder = new TextDecoder();
    return OS.File.read(fileName).then(array => decoder.decode(array));
}
function readUtf8File(fileName){
    //Using the somewhat shorter syntax for a utf-8 encoded file
    return OS.File.read(fileName, { encoding: "utf-8" }).then(text => text);
}

package.json

{
    "title": "Read a text file",
    "name": "read-text-file",
    "version": "0.0.1",
    "description": "Reads a text file in two different ways.",
    "main": "index.js",
    "author": "Makyen",
    "engines": {
        "firefox": ">=38.0a1",
        "fennec": ">=38.0a1"
    },
    "license": "MIT",
    "keywords": [
        "jetpack"
    ]
}

textFile.txt

This is a text file line 1
Line 2

【讨论】:

  • @Greccy,强烈建议不要这样做。我没有尝试在主 UI 线程上以这种方式使用它。如果您在工作线程中使用它,则可以以这种方式使用 OS.File。 MDN has describing why it should be asynchronous is 的文本:“所有开发人员需要记住的一件事是文件 I/O 操作的持续时间是无限的。[继续]
  • @Greccy, [续] 取决于内核当前负载、当前磁盘活动、总线当前负载、磁盘当前转速、电池电量等. 操作可能需要任意时间。我们正在谈论几秒钟来执行看起来微不足道的操作,例如关闭文件或检查文件的最后修改时间。如果在主线程上调用操作,这意味着整个用户体验会卡住几秒钟,这是相当糟糕的。”
  • 这段代码是非 xul 的吗?因为我用 xul 做的只有截止按钮 doAction();我的浏览器变得迟钝。我打开附加组件选项卡并没有加载任何内容)))
  • 我不知道你这个问题是什么意思。代码是 JavaScript(一个完整的基于 Firefox Add-on SDK 的插件。XUL 与 HTML 类似,因为它描述了元素的组织。HTML 和 XUL 都与 JavaScript 一起工作。(我刚刚看到你的评论,你是使用开发基于叠加的附加组件)。您将不需要使用代码的某些部分。我将编辑以使需要的部分更加清晰。
  • @Greccy,您不会使用 package.json 文件,该文件特定于 Firefox Add-on SDK。您可以使用 index.js 文件中以 const Cu = Components.utils; 行开头和以下的所有内容(应取消注释,或代码中其他地方使用的类似行)。上面的行是特定于插件 SDK 的(这些行打开浏览器控制台并设置工具栏按钮,两者在覆盖/XUL 和引导/无重启插件中的完成方式有所不同)。
猜你喜欢
  • 2020-09-25
  • 1970-01-01
  • 2021-02-12
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 2013-01-04
相关资源
最近更新 更多