如何在 Firefox 版本 45+ 的附加组件中读取本地文件
How to read a local file in an add-on for Firefox version 45+
我花了 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));
});
不可能强制上面的代码运行 (((。我做错了什么?
你写的代码基本上可以正常工作。但是,这假定已定义 alert()
并且在您尝试读取文件时没有错误。但是,通常不会定义 alert()
,除非您在代码的其他地方定义了它。如果您查看 Browser Console (Ctrl-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
我花了 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));
});
不可能强制上面的代码运行 (((。我做错了什么?
你写的代码基本上可以正常工作。但是,这假定已定义 alert()
并且在您尝试读取文件时没有错误。但是,通常不会定义 alert()
,除非您在代码的其他地方定义了它。如果您查看 Browser Console (Ctrl-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