3

我正在尝试使用 Firefox 来阅读 SSH 横幅。IE。当您最初连接到 SSH 服务器时,服务器会向您发送横幅,标识服务器软件,然后您向 SSH 服务器发送横幅,标识客户端软件。

为此,我使用此 URL 中的示例:

Firefox 扩展 javascript 中是否有 nsiClientSocket?

这是我的代码:

'use strict';

const {Cc,Ci} = require("chrome")
const prefs = require("simple-prefs");


exports.main = function(options,callbacks) {
    var transport = Components.classes["@mozilla.org/network/socket-transport-service;1"]
                              .getService(Components.interfaces.nsISocketTransportService)
                              .createTransport(null, 0, "localhost", 22, null);

    //var output = transport.openOutputStream(0, 0, 0);
    var input = transport.openInputStream(0, 0, 0);
    var data = "test";
    dump(stream.read());
    //stream.write(data, data.length);
    //stream.close();
    dump("all done!");
};

当我用它做 cfx xpi 时,我得到了这个:

The following lines from file C:\path\to\lib\main.js:
   8: var transport = Components.classes["@mozilla.org/network/socket-transport-service;1"]
   9: .getService(Components.interfaces.nsISocketTransportService) use 'Components' to access chrome authority. To do so, you need to add a line somewhat like the following:

  const {Cc,Ci} = require("chrome");

Then you can use any shortcuts to its properties that you import from the 'chrome' module ('Cc', 'Ci', 'Cm', 'Cr', and 'Cu' for the 'classes', 'interfaces', 'manager', 'results', and 'utils' properties, respectively. And `components` for `Components` object itself).

所以我试试这个:

'use strict';

const {Cc,Ci} = require("chrome")
const prefs = require("simple-prefs");


exports.main = function(options,callbacks) {
    var transport = components.classes["@mozilla.org/network/socket-transport-service;1"]
                              .getService(interfaces.nsISocketTransportService)
                              .createTransport(null, 0, "localhost", 22, null);

    //var output = transport.openOutputStream(0, 0, 0);
    var input = transport.openInputStream(0, 0, 0);
    var data = "test";
    dump(stream.read());
    //stream.write(data, data.length);
    //stream.close();
    dump("all done!");
};

IE。我制作 Components 组件(小写)和 Components.interfaces 只是接口。但是后来我在控制台上收到了这个错误:

    var transport = components.classes["@mozilla.org/network/socket-transport-service;1"]
ReferenceError: components is not defined

有任何想法吗?

4

1 回答 1

13

您应该使用Cc代替Components.classesCi代替Components.interfaces- 这些是您从chrome模块导入的变量并且它们已定义。如果您需要使用Components对象本身(不太可能),您也可以导入它:

const {components, Cc, Ci} = require("chrome");

供参考:chrome权威

于 2012-09-25T19:22:26.857 回答