Get Windows username in a legacy (not WebExtensions) Firefox add-on

强颜欢笑 提交于 2019-11-26 09:46:08

问题


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?


回答1:


This does the trick on Windows:

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



回答2:


You can use nsIEnvironment interface to get USERNAME environmnet variable.




回答3:


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



回答4:


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!




回答5:


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


来源:https://stackoverflow.com/questions/2968690/get-windows-username-in-a-legacy-not-webextensions-firefox-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!