firefoxfirefox-addonfirefox-addon-sdk

Firefox command line arguments in extension


I'm trying to get an arguments of firefox command line in my extension:

'firefox.exe -viewapp url -another-command -blablala:somedata'

And here I have found an example

But I dont get it how can I get all arguments on first start of browser. I need to display it in web console something like this:

console.log(commandLine.arguments)
// -viewapp url -another-command -blablala:somedata

Please help


Solution

  • I see you just want to see what command line arguments it was launched with. I know a js-ctypes solution. That will require you to code for the different platforms. Here's a start, it gets you the arguments on windows and mac. I didnt do the linux part yet:

    Cu.import('resource://gre/modules/osfile.jsm');
    Cu.import('resource://gre/modules/ctypes.jsm');
    
    var lib 
    var osname = OS.Constants.Sys.Name.toLowerCase();
    if (osname.indexOf('win') === 0) {
        // windows
        lib = ctypes.open('kernel32');
        var GetCommandLineW = lib.declare('GetCommandLineW', ctypes.winapi_abi,  ctypes.jschar.ptr);
        var rez = GetCommandLineW();
        console.log('rez:', rez.readString());
    } else if (osname == 'darwin') {
        // mac
    
        lib = ctypes.open(ctypes.libraryName('objc'));
    
        // BASIC TYPES
        var BOOL = ctypes.signed_char;
        var NSUINTEGER = ctypes.unsigned_long;
    
        // COMMON FUNCTIONS
        var objc_getClass = lib.declare('objc_getClass', ctypes.default_abi, ctypes.voidptr_t, ctypes.char.ptr);
        var objc_msgSend = lib.declare('objc_msgSend', ctypes.default_abi, ctypes.voidptr_t, ctypes.voidptr_t, ctypes.voidptr_t, '...');
        var sel_registerName = lib.declare('sel_registerName', ctypes.default_abi, ctypes.voidptr_t, ctypes.char.ptr);
    
        // current_application = [NSRunningApplication currentApplciation];
        var NSProcessInfo = objc_getClass('NSProcessInfo');
        var processInfo = sel_registerName('processInfo');
        var process_info = objc_msgSend(NSProcessInfo, processInfo);
    
        // bundle_identifier = [current_application bundleIdentifier]
        var arguments = sel_registerName('arguments');
        var args = objc_msgSend(process_info, arguments);
    
    
        var count = sel_registerName('count');
        var _count = objc_msgSend(args, count);
        console.info('_count:', _count, _count.toString(), uneval(_count), parseInt(_count));
    
        // make it into a js number
        _count = parseInt(ctypes.cast(_count, NSUINTEGER).value); 
        console.log('_count:', _count);
    
        var objectAtIndex = sel_registerName('objectAtIndex:'); // used in for loop
        var UTF8String = sel_registerName('UTF8String'); // used in for loop
    
        for (var i=0; i<_count; i++) {
            var arg = objc_msgSend(args, objectAtIndex, NSUINTEGER(i)); // must wrap `i` in NSUINTEGER as its variadic, you have to specify the type. this is specific to js-ctypes         
            var argString = ctypes.cast(objc_msgSend(arg, UTF8String), ctypes.char.ptr);
            console.log('arg "' + i + '":', argString.readStringReplaceMalformed(), 'arg:', arg, arg.toString(), uneval(arg));
        }
    
    } else {
        // *nix
    
        // todo
        // per - http://stackoverflow.com/a/821889/1828637
        var libcPaths = ['libc.so', 'libc.so.7', 'libc.so.61.0', 'libc.so.6', 'libc.so.0.1', 'libc.dylib'];
        for (var i=0; i<libcPaths.length; i++) {
            try {
                lib = ctypes.open(libcPaths[i]);
                break;
            } catch(ignore) {}
        }
        if (!lib) {
            throw new Error('failed to find libc on this system');
        }
    
        var popen = lib.declare('popen', ctypes.default_abi, ctypes.voidptr_t, ctypes.char.ptr, ctypes.char.ptr);
        var fread = lib.declare('fread', ctypes.default_abi, ctypes.size_t, ctypes.voidptr_t, ctypes.size_t, ctypes.size_t, ctypes.voidptr_t);
        var feof = lib.declare('feof', ctypes.default_abi, ctypes.int, ctypes.voidptr_t);
        var pclose = lib.declare('pclose', ctypes.default_abi, ctypes.int, ctypes.voidptr_t);
        var getpid = lib.declare('getpid', ctypes.default_abi, ctypes.int32_t);
    
        // ps -fp 2540
        var pid = getpid();
        var file = popen('ps -ww -fp' + pid, 'r');
    
        var buf = ctypes.char.array(100)();
        var jsbuf = [];
        var reachedEof = false;
        while (!reachedEof) {
            var cnt = fread(buf, 1, buf.length * buf.constructor.elementType.size, file);
            cnt = parseInt(cnt);
            if (cnt > 0) {
                jsbuf.push(buf.readString().substring(0, cnt));
            }
            reachedEof = feof(file);
        }
    
        var closeit = pclose(file);
        console.log('closeit:', closeit);
    
        console.log('jsbuf:', jsbuf.join(''));
    }
    
    lib.close();