我成功地从服务工作者后台脚本向社交OAuth提供商启动了webauthflow(根据清单v3要求,后台脚本现在是全面的服务工作者(
然而,在最简单的情况下,我无法将消息发送回我的内容脚本。
这是我的服务人员(background.js(
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "login") {
if (user_signed_in) {
console.log("already signed in");
} else {
chrome.identity.launchWebAuthFlow({
url: createOAuthEndpoint(),
interactive: true,
}, function (redirect_uri) {
if (chrome.runtime.lastError) {
sendResponse({
message: "fail"
});
} else {
if (redirect_uri.includes("error")) {
sendResponse({
message: "fail"
});
} else {
//we get here but this message is never sent
sendResponse({
message: "success",
profile: "blah"
});
}
}
});
}
}
return true;
});
这是我的内容脚本。。。(popupsignin.js(
document.querySelector('#sign-in').addEventListener('click', () => {
chrome.runtime.sendMessage({ message: 'login' }, (response) => {
console.log('got the response'); //this log is never written
if (response.message === 'success'){
console.log(response.profile);
}
});
});
发现代码没有任何问题。由于某种原因,我无法读取回调方法中的console.log(…(输出。我添加了一个警报,它发射得很好。