简体   繁体   中英

How to get Windows username in a legacy (not WebExtensions) Firefox add-on?

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?

This does the trick on Windows:

function getUser() {
   return Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('USERNAME');
}      

您可以使用nsIEnvironment接口来获取USERNAME环境变量。

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

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!

<html>
<head>
    <script language="javascript">
        function GetUserName()
        {
            var wshell = new ActiveXObject("WScript.Shell");
            alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
        }
    </script>
</head>
<body OnLoad="GetUserName();">
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM