用firefox sdk运行linux命令

时间:2015-01-15 13:10:29

标签: firefox-addon firefox-addon-sdk

我想用firefox sdk运行Linux命令。

我已经搜索过了,但所有示例都描述了如何在Windows中运行可执行文件。我需要运行Linux命令。

3 个答案:

答案 0 :(得分:3)

以这种方式使用nsIProcess

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

// create an nsIFile for the executable
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.initWithPath("/usr/bin/du");

// create an nsIProcess
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);

// Run the process.
// If first param is true, calling thread will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var args = ["-h"];
process.run(false, args, args.length);

答案 1 :(得分:1)

Add-On SDK现在有自己的方式来创建适用于所有平台的子进程:https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/system_child_process

以下是一个例子:

var child_process = require("sdk/system/child_process");

var ls = child_process.spawn('/bin/ls', ['-lh', '/usr']);

ls.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ls.on('close', function (code) {
  console.log('child process exited with code ' + code);
});

答案 2 :(得分:0)

此主题适用于此:ask.m.o :: X11: Getting output of nsIProcess

nsIProcess

@matagus是对的,您可以使用nsIProcess启动该文件。

子进程(在其中使用js-ctypes)

您可以使用subrpcoess.jsm:

https://github.com/ochameau/jetpack-subprocess

const subprocess = require("subprocess");
var p = subprocess.call({
  command:     'echo',

  // Print stdin and our env variable
  arguments:   ['$@', '$ENV_TEST'],
  environment: ['ENV_TEST=OK'],

  stdin: subprocess.WritablePipe(function() {
    this.write("stdin");
    this.close();
  }),
  stdout: subprocess.ReadablePipe(function(data) {
    // data should be equal to: "stdin OK"

  }),
  stderr: subprocess.ReadablePipe(function(data) {

  }),
  onFinished: subprocess.Terminate(function() {

  }),
  mergeStderr: false
});

js-ctypes popen

您还可以使用jsctypes和popen

GitHubGIST :: Noitidart / _ff-addon-snippet-LibC_popen.js

阅读要点的自述文件。

在下面的示例中我正在做readlink。保持osfile.jsm导入为用于检查mac或linux的那些

Cu.import('resource://gre/modules/ctypes.jsm');
Cu.import('resource://gre/modules/osfile.jsm');

function doit() {
    try {
        if (OS.Constants.Sys.Name == 'Darwin') {
            var _libc = ctypes.open('libc.dylib');
        } else if (OS.Constants.Sys.Name == 'Linux') {
            var _libc = ctypes.open('libc.so.6');
        }
    } catch (e) {
        try {
            var libName = ctypes.libraryName('libc');
        } catch (e) {
            console.error('Integration: Could not get libc name; not activating');
            return;
        }

        try {
            _libc = ctypes.open(libName);
        } catch (e) {
            console.error('Integration: Could not open ' + libName + '; not activating');
            return;
        }
    }

    var popen = _libc.declare('popen', ctypes.default_abi, ctypes.voidptr_t, // Return int
        ctypes.char.ptr, // Param1 const char *command
        ctypes.char.ptr // Param1 const char *command
    );

    var fread = _libc.declare('fread', ctypes.default_abi, ctypes.size_t, // Return int
        ctypes.voidptr_t,
        ctypes.size_t,
        ctypes.size_t,
        ctypes.voidptr_t
    );

    var pclose = _libc.declare('pclose', ctypes.default_abi, ctypes.int, // Return int
        ctypes.voidptr_t
    );

    ////////////////////////
    ////END DECLARATIONS
    ////////////////////////

    //var rez = popen('readlink -q "/home/noi/.mozilla/firefox/q0rlb7ap.Unnamed Profile 1/.parentlock"', 'r')
    //console.log('rez:', rez.toString())
    var lockFilePath = OS.Path.join(OS.Constants.Path.profileDir, 'lock');
  //var lockFilePath = '/home/noi/.mozilla/firefox/q0rlb7ap.Unnamed Profile 1/lock';
  //var lockFilePath = '/home/noi/.mozilla/firefox/q0rlb7ap.Unnamed Profile 1/.parentlock';
  console.log(lockFilePath);

    /*
  var promise = OS.File.read(OS.Path.join(OS.Constants.Path.profileDir, '.parentlock'), {encoding:'utf-8'});
  promise.then(
   function(aVal) {
     console.log('read val:', aVal);
   },
    function(aReason) {
    console.log('failed for aReason:', aReason, aReason.becauseNoSuchFile);
    }
  )

    //on linux it reads .parentlock, and if you try to read lock it returns aReason.becauseNoSuchFile == true
    */

  console.time('popen');
    var file = popen('readlink -q "' + lockFilePath + '"', 'r')
  console.log(file.toString())
    var bufferSize = 1000;
    var buffer = ctypes.char.array(bufferSize)('');
    var size = bufferSize;
    var outList = [];
    while (size == bufferSize) {
        size = fread(buffer, 1, bufferSize, file);
        outList.push(buffer.readString().substring(0, size));
    }
    pclose(file);
    console.timeEnd('popen')
    console.log(outList.join(''));


}

doit();