Search code examples
typescriptgoogle-chrome-extensiongoogle-apiservice-workermanifest

Manifest V3 - TypeError: Cannot read property 'data' of undefined


Hi my chrome extension just updated to Manifest V3 and I'm encountering a problem when posting data to https://oauth2.googleapis.com/token

here's my code:

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);
    }
  }
)

and getting the error in my console:

Error

this is my code on the background:

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();
  }
}

can somebody help me?

thanks in advance


Solution

  • ALRIGHT! this is solved by Replacing XMLHttpRequest into fetch() and request.action to request.contentScriptQuery