Manifest V3 - TypeError: Cannot read property 'data' of undefined
Manifest V3 - TypeError: Cannot read property 'data' of undefined
嗨,我的 chrome 扩展刚刚更新到 Manifest V3,我在将数据发布到 https://oauth2.googleapis.com/token
时遇到问题
这是我的代码:
chrome.runtime.sendMessage({
action: "post_request",
url: 'https://oauth2.googleapis.com/token',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
params: `code=${authorizationCode}&client_id=${dbClientId}&client_secret=&redirect_uri=${redirectUri}&grant_type=authorization_code` },
async function(_response) {
let response = JSON.parse(_response.data);
if (response.access_token) {
response.success = true
const idTokenDetails = await getIdTokenInfo(response.id_token);
// Set expire time for token and save in Chrome Storage
response.expiresAt = nowInSeconds + response.expires_in
response.idTokenDetails = idTokenDetails;
//save in storage
UserInfoStorage.setGoogleIdToken(idTokenDetails)
UserInfoStorage.setGoogleAccessToken(response);
callback(response)
}
else {
response.success = false;
callback(response);
}
}
)
并在我的控制台中收到错误消息:
这是我在后台的代码:
if(request.action === 'post_request') {
var xhr = new XMLHttpRequest();
xhr.open('POST', request.url, true);
if(request.headers) {
for(let header in request.headers) {
xhr.setRequestHeader(header, request.headers[header]);
}
}
xhr.onload = function () {
if (xhr.response) {
sendResponse({success:true, data:xhr.response})
} else {
sendResponse({success:false, statusText:xhr.statusText})
}
};
if(request.params) {
xhr.send(request.params)
}
else {
xhr.send();
}
}
有人可以帮我吗?
提前致谢
好的!这可以通过将 XMLHttpRequest
替换为 fetch()
并将 request.action
替换为 request.contentScriptQuery
来解决
嗨,我的 chrome 扩展刚刚更新到 Manifest V3,我在将数据发布到 https://oauth2.googleapis.com/token
这是我的代码:
chrome.runtime.sendMessage({
action: "post_request",
url: 'https://oauth2.googleapis.com/token',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
params: `code=${authorizationCode}&client_id=${dbClientId}&client_secret=&redirect_uri=${redirectUri}&grant_type=authorization_code` },
async function(_response) {
let response = JSON.parse(_response.data);
if (response.access_token) {
response.success = true
const idTokenDetails = await getIdTokenInfo(response.id_token);
// Set expire time for token and save in Chrome Storage
response.expiresAt = nowInSeconds + response.expires_in
response.idTokenDetails = idTokenDetails;
//save in storage
UserInfoStorage.setGoogleIdToken(idTokenDetails)
UserInfoStorage.setGoogleAccessToken(response);
callback(response)
}
else {
response.success = false;
callback(response);
}
}
)
并在我的控制台中收到错误消息:
这是我在后台的代码:
if(request.action === 'post_request') {
var xhr = new XMLHttpRequest();
xhr.open('POST', request.url, true);
if(request.headers) {
for(let header in request.headers) {
xhr.setRequestHeader(header, request.headers[header]);
}
}
xhr.onload = function () {
if (xhr.response) {
sendResponse({success:true, data:xhr.response})
} else {
sendResponse({success:false, statusText:xhr.statusText})
}
};
if(request.params) {
xhr.send(request.params)
}
else {
xhr.send();
}
}
有人可以帮我吗?
提前致谢
好的!这可以通过将 XMLHttpRequest
替换为 fetch()
并将 request.action
替换为 request.contentScriptQuery