如何从Thunderbird WebExtension执行/访问本地文件



我喜欢写一个Thunderbird插件来加密东西。为此,我已经从compose窗口中提取了所有数据。现在,我必须将其保存到文件中,并

运行本地可执行文件我找到了文件和目录条目API文档,但它似乎不起作用。在尝试使用以下代码获取对象时,我总是得到未定义

var filesystem = FileSystemEntry.filesystem;
console.log(filesystem); // --> undefined

至少,我可以检查是否有一个正在工作的AddOn,以了解它是如何工作的,也许我可以在manifest.json中请求什么权限?

注意:必须跨平台工作(Windows和Linux(。

答案是,WebExtensions当前无法执行本地文件。此外,也无法保存到磁盘上的某个本地文件夹。

相反,您需要在项目中添加一些WebExtension实验,然后使用遗留API。在那里,您可以使用IOUtilsFileUtils扩展来实现您的目标:

执行文件:

在您的后台JS文件中:

var ret = await browser.experiment.execute("/usr/bin/executable", [ "-v" ]);

在实验中,你可以这样执行:

var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
var { FileUtils }       = ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
var { XPCOMUtils }      = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["IOUtils");
async execute(executable, arrParams) {
var fileExists = await IOUtils.exists(executable);
if (!fileExists) {
Services.wm.getMostRecentWindow("mail:3pane")
.alert("Executable [" + executable + "] not found!");
return false;
}
var progPath = new FileUtils.File(executable);
let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(progPath);
process.startHidden = false;
process.noShell = true;
process.run(true, arrParams, arrParams.length);
return true;
},

将附件保存到磁盘:

在你的后台JS文件中,你可以这样做:

var f = messenger.compose.getAttachmentFile(attachment.id)
var blob = await f.arrayBuffer();
var t = await browser.experiment.writeFileBinary(tempFile, blob);

在实验中,你可以这样写文件:

async writeFileBinary(filename, data) {
// first we need to convert the arrayBuffer to some Uint8Array
var uint8 = new Uint8Array(data);
uint8.reduce((binary, uint8) => binary + uint8.toString(2), "");
// then we can save it
var ret = await IOUtils.write(filename, uint8);
return ret;
},

欠条文档:

https://searchfox.org/mozilla-central/source/dom/chrome-webidl/IOUtils.webidl

FileUtils文档:

https://searchfox.org/mozilla-central/source/toolkit/modules/FileUtils.jsm

最新更新