Skip to content

Commit

Permalink
Set headers using Bidi (Firefox 128) (#2108)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Jul 15, 2024
1 parent 857b5c4 commit afb132c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
49 changes: 49 additions & 0 deletions lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class FirefoxBidi {
}
});
}

async setBasicAuth(basicAuth) {
const parts = basicAuth.split('@');
const bidi = new Bidi(this.driver, this.options.browser);
Expand Down Expand Up @@ -146,4 +147,52 @@ export class FirefoxBidi {
}
}
}

async setRequestHeaders(requestHeaders) {
const headersArray = toArray(requestHeaders);
const headers = [];
for (let header of headersArray) {
if (header.indexOf && header.includes(':')) {
const parts = header.split(':');
headers.push({
name: parts[0],
value: {
type: 'string',
value: parts[1]
}
});
} else {
log.error(
'Request headers need to be of the format key:value not ' + header
);
}
}

const command = {
method: 'network.addIntercept',
params: {
phases: ['beforeRequestSent']
}
};
const bidi = new Bidi(this.driver, this.options.browser);

await bidi.send(command);
await bidi.subscribe('network.beforeRequestSent');
await bidi.onMessage(async function (event) {
const parsedEvent = JSON.parse(Buffer.from(event.toString()));
if (parsedEvent.method === 'network.beforeRequestSent') {
const continueRequest = {
method: 'network.continueRequest',
params: {
request: parsedEvent.params.request.request,
headers: [...parsedEvent.params.request.headers, ...headers]
}
};
const result = await bidi.send(continueRequest);
if (result.type != 'success') {
log.error(result);
}
}
});
}
}
33 changes: 18 additions & 15 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,25 @@ export class Firefox {
await this.browsertimeBidi.setBasicAuth(this.options.basicAuth);
}

if (this.options.block) {
await this.browsertimeBidi.blockUrls(this.options.block);
}
if (this.options.requestheader) {
await this.browsertimeBidi.setRequestHeaders(this.options.requestheader);
if (this.options.block) {
await this.browsertimeBidi.blockUrls(this.options.block);
}

if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
) {
const currentUserAgent = await runner.runScript(
'return navigator.userAgent;',
'GET_USER_AGENT'
);
let script = `Services.prefs.setStringPref('general.useragent.override', '${currentUserAgent} ${
this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent
}');`;
return runner.runPrivilegedScript(script, 'SET_USER_AGENT');
if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
) {
const currentUserAgent = await runner.runScript(
'return navigator.userAgent;',
'GET_USER_AGENT'
);
let script = `Services.prefs.setStringPref('general.useragent.override', '${currentUserAgent} ${
this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent
}');`;
return runner.runPrivilegedScript(script, 'SET_USER_AGENT');
}
}
}

Expand Down

0 comments on commit afb132c

Please sign in to comment.