Skip to content

Commit afb132c

Browse files
authored
Set headers using Bidi (Firefox 128) (#2108)
1 parent 857b5c4 commit afb132c

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

lib/firefox/firefoxBidi.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class FirefoxBidi {
7878
}
7979
});
8080
}
81+
8182
async setBasicAuth(basicAuth) {
8283
const parts = basicAuth.split('@');
8384
const bidi = new Bidi(this.driver, this.options.browser);
@@ -146,4 +147,52 @@ export class FirefoxBidi {
146147
}
147148
}
148149
}
150+
151+
async setRequestHeaders(requestHeaders) {
152+
const headersArray = toArray(requestHeaders);
153+
const headers = [];
154+
for (let header of headersArray) {
155+
if (header.indexOf && header.includes(':')) {
156+
const parts = header.split(':');
157+
headers.push({
158+
name: parts[0],
159+
value: {
160+
type: 'string',
161+
value: parts[1]
162+
}
163+
});
164+
} else {
165+
log.error(
166+
'Request headers need to be of the format key:value not ' + header
167+
);
168+
}
169+
}
170+
171+
const command = {
172+
method: 'network.addIntercept',
173+
params: {
174+
phases: ['beforeRequestSent']
175+
}
176+
};
177+
const bidi = new Bidi(this.driver, this.options.browser);
178+
179+
await bidi.send(command);
180+
await bidi.subscribe('network.beforeRequestSent');
181+
await bidi.onMessage(async function (event) {
182+
const parsedEvent = JSON.parse(Buffer.from(event.toString()));
183+
if (parsedEvent.method === 'network.beforeRequestSent') {
184+
const continueRequest = {
185+
method: 'network.continueRequest',
186+
params: {
187+
request: parsedEvent.params.request.request,
188+
headers: [...parsedEvent.params.request.headers, ...headers]
189+
}
190+
};
191+
const result = await bidi.send(continueRequest);
192+
if (result.type != 'success') {
193+
log.error(result);
194+
}
195+
}
196+
});
197+
}
149198
}

lib/firefox/webdriver/firefox.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,25 @@ export class Firefox {
102102
await this.browsertimeBidi.setBasicAuth(this.options.basicAuth);
103103
}
104104

105-
if (this.options.block) {
106-
await this.browsertimeBidi.blockUrls(this.options.block);
107-
}
105+
if (this.options.requestheader) {
106+
await this.browsertimeBidi.setRequestHeaders(this.options.requestheader);
107+
if (this.options.block) {
108+
await this.browsertimeBidi.blockUrls(this.options.block);
109+
}
108110

109-
if (
110-
this.firefoxConfig.appendToUserAgent ||
111-
this.options.appendToUserAgent
112-
) {
113-
const currentUserAgent = await runner.runScript(
114-
'return navigator.userAgent;',
115-
'GET_USER_AGENT'
116-
);
117-
let script = `Services.prefs.setStringPref('general.useragent.override', '${currentUserAgent} ${
118-
this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent
119-
}');`;
120-
return runner.runPrivilegedScript(script, 'SET_USER_AGENT');
111+
if (
112+
this.firefoxConfig.appendToUserAgent ||
113+
this.options.appendToUserAgent
114+
) {
115+
const currentUserAgent = await runner.runScript(
116+
'return navigator.userAgent;',
117+
'GET_USER_AGENT'
118+
);
119+
let script = `Services.prefs.setStringPref('general.useragent.override', '${currentUserAgent} ${
120+
this.firefoxConfig.appendToUserAgent || this.options.appendToUserAgent
121+
}');`;
122+
return runner.runPrivilegedScript(script, 'SET_USER_AGENT');
123+
}
121124
}
122125
}
123126

0 commit comments

Comments
 (0)