在Firefox扩展中导入.jsm文件(带附件sdk)

时间:2014-07-23 07:18:53

标签: javascript firefox-addon firefox-addon-sdk jsm

我试图导入名为" WebSocketServer.jsm"的.jsm文件(来自This github)。在我的Firefox扩展中。以下情况:

  • .jsm文件位于名为modules的目录中,其路径为:
  

root_of_extension/modules/WebSocketServer.jsm

  • 我已将此行添加到我的chrome.manifest文件中:
  

resource external_components modules/

  • 我在我的代码中插入了导入文件的命令:
  

Cu.import("资源://external_components/WebSocketServer.jsm");

问题是从控制台我看到以下错误

console.error: 
Object
- message = Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]
- fileName = undefined
- lineNumber = 6
- stack = @undefined:6:undefined|CuddlefishLoader/options<.load@resource://gre/modules/commonjs/sdk/loader/cuddlefish.js:129:9|run@resource://gre/modules
/commonjs/sdk/addon/runner.js:149:1|startup/<@resource://gre/modules/commonjs/sdk/addon/runner.js:113:7|resolve@resource://gre/modules/commonjs
/sdk/core/promise.js:118:11|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/
promise.js:43:43|then@resource://gre/modules/commonjs/sdk/core/promise.js:153:9|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/
promise.js:43:43|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:118:11|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/
promise.js:43:43|then@resource://gre/modules/commonjs/sdk/core/promise.js:153:9|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/
promise.js:43:43|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/promise.js:43:43|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/
promise.js:43:43|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/promise.js:43:43|resolve@resource://gre/modules/commonjs/sdk/core/
promise.js:185:11|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:118:11|then@resource://gre/modules/commonjs/sdk/core/
promise.js:43:43|resolve@resource://gre/modules/commonjs/sdk/core/promise.js:185:11|readAsync/<@resource://gre/modules/commonjs/
sdk/net/url.js:49:9|NetUtil_asyncOpen/<.onStopRequest@resource://gre/modules/NetUtil.jsm:137:17|
- toString = function () /* use strict */ toString

&#34; Cu.import&#34;中的第6行线。你有什么想法吗?

2 个答案:

答案 0 :(得分:2)

只需将其放在数据文件夹中,就像获取数据文件夹中任何其他文件一样。

var {Cu} = require('chrome');
const self = require('sdk/self');    
Cu.import(self.data.url('modules/WebSocket.jsm'));

不需要弄清楚我很清楚。

答案 1 :(得分:1)

这是我刚刚测试过的解决方案。

首先是结构:

$ ls -lR
total 8
drwxr-xr-x  3 maierman  staff  102 23 Jul 15:45 data
drwxr-xr-x  3 maierman  staff  102 23 Jul 15:47 lib
-rw-r--r--  1 maierman  staff  164 23 Jul 15:33 package.json

./data:
total 40
-rw-------  1 maierman  staff  17925 23 Jul 15:45 WebSocketServer.jsm

./lib:
total 8
-rw-r--r--  1 maierman  staff  874 23 Jul 15:47 main.js

现在,main.js

const self = require("sdk/self");
const {Cc, Ci, Cu} = require("chrome");

const wssuri = self.data.url("WebSocketServer.jsm");

Cu.import(wssuri);

// Create an echo server
var clients = [];
var server;

exports.onUnload = function() {
  try {
    server.close();
    clients.slice().forEach(function(c) {
      try {
        c.close();
      }
      catch (cex) {
        console.debug("Failed to disconnect client " + client, cex);
      }
    });
  }
  catch (ex) {
    console.debug("Failed to disconnect server", ex);
  }
  Cu.unload(wssuri);
};

server = new WebSocketServer(12345);
server.onclient = function(client) {
  console.log(client + " connected");
  clients.push(client);

  client.onmessage = function(client, msg) {
    console.log(client + " message:" + msg);
    client.send(msg);
  };

  client.onclose = function(client) {
    console.log(client + " disconnected");
    clients = clients.filter(function(c) {
      return c != client;
    });
  };
};
server.connect();

还要确保获得WebSocketServer.jsm的最新版本。我刚注意到并修复了一个错误,其中客户端断开连接未得到妥善处理(但这与您的问题无关)。

通过在Scratchpad中执行以下操作来测试它。

var ws = new WebSocket("ws://localhost:12345");
ws.onmessage = function(e) {
  console.log(e.data);
};
ws.onopen = function(e) {
  console.log("opened");
  ws.send("hello, world!");
};
ws.onerror = ws.onclose = function(e) {
  console.log(e.type);
};