user8388622
user8388622

Reputation:

Chrome WebRequest API: How to block "Set-Cookies"?

I need to block all cookies from a certain domain. (I cant use the content settings API, since FireFox doesn't support it yet)

I am not having a whole lot of success with that I have now, I wonder if I am going in the right direction?

Using the WebRequest API, I added a listener to

onHeadersReceived

and made sure my function returned a promise, I then went through the headers like so:

function modifyHeaders(headers: HttpHeader[]) {
    headers.forEach((header: HttpHeader) => {
        if (header.name === "set-cookie") {
            /* makeExpire sets "Expires= sometimeInThePast" */ 
            header.value = makeExpire(header.value!)
        }
    })
}

modifyHeaders(headers);
return { responseHeaders: headers }

This seems like the lowest overhead way of doing it, but so far it doesn't seem to work. I think I might be on the wrong track some how.

Upvotes: 0

Views: 746

Answers (1)

Lusito
Lusito

Reputation: 1023

function onHeadersReceived(details) {
    if (details.responseHeaders) {
        return {
            responseHeaders: details.responseHeaders.filter((x) => {
                return x.name.toLowerCase() !== 'set-cookie';
            })
        };
    }
    return {};
}
browser.webRequest.onHeadersReceived.addListener(onHeadersReceived, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);

Upvotes: 1

Related Questions