25

I am working a Firefox add-on (which is written in JavaScript) and need to determine the Windows user currently logged on. Is there a way to do this?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
cwhiii
  • 1,496
  • 2
  • 14
  • 16
  • This is a security risk and is why it is not exposed in javascript. What are you trying to do? – John Hartsock Jun 03 '10 at 18:41
  • I'm building an internal extension for a company and need to see which user requests which document. – cwhiii Jun 03 '10 at 19:02
  • @duskwuff, Why bump a 7 year old question just for that edit? All you did was replace one tag in the title with another tag in the title. The tags already made it clear this was about a Firefox add-on. There was no need to edit this to make that change. In fact, if you want to get pedantic, we're not supposed to have non-integral tags in titles, so "Get Windows username" would have been more appropriate. Are you proposing we always put tags in all titles? – Makyen Oct 07 '17 at 05:54
  • 1
    @Makyen The qualifier "in Firefox addon" is critical to understanding what is being asked -- and, more importantly, to understanding the limitations of the answers. The previous title misled at least one user into thinking that the code they found here would work on a web page. –  Oct 07 '17 at 06:07
  • @duskwuff While I understand that there might be some confusion, the question is *very* clear with/without either tag in the title *and* includes the appropriate tags. The problem is one of user education, not with this, or any other, question. Your edit inherently advocates to put tags in titles for every tag which involves environments which are not, necessarily, compatible with the most commonly used case. If it was appropriate/needed for this question, then it's appropriate/needed in every single other similar situation (e.g. jQuery vs. JavaScript), which is 100s of thousands of questions. – Makyen Oct 07 '17 at 06:19
  • 1
    @Makyen I'd advise you to take a look at the questions linking to this one (in the sidebar) for some context. This question gets linked to a lot. Making its limitations absolutely clear -- especially to users who will jump to the answers without fully reading the question! -- is important. –  Oct 07 '17 at 06:30
  • @duskwuff Well, if we're going to do it for this question, we should at least make it clear that the question is about legacy add-ons (i.e. that will be removed from Firefox as of Firefox 57 on 2017-11-14). I can understand the desire to have it more obvious. It is clear that the code has been taken out of context about once every 5 months. Perhaps having a more explicit title will be helpful. However, it's still an issue of educating people to actually read what's being linked to/suggested. – Makyen Oct 07 '17 at 07:02
  • @duskwuff, The other side of this is: have you gone through the links and down-voted/delete-voted where people have used the code inappropriately? For instance, there are two answers just on this question which suggest using ActiveX, which is obviously not possible in Firefox (without an extension which stopped working a long time ago). There are multiple other, similar issues on the various linked questions. – Makyen Oct 07 '17 at 07:05
  • @Makyen I've voted on the answers to this answer, yeah. I haven't had time to dig deep into the other questions yet; there's a lot to go through there. –  Oct 07 '17 at 15:43

5 Answers5

9

This does the trick on Windows:

function getUser() {
   return Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('USERNAME');
}      
Strelok
  • 50,229
  • 9
  • 102
  • 115
cwhiii
  • 1,496
  • 2
  • 14
  • 16
  • 1
    This solution gives me this error`TypeError: Components.classes is undefined`, What do you think the solution for this error? – Scar Oct 08 '12 at 13:22
  • This does not work in Firefox as the original question is asking. – Kyle Pittman Jan 13 '15 at 20:05
  • @Scar : As far as I've understood [this Stackoverflow-post](http://stackoverflow.com/questions/21378058/why-components-classes-is-undefined), you can't use Components.classes in regular webpages. It's meant for use in Firefox extensions/addons. – Stephanie Mar 12 '15 at 13:09
  • Here, Components is not defined – KarSho Oct 19 '15 at 05:02
5

You can use nsIEnvironment interface to get USERNAME environmnet variable.

  • 2
    is there an IE equivalent for this that does not give the trouble ActiveX does (the user being told that there is a security risk etc.) when going through the process of getting the username? – pythonian29033 Aug 08 '13 at 09:16
4

Following code works for me instead of onload event with function calling:

var objUserInfo = new ActiveXObject("WScript.network");
document.write(objUserInfo.ComputerName+"<br>"); 
document.write(objUserInfo.UserDomain+"<br>"); 
document.write(objUserInfo.UserName+"<br>");  
var uname =  objUserInfo.UserName;
alert(uname);
Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
rashid
  • 57
  • 1
  • 1
  • 7
    The asker uses *firefox*. This answer uses M$ ActiveX! It will not work (and I can't believe I actually tested it, but I did). Actually there was a ActiveX Plugin for firefox (some ages ago, around the era of FF2), but ActiveX is now on the mozilla blocklist. ([source](http://kb.mozillazine.org/ActiveX)). – GitaarLAB Apr 06 '13 at 17:37
  • Tested but this code only work in Internet Explorer & will not function in Firefox. The best option is create a hidden control/textbox which to hold the value given from code behind(server side), use javascript to get the value you need. Considering current trend, not everyone use IE to do their work anymore. – mutanic Jul 03 '13 at 03:17
2

Firefox already has Integrated Authentication built-in (many people don't know that).
See: https://developer.mozilla.org/en-US/docs/Integrated_Authentication

Here is a Popular Firefox addon that eases the configuration: https://addons.mozilla.org/nl/firefox/addon/integrated-auth-for-firefox/

Here is some extra explanation:
http://justgeeks.blogspot.nl/2011/01/firefox-supports-integrated-windows.html

Good luck!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
-2
<html>
<head>
    <script language="javascript">
        function GetUserName()
        {
            var wshell = new ActiveXObject("WScript.Shell");
            alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
        }
    </script>
</head>
<body OnLoad="GetUserName();">
</body>
</html>
carny666
  • 2,325
  • 2
  • 18
  • 27