Finding the currently logged in user from a Firefox extension
Asked Answered
G

4

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?

Gaytan answered 30/12, 2008 at 1:13 Comment(0)
D
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...

Degrease answered 30/12, 2008 at 9:48 Comment(3)
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.Gaytan
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!Degrease
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?Cordoba
H
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); },

Heady answered 16/9, 2015 at 20:7 Comment(0)
B
0

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

Basia answered 30/12, 2008 at 2:1 Comment(0)
D
-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...)

Decato answered 30/12, 2008 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.