Спасибо wOxxOm, я написал, что хотел:
function getRedirect(url) {
return new Promise((resolve) => {
let blockedURL;
function onRequest(details) {
//console.log("onBeforeRequest ", details);
if (details.url === blockedURL) {
setTimeout(() => {
chrome.webRequest.onBeforeRequest.removeListener(onRequest);
resolve(blockedURL);
});
return { cancel: true };
}
}
chrome.webRequest.onBeforeRequest.addListener(onRequest, { urls: ["<all_urls>"] }, ["blocking"]);
function onRedirect(details) {
//console.log("onBeforeRedirect", details);
blockedURL = details.redirectUrl;
chrome.webRequest.onBeforeRedirect.removeListener(onRedirect);
}
chrome.webRequest.onBeforeRedirect.addListener(onRedirect, { urls: ["<all_urls>"] });
fetch(url, { redirect: "follow" }).catch(console.warn);
});
}
Давайте проверим код:
(async () => {
console.log("URL Redirects to ", await getRedirect("http://crbug.com"));
// "URL Redirects to https://crbug.com"
console.log("URL Redirects to ", await getRedirect(await getRedirect("http://crbug.com")));
// "URL Redirects to https://bugs.chromium.org/p/chromium/issues/list"
await fetch("http://crbug.com");
// It works in common way (Fetch finished loading: GET "https://bugs.chromium.org/p/chromium/issues/list")
})();
В manifest.json: "permissions": ["<all_urls>", "webRequest", "webRequestBlocking"]