Here is a glimpse of the code from my Google Chrome extension:
Manifest:
"content_scripts": [
{
"matches": [
"https://mail.google.com/*",
"https://inbox.google.com/*"
],
"js": ["js/jquery-3.2.1.min.js", "js/content.js"],
"css": ["css/custom.css"],
"run_at": "document_end"
}
],
"background":{
"scripts": ["js/background.js"],
"persistent": true
},
"permissions": [
"https://mail.google.com/",
"https://inbox.google.com/",
"https://accounts.google.com/*",
"identity",
"identity.email",
"storage"
],
"oauth2":{
"client_id": "xxx",
"scopes": [
"profile",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/contacts.readonly",
"https://www.googleapis.com/auth/plus.login",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email"
]
}
background.js
// Retrieving Google OAuth Token
chrome.identity.getAuthToken({
interactive: true
}, function(token) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
alert(chrome.runtime.lastError.message);
return;
}
console.log(token);
currentSessionAccessToken = token;
var x = new XMLHttpRequest();
x.open('GET',
'https://www.googleapis.com/oauth2/v2/userinfo?alt=json&access_token=' + token
);
x.onload = function() {
console.log(x.response);
};
x.send();
});
// Registering event handler for signIn change
chrome.identity.onSignInChanged.addListener(function(accountInfo, isSignedIn){
console.log(accountInfo);
});
However, the onSignInChanged event doesn't fire when I switch to another user. I intend to revoke the token for the previously logged-in user upon a sign-in change, generating a new token for the newly logged-in user.
Unfortunately, as the event is not triggered, my extension continues to load contacts from the previously signed-in user. I have followed Google's documentation, but I'm unsure why the event isn't firing. Is there something I might be overlooking?