bhollis
bhollis

Reputation: 4673

Finding the currently logged in user from a Firefox extension

I'm writing a Firefox extension that needs to know what the username of the currently logged in user is in Windows, Mac, or Linux. So if I'm logged into my machine as "brh", it'll return "brh". Any idea how to do that from extension JavaScript?

Upvotes: 6

Views: 6288

Answers (4)

Don
Don

Reputation: 124

The flagged correct answer works fine. I use this in our extension on Firefox 38. I also use events so that the page can communicate with the extension and retrieve windows properties from the extension.

getWindowsProperty: function(prop){ return Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get(prop); },

Upvotes: 1

James Brady
James Brady

Reputation: 27482

Firefox extensions play by different rules to normal JavaScript running in the page: finding the current user is absolutely possible.

Open your Error Console (in Tools) and enter this:

Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('USER')

The environment variables Firefox was started with are available through that NsIEnvironment XPCOM component.

You may have to customise this per platform. If all else fails, you might be able to create an NsIFile in ~ then look at its .path; I'm not sure if shell expressions are honoured there, though...

Upvotes: 11

DFectuoso
DFectuoso

Reputation: 4897

Yea, not possible... Javascript runs in a secure enviroment, and all FF extensions are javascript so you wont be able to be doing much interaction with the OS... but ill stick around to see if someone knows a way(it would be VERY cool...)

Upvotes: -1

Dave Kasper
Dave Kasper

Reputation: 1389

Don't think that's possible, seems like it would be a security hole if it were.

Upvotes: 0

Related Questions