set Accept-Language via Chrome DevTools protocol?

110 views Asked by At

Is there some way to set the default Accept-Language header via the Chrome DevTools Protocol ?

I know i can manually set the header on individual crafted Fetch/XHR requests, that's not what I want to do, rather I want to set the default header.

1

There are 1 answers

0
hanshenrik On

This can be set with Network.setUserAgentOverride - but it's made needlessly complex by making the "userAgent" parameter required (it should have been optional IMO, but most likely the protocol developers did not think anybody would want to change only accept-language)

example of setting it with chrome-php/chrome:

$page->getSession()->sendMessageSync(new \HeadlessChromium\Communication\Message(
    'Network.setUserAgentOverride',
    [
        "userAgent" => $page->evaluate('navigator.userAgent')->getReturnValue(), // we dont want to change the user agent, but it is a required parameter :(
        "acceptLanguage" => "en-US,en;q=0.5",
    ]
));

in addition to the Accept-Language header, this also sets navigator.languages so javascript-based language detection will detect the new language :)