[英]OS.File in Firefox SDK Add-on
這是我第一次編寫Firefox附加組件。
我正在嘗試讀取我正在編寫的Firefox附件中包含的文本文件。
我正在遵循MDN的示例
let decoder = new TextDecoder(); // This decoder can be reused for several reads
let array = OS.File.read("file.txt"); // Read the complete file as an array
let text = decoder.decode(array); // Convert this array to a text
在我的main.js
我有以下代碼片段:
Components.utils.import("resource://gre/modules/osfile.jsm");
var pathFile = OS.Path.join("_locales", "en", "messages.json");
let decoder = new TextDecoder('utf-8');
let promise = OS.File.read(pathFile);
promise = promise.then(
function onSuccess(array) {
return decoder.decode(array);
},
function onReject(array) {
console.log("onReject read: ");
}
);
當我使用cfx run
命令運行附加組件時,出現以下錯誤:
Message: ReferenceError: TextDecoder is not defined
我目前正在使用Firefox 30和Firefox附加SDK 1.16。
我是否應該在main.js
OS.File
中的main.js
中不使用OS.File
?
我應該使用FileUtils
來代替,即Components.utils.import("resource://gre/modules/FileUtils.jsm");
?
首先,您鏈接了OS.File
for Workers-docs。 您不是在工作線程中使用它(在任何地方都沒有new Worker
線程),而是在主線程中使用它,因此您將希望參考OS.File
作為主線程 -docs。
不幸的是,SDK模塊目前無法自動獲取TextEncoder
/ TextDecoder
。 正如@erikvold建議的那樣,您可以:
const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer');
甚至直接從osfile.jsm
獲取它:
const { Cc, Ci, Cu } = require("chrome");
const { OS, TextEncoder, TextDecoder } = Cu.import("resource://gre/modules/osfile.jsm", {});
現在回到您的原始問題:
通常, OS.File
可以在SDK附加組件中使用,並且是在文件系統中讀取/寫入任意文件的好方法。
然而OS.File
, FileUtils
, sdk/io/...
不適用於讀取文件中的插件,因為通常這些文件不會直接在用戶的文件系統中的位置,而是位於安裝的XPI內文件(僅是一個zip文件),這些API允許您逐字讀取文件,但不會為您解析容器zip文件(XPI)。
不過,您有兩種選擇:
l10n
模塊 。 self.data.load()
讀取位於加載項的data/
文件夾中的東西,但是您仍然需要解碼/解析數據。 XMLHttpRequest
讀取打包在附件(XPI)中的文件,SDK在net/xhr
模塊中提供了該文件。 另外,您可以通過設置適當的.responseType
值來讓XHR
對象為您解碼和/或解析文件。 如果要讀取的文件位於附加組件的data/
文件夾中,則為XHR
構造URI很容易。
cont self = require("sdk/self");
var uri = self.data.url("en/messages.json");
// would map to data/en/messages.json
否則,實際上並沒有獲得官方支持的獲取URI的方法,但是例如,以下將適用於lib/
(目前,但將來可能會失敗)。
cont self = require("sdk/self");
var uri = self.data.url("../lib/main.js");
// would map to data/../lib/main.js -> lib/main.js
將您自己的文件放置在其他位置比較棘手,因為SDK只會在構建XPI時打包某些路徑。 通常是lib/
, data/
, locale/
(請參見l10n
), chrome/
(請參見XUL遷移指南 )
這是使用XHR
一個最小示例(但請記住,對於本地化已經有l10n
模塊):
const self = require("sdk/self");
const { XMLHttpRequest } = require("sdk/net/xhr");
// Assume data/some.json is:
// {"abc":123}
var req = new XMLHttpRequest();
req.onload = function() {
// req.response is an object containing the parsed JSON.
console.log(req.response.abc); // 123
};
req.open("GET", self.data.url("some.json"));
req.responseType = "json";
req.send();
PS:當工作OS.File
特別是和承諾在一般情況下,可以考慮使用Task.jsm
,它可以使你更容易編寫和重新閱讀(保持)代碼。
嘗試
const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer');
const { OS } = require("resource://gre/modules/osfile.jsm");
var pathFile = OS.Path.join("_locales", "en", "messages.json");
let decoder = new TextDecoder('utf-8');
let promise = OS.File.read(pathFile);
promise = promise.then(function onSuccess(array) {
return decoder.decode(array);
}, function onReject(array) {
console.log("onReject read: ");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.