Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension: How to get user name?

I'm creating a Google chrome extension. For some functionality, I need user's system login name (no password). By using JavaScript, it is not possible to do so.

Some suggest NPAPI, but I have no idea about it, so I quit.

Next thing I'm trying to get user name in Chrome Browser. But still no success.

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.

I need to send this username as parameter to my service, as user name works as primary key.

like image 745
Akash R Avatar asked Mar 11 '15 07:03

Akash R


People also ask

How do I find the Chrome extension key?

To get a suitable key value, first install your extension from a . crx file (you may need to upload your extension or package it manually). Then, in your user data directory, look in the file Default/Extensions/EXTENSION_ID/EXTENSION_VERSION/manifest. json .


1 Answers

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.

There are two approaches to take here.

  1. Chrome's chrome.identity API can provide both 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 */
    });
    
  2. An alternative approach is to use Google's OAuth on your service. Again, see the chrome.identity documentation.

like image 133
Xan Avatar answered Sep 18 '22 09:09

Xan