12 Jan 2017 : Checking the user's email address in a chrome extension

I’m making a chrome extension that watches Google calendar and warns me when I forget to invite the second person to a two person meeting. Is this is a thing that happens to other people? I’ve done it a bunch of times: set up a meeting called myname:someoneelse, futz with locations and times, then save the meeting without actually inviting the other person. Well, it’s a good excuse to learn some JQuery :-)

I didn’t want it to keep telling me that I hadn’t invited myself, so I had to check the username of the person logged into Calendar, and for that I needed Chrome’s Identity API. It requests the identity and identity.email permissions (you need both), and then you can use chrome.identity.getProfileUserInfo to get the email address for the signed in account, even if they’re offline. Much cleaner than trying to parse it out of the HTML.

Edit: this gives the address of the user logged into Chrome, not necessarily the user logged into Calendar. See chrome-identity-api-part-2 for more about that.

Most of the Chrome APIs aren’t available to content scripts, so I had to add a background script to do the work. What’s the difference? A content script runs on the DOM, the same HTML that the user is looking at. It’s what’s doing all of the text, html and image modification that make chrome extensions great, like inserting pictures of cats or replacing the word ‘cloud’ with the word ‘butt’. But content scripts only have very limited access to the Chrome APIs: for most things you need background pages.

Background pages don’t run per-tab: your browser runs one of them for the whole whole lifetime of the extension. They don’t have access to your tab’s HTML, and when they write to the console, you won’t see it on the usual inspect page; instead, http://chrome://extensions will have a “background page” link that opens a web inspector to let you see what’s happening.

The manifest.json file has sections for each. Here’s mine:

"name":"One To None",
"description":"Forgetting to add the other person in your two person meeting",
"version":"1",
"manifest_version":2,
"content_scripts": [
    {
      "matches": ["*://calendar.google.com/*"],
      "js": ["jquery-3.1.1.min.js", "content.js"]
    }
  ],
"background": {
  "scripts": ["background.js"],
  "persistent": false
  },
"permissions": ["identity", "identity.email"]

The background and content scripts talk to each other using message passing.

  • The background.js script gets the user information and sits around waiting for someone to ask for it:
chrome.identity.getProfileUserInfo(function(info) { email = info.email; });

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    sendResponse( {email: email})
});
  • And then the content.js script requests it:
var logged_in_user

chrome.extension.sendMessage({}, function(response) {
  logged_in_user = response.email
});

console.log("Got user:", logged_in_user);

// Cats/cloudbutts/calendar-wrangling goes here.

Edit: as mentioned above, this technique gets you the email address of the user logged into Chrome. It’s not necessarily the same user that’s logged into Calendar. To get that, I needed to use oauth2. For more exciting adventures from the weekend I spent trying to get an email address out of Google Calendar, read chrome-identity-api-part2. (Excitement not guaranteed.)

Comments