I'm using the webRequest.onBeforeSendHeaders API to modify the headers of some HTTP requests. My problem is that each event listener should only be executed once, but for some reason the event listener can't modify the HTTP request and disconnect itself.
Here's a little demo:
function addReferrer(e){
// This line prevents the function from doing its job
browser.webRequest.onBeforeSendHeaders.removeListener(addReferrer)
e.requestHeaders.push({
name: 'Referer',
value: 'https://fake.referrer'
})
return {requestHeaders: e.requestHeaders}
}
browser.webRequest.onBeforeSendHeaders.addListener(
addReferrer,
{urls: ['https://shouldiblamecaching.com/']},
["blocking", "requestHeaders"]
)
This is supposed to add a Referer
header when you visit https://shouldiblamecaching.com/, but because of the removeListener(addReferrer)
, it doesn't. (You can test this with the "Network" tab in the browser console.) (Tested with Firefox, not sure if it works in other browsers.)
Workarounds I've tried:
- Disconnect with
setTimeout(..., 1)
- Adding a 2nd listener that disconnects both listeners
Is there a clean way to create a one-shot event listener?