【问题标题】:Thunderbird extension - How to use relative paths with Javascript?Thunderbird 扩展 - 如何在 Javascript 中使用相对路径?
【发布时间】:2014-03-05 14:46:50
【问题描述】:

我正在开发自己的 Thunderbird 扩展程序。 该扩展添加了一个 .xml 文件作为 Thunderbird 邮件的附件(效果很好)。 我唯一的问题是我不知道如何使用相对路径。

看起来像这样:

var file= 'C:\\...[… \\…]...\\chrome\\VHitG2.xml';
var attachments = [];
    attachments.push(FileToAttachment(file));
    AddAttachments(attachments); 

如果扩展安装在不同的路径中,扩展将无法工作。
有谁知道如何使用相对路径?

【问题讨论】:

  • 相对于什么?该文件是您的扩展包的一部分吗?
  • 是的,它是我扩展的一部分。会被其他人使用,所以 Thunderbird 的安装路径会有所不同。

标签: javascript thunderbird thunderbird-addon


【解决方案1】:

FileToAttachment() 函数没有魔法,它实际上是very simple。我假设您正在谈论作为扩展程序一部分的静态文件 - 它应该可以通过像 chrome://myextension/content/VHitG2.xml 这样的 URL 访问。然后,您可以使用该 URL 自己创建一个 nsIMsgAttachment 实例:

var attachment = Components.classes["@mozilla.org/messengercompose/attachment;1"]
                           .createInstance(Components.interfaces.nsIMsgAttachment);
attachment.url = "chrome://myextension/content/VHitG2.xml";
AddAttachments([attachment]);

请注意,您的扩展不需要为此解压安装,您不需要磁盘上的实际文件。

【讨论】:

  • 谢谢。我用attachment.url = "chrome://myextension@example.com/chrome/VHitG2.xml"; 试过了。 attachmentbucked 中填充了一个名为“chrome://myextension@example.com/chrome/VHitG2.xml”的文件,我无法打开此附件或发送带有附件的邮件(缺乏授权)。跨度>
  • @MSKV: chrome:// URL 不能这样工作,请阅读developer.mozilla.org/en/docs/Chrome_Registration
  • @Wladimir Palant:我将content attach chrome://myextension/content/VHitG.xml 添加到我的chrome.manifest 中,并且它有效!我现在的问题是我无法使用NetUtilFileUtils(以前工作)更改.xml 文件的内容。
  • 我不知道如何修改您的代码以获得nsIFile 而不是nsiMsgAttachmet
  • @MSKV:对于不是物理文件(它位于压缩存档中)的东西,您无法获得 nsIFile 实例。但是,如果您想要可以更改的内容,那么使用 data: URLs 可能会起作用。
【解决方案2】:

我使用了一种非常迂回的方式来获取扩展文件的 URL:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
var test1 = FileUtils.getFile("CurProcD", ["VHitG2.xml"]);
var test2 = FileUtils.getFile("CurProcD", ["VHitG.xml"]);
var file1 = test1.path.replace(/VHitG2.xml/i, "extensions\\custom-toolbar-button@example.com\\chrome\\VHitG2.xml");
var file2 = test2.path.replace(/VHitG.xml/i, "extensions\\custom-toolbar-button@example.com\\chrome\\VHitG.xml");
var attachment1 = file1.replace(/\\/g, "\\\\");
var attachment2 = file2.replace(/\\/g, "\\\\");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2011-04-26
    • 2011-09-12
    相关资源
    最近更新 更多