6

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?

bhollis
  • 4,624
  • 2
  • 28
  • 32

4 Answers4

11

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

James Brady
  • 27,032
  • 8
  • 51
  • 59
  • 1
    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. – bhollis Dec 30 '08 at 19:10
  • 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! – James Brady Dec 31 '08 at 00:35
  • 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 Nov 24 '09 at 18:08
1

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); },

Don
  • 124
  • 7
0

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

Dave Kasper
  • 1,369
  • 1
  • 11
  • 18
-1

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
  • 4,877
  • 13
  • 39
  • 55