Skip to content

Commit

Permalink
Move injectJS functionality to bidi for Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Mar 7, 2024
1 parent 18fcbdf commit aa995e0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/extensionserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class ExtensionServer {
options.block ||
options.basicAuth ||
options.cookie ||
options.injectJs ||
options.clearCacheKeepCookies)
? true
: false;
Expand Down
4 changes: 0 additions & 4 deletions lib/extensionserver/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ function generateURL(port, testUrl, options) {
query.ba = options.basicAuth + '@' + testUrl;
}

if (options.injectJs && options.browser === 'firefox') {
query.js = options.injectJs;
}

if (options.cookie) {
const cookies = toArray(options.cookie);
query.cookie = [];
Expand Down
25 changes: 25 additions & 0 deletions lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import intel from 'intel';
const log = intel.getLogger('browsertime.firefox.bidi');

export class FirefoxBidi {
constructor(bidi, browsingContextId, options) {
this.options = options;
this.bidi = bidi;
this.browsingContextId = browsingContextId;
}

async injectJavaScript(script) {
const params = {
method: 'script.addPreloadScript',
params: {
functionDeclaration: script
}
};

try {
await this.bidi.send(params);
} catch (error) {
log.error('Could not inject JavaScript:' + error);
}
}
}
2 changes: 1 addition & 1 deletion lib/firefox/webdriver/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export async function configureBuilder(builder, baseDir, options) {

// Enable bidi for HAR support
if (firefoxConfig.bidihar) {
ffOptions.enableBidi();
/* We do not need to do anything for bidi har */
} else {
if (!options.skipHar) {
if (isAndroidConfigured(options)) {
Expand Down
11 changes: 11 additions & 0 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { GeckoProfiler } from '../geckoProfiler.js';
import { MemoryReport } from '../memoryReport.js';
import { PerfStats } from '../perfStats.js';
import { NetworkManager } from '../networkManager.js';
import { FirefoxBidi } from '../firefoxBidi.js';
import { getHAR } from '../getHAR.js';

const log = intel.getLogger('browsertime.firefox');
Expand Down Expand Up @@ -61,12 +62,22 @@ export class Firefox {
driver: runner.getDriver()
});
}
const windowId = await runner.getDriver().getWindowHandle();
this.bidi = new FirefoxBidi(
await runner.getDriver().getBidi(),
windowId,
this.options
);
}

/**
* Before the first iteration of your tests start.
*/
async beforeStartIteration(runner) {
if (this.options.injectJs) {
await this.bidi.injectJavaScript(this.options.injectJs);
}

if (
this.firefoxConfig.appendToUserAgent ||
this.options.appendToUserAgent
Expand Down
2 changes: 1 addition & 1 deletion lib/support/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ export function parseCommandLine() {
})
.option('injectJs', {
describe:
'Inject JavaScript into the current page at document_start. Works for Firefox and Chrome. More info: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/contentScripts'
'Inject JavaScript into the current page at document_start. Works for Firefox, Chrome and Edge. When injecting to Firefox make sure to wrap the code in a function!'
})
.option('block', {
describe:
Expand Down

0 comments on commit aa995e0

Please sign in to comment.