I get undefined from fetch only on chrome, while it works on firefox.
// content script
async function fetchAPI(link, init = {}) {
let res = await chrome.runtime.sendMessage({
url: link,
config: init
})
return res;
}
fetchAPI(`some link`);
// background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log(request);
return new Promise((resolve) => {
fetch(request.url, request.config)
.then(res => {
res.json()
.then(value => {
resolve(value);
});
});
});
});
Sorry about the messy background script code. I don't think I have much more to explain.