Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fetch in chrome extension doesn't include referer header in the request

Normally, using fetch from a website to send a request would include a referrer header in the request depending on the referrer-policy

On a chrome extension background script, I have tried with referrer as client and referrerPolicy as unsafe-url, origin and origin-when-cross-origin. In my manifest, I have my endpoint url in the permission but not all-urls.

fetch(url, {
    method: 'POST',
    mode: 'cors',
    credentials: 'include',
    cache: 'no-cache',
    headers: { 
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    referrer: 'client',
    referrerPolicy: 'origin',
    body: JSON.stringify({ params })
  })
like image 437
Jun711 Avatar asked Apr 15 '26 10:04

Jun711


1 Answers

A violent resolution:

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    details.requestHeaders.push({name: 'Referer', value:'http://localhost/referer'});
    return {requestHeaders: details.requestHeaders};
  },
  {urls: ["http://localhost/*"]},
  ["blocking", "requestHeaders"]
);


fetch('http://localhost/')
like image 87
tabris17 Avatar answered Apr 16 '26 22:04

tabris17



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!