Skip to content

Instantly share code, notes, and snippets.

@mpetrunic
Created August 29, 2022 14:17
Show Gist options
  • Select an option

  • Save mpetrunic/1bc80c08d080a30e306479ce0330935b to your computer and use it in GitHub Desktop.

Select an option

Save mpetrunic/1bc80c08d080a30e306479ce0330935b to your computer and use it in GitHub Desktop.
Sample code to install firefox extension using playwright
import { firefox } from '@playwright/test';
import { Buffer } from 'buffer';
import net from 'net';
describe("demo", function() {
it('homepage has Playwright in title and get started link linking to the intro page', async () => {
const browser = await firefox.launch({
headless: false,
slowMo: 300,
args: [ '-start-debugger-server', String(12345) ],
firefoxUserPrefs: {
'devtools.debugger.remote-enabled': true,
'devtools.debugger.prompt-connection': false,
"xpinstall.signatures.required": false,
"xpinstall.whitelist.required": false,
"extensions.langpacks.signatures.required": false
}
})
const page = await browser.newPage()
await loadFirefoxAddon(12345, "localhost", "/home/mpetrunic/Projects/ChainSafe/snaps/playwright-firefox/metamask-firefox-10.18.4.zip")
});
});
export const loadFirefoxAddon = (port: number, host: string, addonPath: string) => {
return new Promise<boolean>((resolve) => {
const socket = net.connect({
port,
host,
});
let success = false;
socket.once('error', () => {});
socket.once('close', () => {
resolve(success);
});
const send = (data: Record<string, string>) => {
const raw = Buffer.from(JSON.stringify(data));
socket.write(`${raw.length}`);
socket.write(':');
socket.write(raw);
};
send({
to: 'root',
type: 'getRoot',
});
const onMessage = (message: any) => {
if (message.addonsActor) {
send({
to: message.addonsActor,
type: 'installTemporaryAddon',
addonPath,
});
}
if (message.addon) {
success = true;
socket.end();
}
if (message.error) {
socket.end();
}
};
const buffers: Buffer[] = [];
let remainingBytes = 0;
socket.on('data', (data) => {
while (true) {
if (remainingBytes === 0) {
const index = data.indexOf(':');
buffers.push(data);
if (index === -1) {
return;
}
const buffer = Buffer.concat(buffers);
const bufferIndex = buffer.indexOf(':');
buffers.length = 0;
remainingBytes = Number(buffer.subarray(0, bufferIndex).toString());
if (!Number.isFinite(remainingBytes)) {
throw new Error('Invalid state');
}
data = buffer.subarray(bufferIndex + 1);
}
if (data.length < remainingBytes) {
remainingBytes -= data.length;
buffers.push(data);
break;
} else {
buffers.push(data.subarray(0, remainingBytes));
const buffer = Buffer.concat(buffers);
buffers.length = 0;
const json = JSON.parse(buffer.toString());
queueMicrotask(() => {
onMessage(json);
});
const remainder = data.subarray(remainingBytes);
remainingBytes = 0;
if (remainder.length === 0) {
break;
} else {
data = remainder;
}
}
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment