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.identity.getAuthToken({interactive: false}, function (token) {
if (!token) {
if (chrome.runtime.lastError.message.match(/not signed in/)) {
console.log("not singed in");
} else {
console.log("singed in");
}
}
});
And don't forget to add "identity"
to permissions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With