How can I use a Chrome extensions API that can be used with a callback function, such as chrome.identity.getProfileUserInfo, in chrome.webRequest.onBeforeRequest?
For example, I have a part of script similar to:
chrome.webRequest.onBeforeRequest.addListener(
function (request) {
return { redirectUrl: `https://example.com` };
},
// filters here.
// permissions here.
)
The above works, it redirects request attempts to the https://example.com.
But the following, with wrapping the code in a callback function, the return/redirectUrl part is ignored and it doesn't work; it doesn't redirect.
chrome.webRequest.onBeforeRequest.addListener(
function (request) {
chrome.identity.getProfileUserInfo(function (profileUserInfo) {
console.log(profileUserInfo.email);
return { redirectUrl: `https://example.com` };
});
},
// filters here.
// permissions here.
)
This is a synchronous way that webRequest.onBeforeRequest is only capable of, correct? I use these callbacks, synchronous way because I learnt the chrome extensions API doesn't support an asynchronous way (e.g. async/await, Promise) as I asked on SO other day: javascript - async in chrome.webRequest.onBeforeRequest? - Stack Overflow
So, is it possible, to use chrome.identity.getProfileUserInfo in chrome.webRequest.onBeforeRequest? Thanks.
No, you can't. "chrome.identity.getProfileUserInfo" like most "chrome." methods take time to do, therefore the callback function. "chrome.webRequest.onBeforeRequest" only supports the "blocking" option but not "asyncBlocking". But in your case, you could easily assign the ProfileUserInfo to a variable beforehand somewhere in your background script and use it like that.