javascript Chrome 扩展:获取用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28980582/
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
Chrome Extension:Get User Name
提问by Akash R
I'm creating a Google chrome extension, for some functionality I need user's system login name (no password), by using JavaScriptit is not possible to do so.
我正在创建一个Google chrome 扩展,对于某些功能,我需要用户的系统登录名(无密码),通过使用JavaScript是不可能的。
Some suggest NPAPI, but I have no idea about it, so I quit.
有人建议NPAPI,但我不知道它,所以我退出了。
Next thing I'm trying to get user name in Chrome Browser. But still no success.
接下来我试图在Chrome 浏览器中获取用户名。但仍然没有成功。
I try to use some thing like:
我尝试使用类似的东西:
var currentUser;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 1) {
currentUser = null;
if (xhr.status == 200)
{
var re = new RegExp(/<b class="?gb4"?>[\s]*([^<]+@[^<]+) <\/b>/i);
var m = re.exec(xhr.responseText);
if (m && m.length == 2) {
currentUser = m[1];
}
}
console.log("Currently logged user (on google.com): " + currentUser);
}
};
xhr.open('GET', ' https://myaccount.google.com/?pli=1', false);
xhr.send(); `
still no success.
仍然没有成功。
My whole agenda is to get user name (either desktop login name or Chrome login name), and I'm not able to get it.
我的整个议程是获取用户名(桌面登录名或 Chrome 登录名),但我无法获取。
(added from comments)I need to send this username as parameter to my service, as user name works as primary key.
(从评论中添加)我需要将此用户名作为参数发送到我的服务,因为用户名用作主键。
回答by Xan
First off, you say that you need this login information to identify a user, using it as a primary key.
首先,您说您需要此登录信息来识别用户,并将其用作主键。
That automatically disqualifies system login names: they are not unique.
这会自动取消系统登录名的资格:它们不是唯一的。
Now, let's get back to logged-in Chrome user. Google Account is reasonably unique.
现在,让我们回到登录的 Chrome 用户。Google 帐户相当独特。
There are two approaches to take here.
这里有两种方法。
Chrome's
chrome.identity
API can provideboth the email and, maybe better for your purposes, a unique ID for the account.You will need
"identity"
and"identity.email"
permissions. Then:chrome.identity.getProfileUserInfo(function(userInfo) { /* Use userInfo.email, or better (for privacy) userInfo.id They will be empty if user is not signed in in Chrome */ });
An alternative approach is to use Google's OAuth on your service. Again, see the
chrome.identity
documentation.
Chrome 的
chrome.identity
API 既可以提供电子邮件,也可以提供帐户的唯一 ID(可能更适合您的目的)。您将需要
"identity"
和"identity.email"
权限。然后:chrome.identity.getProfileUserInfo(function(userInfo) { /* Use userInfo.email, or better (for privacy) userInfo.id They will be empty if user is not signed in in Chrome */ });
另一种方法是在您的服务上使用 Google 的 OAuth。再次,请参阅
chrome.identity
文档。
回答by Shubham Aggarwal
Chrome.identity has a method getProfileUserInfo but it gives out the details of only primary user.
Chrome.identity 有一个方法 getProfileUserInfo 但它只给出主要用户的详细信息。
There is an alternative:
有一个替代方案:
When you open chrome, you launch onto the home screen and you should be able to see a circle on the top right corner which shows image of logged in user. You can access that using DOM. Once you do that you will be able to get information about all the logged in users.
当您打开 chrome 时,您会启动到主屏幕,您应该能够在右上角看到一个圆圈,该圆圈显示了登录用户的图像。您可以使用 DOM 访问它。完成此操作后,您将能够获取有关所有登录用户的信息。