簡體   English   中英

Firefox SDK附加組件中的OS.File

[英]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.FileFileUtilssdk/io/...不適用於讀取文件中的插件,因為通常這些文件不會直接在用戶的文件系統中的位置,而是位於安裝的XPI內文件(僅是一​​個zip文件),這些API允許您逐字讀取文件,但不會為您解析容器zip文件(XPI)。

不過,您有兩種選擇:

  • 文件名似乎表明您要本地化某些內容 為此,SDK附帶了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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM