views:

439

answers:

3

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?

A: 

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

David
A: 

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

DFectuoso
+3  A: 

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

Alabaster Codify
Fantastic! In Windows, the environment variable is USERNAME instead of USER, by the way - by trying both, you cover all the platforms you'd want.
BRH
No probs. I actually agree with David and DFectuoso - it does seem like a security hole... In fact, extensions have the same rights as Firefox itself - they really are trusted. Honestly surprised there's so little FF malware!
Alabaster Codify
Speaking of security holes, you can set USERNAME so it might not be the best way of identifying the user - they could perhaps use that to impersonate someone else?
Rory