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



我试图导入一个.jsm文件(从这个github)称为"WebSocketServer。jsm"在我的Firefox扩展。情况如下:

    jsm文件位于modules目录下,路径如下:

root_of_extension/modules/WebSocketServer.jsm

    我已经添加到我的chrome浏览器。

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

第6行。进口"。你有什么主意吗?

把它放到你的data文件夹中,而不是像在data文件夹中获取任何其他文件那样获取路径。

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

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

首先是结构:

$ 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);
};

相关内容

  • 没有找到相关文章

最新更新