检测用户是否已从Chrome扩展程序登录 [英] Detecting whether user is logged in or not from a Chrome extension
问题描述
是否有一种简单的方法可以从Chrome扩展程序中检测到用户是否已登录他的Google帐户。
我想有一种讨厌的方式这通过加载需要验证的html / css / js资源来完成。
但我想以清晰的方式做到这一点。
thx和最好的,
Viktor
在铬扩展邮件列表中已经有一些讨论:
http://groups.google.com/a/chromium.org/group/chromium-extensions/ browse_thread / thread / 6e46a3a6e46d9110 /
基本上,根据用户的建议,您向Google.com发送Xml Http请求,并通过正则表达式搜索,如果当前用户已登录:
来源:Guillaume Boudreau(位于铬扩展邮件列表)
var currentUser;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data){
if(xhr.readyState == 4){
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(当前记录的用户(在google.com上):+
currentUser);
}
};
xhr.open('GET','https://www.google.com/',false);
xhr.send();
is there an easy way to detect from a Chrome extension that the user is logged into his google account or not.
I guess there is a nasty way to figure this out by loading a html/css/js resource that requires authentication. But i'd like to do this in a "clear" way.
thx and best, Viktor
there has been some discussions on that within the chromium-extensions mailing list: http://groups.google.com/a/chromium.org/group/chromium-extensions/browse_thread/thread/6e46a3a6e46d9110/
Basically, as a user suggested, you send an Xml Http Request to google.com, and search through regex, if a current user is signed in:
Source: Guillaume Boudreau (on chromium-extensions mailing list)
var currentUser;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
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://www.google.com/', false);
xhr.send();
这篇关于检测用户是否已从Chrome扩展程序登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!