以下是我的Google Chrome扩展程序的代码
清单:
"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
//get 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();
});
//registered event handler for signIn change
chrome.identity.onSignInChanged.addListener(function(accountInfo, isSignedIn){
console.log(accountInfo);
});
但是,当我切换到另一个用户时,不会触发 onSignInChanged 事件。实际上,我想在signIn更改时撤消为先前登录的用户生成的令牌,并为新登录的用户生成新令牌。
由于事件未被触发,我的扩展程序正在加载以前登录用户的联系人。我已经关注了Google的this文档。不知道为什么事件没有被解雇。我错过了什么吗?
答案 0 :(得分:0)
事件未被触发,但是,解决方法是检查当前是否已登录用户以及与身份验证令牌关联的电子邮件是否相同。如果它们相同则继续应用程序,如果没有,则撤销当前令牌并生成一个新的选择当前登录的用户帐户。之后发出Google API请求。