For the main page (not an iframe) it should happen automatically as the extension already has a host permission for this site. For sites inside an iframe there's a problem with cookie partitioning I guess, in which case you may need to send document.cookie to your background script, which will use
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
makeRequest(msg).then(sendResponse);
return true;
});
async function makeRequest({url, opts, cookie}) {
const old = await chrome.declarativeNetRequest.getSessionRules();
const id = old.length + 1;
await chrome.declarativeNetRequest.updateSessionRules({
addRules: [{
id,
condition: {
initiatorDomains: [
chrome.runtime.id],
resourcesTypes: ['xmlhttprequest'],
},
action: {
type: 'modifyHeaders',
requestHeaders: [{header: 'cookie', operation: 'set', value: cookie}],
},
}],
});
const res = await (await fetch(url, opts)).text();
await chrome.declarativeNetRequest.updateSessionRules({
removeRuleIds: [id],
});
return res;
}