Javascript 在旧版(非 WebExtensions)Firefox 插件中获取 Windows 用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2968690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Windows username in a legacy (not WebExtensions) Firefox add-on
提问by cwhiii
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?
我正在使用 Firefox 插件(用 JavaScript 编写),需要确定当前登录的 Windows 用户。有没有办法做到这一点?
回答by cwhiii
This does the trick on Windows:
这在 Windows 上很有效:
function getUser() {
return Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('USERNAME');
}
回答by el.pescado
You can use nsIEnvironmentinterface to get USERNAMEenvironmnet variable.
您可以使用nsIEnvironment接口来获取USERNAME环境变量。
回答by rashid
Following code works for me instead of onload event with function calling:
以下代码适用于我,而不是使用函数调用的 onload 事件:
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);
回答by GitaarLAB
Firefox already has Integrated Authentication built-in (many people don't know that).
See: https://developer.mozilla.org/en-US/docs/Integrated_Authentication
Firefox 已经内置了集成身份验证(很多人不知道)。
请参阅: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/
这是一个简化配置的流行 Firefox 插件: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
这里有一些额外的解释:http:
//justgeeks.blogspot.nl/2011/01/firefox-supports-integrated-windows.html
Good luck!
祝你好运!
回答by carny666
<html>
<head>
<script language="javascript">
function GetUserName()
{
var wshell = new ActiveXObject("WScript.Shell");
alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
}
</script>
</head>
<body OnLoad="GetUserName();">
</body>
</html>

