0

I'm successfully launching a webauthflow to an social OAuth provider from a service worker background script (as per manifest v3 requirements, background scripts are now full blown service workers)

However I'm unable to send a message back to my content script in what should be the simplest scenario.

Here is my service worker (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;
});

And here is my content script...(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);            
        }
    });
});
Mattia Righetti
  • 495
  • 11
  • 25
Senkwe
  • 2,127
  • 3
  • 23
  • 31
  • 1
    The message-related code is correct so the problem is elsewhere. Use devtools to set breakpoints in the callbacks inside the background worker and see what happens when the message is received. – wOxxOm May 03 '21 at 14:03
  • Yes, so it turns out there was nothing wrong with code as you said. There was just nowhere for me to read the console.log(...) output in the callback method. I added an alert instead and it fired just fine. – Senkwe May 04 '21 at 12:58

1 Answers1

0

Turns out there was nothing wrong with code. There was(is) just nowhere for me to read the console.log(...) output in the callback method for some reason. I added an alert instead and it fired just fine.

Senkwe
  • 2,127
  • 3
  • 23
  • 31