nsISocketTransportService using Firefox addon sdk

折月煮酒 提交于 2019-12-21 20:15:22

问题


I'm trying to use Firefox to read the SSH banner. ie. when you initially connect to an SSH server the server sends you its banner, identifying the server software and you send the SSH server your banner, identifying your client software.

To do so I'm using the example at this URL:

Is there nsiClientSocket in firefox extension javascript?

Here's my code:

'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!");
};

When I do cfx xpi with that I get this:

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).

So I try this instead:

'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. I make Components components (lowercase) and Components.interfaces just interfaces. But then I get this error on the console:

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

Any ideas?


回答1:


You should use Cc instead of Components.classes and Ci instead of Components.interfaces - these are the variables you imported from the chrome module and they are defined. Should you ever need to use the Components object itself (unlikely) you can import it as well:

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

For reference: chrome authority



来源:https://stackoverflow.com/questions/12558983/nsisockettransportservice-using-firefox-addon-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!