Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active September 13, 2023 09:20
Show Gist options
  • Save AutoSponge/5e2fc3eb65a9e010937766e581f2ee06 to your computer and use it in GitHub Desktop.
Save AutoSponge/5e2fc3eb65a9e010937766e581f2ee06 to your computer and use it in GitHub Desktop.
// node --experimental-repl-await ./repl.js
// OLD API (See below for a newer one)
const repl = require('repl');
const playwright = require('playwright');
const config = {
headless: false,
args: [
// https://tink.uk/playing-with-the-accessibility-object-model-aom/
'--enable-blink-features=AccessibilityObjectModel',
],
};
const completions = ['.help', '.exit', '.load', '.save', 'playwright'];
// https://nodejs.org/api/readline.html#readline_use_of_the_completer_function
function completer(line) {
const hits = completions.filter(c => c.startsWith(line));
return [hits.length ? hits : completions, line];
}
(async () => {
const browser = await playwright.chromium.launch(config);
const targets = await browser.targets();
const pageTarget = targets.find(t => t._targetInfo.type === 'page');
const defaultContext = pageTarget.context();
const [defaultPage] = await defaultContext.pages();
const context = await browser.newContext({
viewport: null,
});
const page = await context.newPage();
await defaultPage.close();
const client = await browser.pageTarget(page).createCDPSession();
await client.send('Accessibility.enable');
await client.send('Runtime.enable');
await client.send('DOM.enable');
return { browser, context, page, client };
})().then(props => {
completions.push(...Object.keys(props));
const r = repl.start({
prompt: '> ',
useColors: true,
preview: true,
completer,
});
Object.assign(r.context, props);
});
// node --experimental-repl-await ./repl.js
// v1.18 API
const repl = require("repl");
const playwright = require("playwright");
const config = {
headless: false,
args: [
// https://tink.uk/playing-with-the-accessibility-object-model-aom/
"--enable-blink-features=AccessibilityObjectModel",
],
};
const completions = [".help", ".exit", ".load", ".save", "playwright"];
// https://nodejs.org/api/readline.html#readline_use_of_the_completer_function
function completer(line) {
const hits = completions.filter((c) => c.startsWith(line));
return [hits.length ? hits : completions, line];
}
(async () => {
const browser = await playwright.chromium.launch(config);
const context = await browser.newContext({
viewport: null,
});
const page = await context.newPage();
const client = await page.context().newCDPSession(page);
await client.send("Accessibility.enable");
await client.send("Runtime.enable");
await client.send("DOM.enable");
return { browser, context, page, client };
})().then((props) => {
completions.push(...Object.keys(props));
const r = repl.start({
prompt: "> ",
useColors: true,
preview: true,
completer,
});
Object.assign(r.context, props);
});
@sandeepthukral
Copy link

Hello AutoSponge. I found this using Google and I would love to play with it. Unfortunately, I get the error
(node:67447) UnhandledPromiseRejectionWarning: TypeError: browser.targets is not a function

@AutoSponge
Copy link
Author

I updated the script to the new API. The one you found was quite old. That was before I turned it into a project called https://www.npmjs.com/package/scriptwriter. But that probably needs to be updated too.

@stevez
Copy link

stevez commented Mar 7, 2022

I love this repl, it would be great if you can update the npm library, I found when I typed command 'playwright' in the repl, I got the error: "Uncaught ReferenceError: playwright is not defined", is it a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment