Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 723
bhollis Avatar asked Dec 30 '08 01:12

bhollis


2 Answers

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

like image 191
James Brady Avatar answered Oct 30 '22 22:10

James Brady


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

like image 20
Don Avatar answered Oct 30 '22 22:10

Don