javascript - 如何从 SDK 插件数据文件夹加载 dll?

标签 javascript firefox-addon firefox-addon-sdk

我们使用 Web IDE 来创建插件。我的 test.dll 位于数据文件夹中。如何通过js-ctypes加载它?

使用像“c:\test.dll”这样的绝对路径没有问题,但我不能使用这个路径来分发它。

var lib = ctypes.open("c:\\test.dll"); 
// works but how i get path to addon inner data directory?

最佳答案

我在这里为您提供阻力最小的方法...还有其他方法,例如从安装的 XPI 中手动解压 DLL,但这过于广泛、容易出错且复杂。

  1. 您需要在 package.json 中定义 "unpack": true,以便 XPI 在安装时解压。
  2. 您需要使用self.data.url()和各种其他工具来找出DLL文件的实际路径。在成为文件 URI 之前,URI 可能会多次包装在“resource:”和/或“chrome:”URI 中。所以这也需要解开。

    const {Cc, Cu, Ci} = require("chrome");
    Cu.import("resource://gre/modules/Services.jsm");
    const ResProtocolHandler = Services.io.getProtocolHandler("resource").
                               QueryInterface(Ci.nsIResProtocolHandler);
    const ChromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].
                           getService(Ci.nsIChromeRegistry);
    
    function resolveToFile(uri) {
      switch (uri.scheme) {
        case "chrome":
          return resolveToFile(ChromeRegistry.convertChromeURL(uri));
        case "resource":
          return resolveToFile(Services.io.newURI(ResProtocolHandler.resolveURI(uri), null, null));
        case "file":
          return uri.QueryInterface(Ci.nsIFileURL).file;
        default:
          throw new Error("Cannot resolve");
      }
    }
    
    const {data} = require("self");
    let dll = data.url("test.dll");
    dll = resolveToFile(Services.io.newURI(dll, null, null));
    console.log(dll.path); // dll.path is the full, platform-dependent path for the file.
    

关于javascript - 如何从 SDK 插件数据文件夹加载 dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19382201/

相关文章:

javascript - Angular : Variables in ng-model

javascript - 火狐插件 : Access Variable in other File

javascript - Firefox Add-on-SDK page-mod 匹配模式/通配符错误

firefox-addon - 如何在 Add-on SDK 中访问窗口和文档

javascript - Mongoose 无限循环模型创建

javascript - 在 Javascript 中计算类列表的值时遇到问题

javascript - 单击时有反应的 Firefox 插件(左)

firefox-addon - zip 糊涂了。插件似乎已损坏

HTML:无法使表格单元格中的 URL 符合我想要的方式

javascript - 按年龄限制访问 FB 应用程序