views:

16

answers:

1

Hi,

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

A: 

Hi, 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();
Mohamed Mansour