4 * nodejs script to test basic CDP behaviors against Firefox and Chromium.
6 * Install chrome-remote-interface, the npm package for a CDP client in node:
7 * $ npm install chrome-remote-interface
9 * Run Firefox or Chromium with server turned on:
10 * $ ./mach run --setpref "remote.enabled=true" --remote-debugging-port 9222
11 * $ firefox --remote-debugging-port 9222
12 * $ chromium-browser --remote-debugging-port=9222
17 const CDP = require("chrome-remote-interface");
19 async function demo() {
23 const {Log, Network, Page, Runtime} = client;
25 // Bug 1553756, Firefox requires `contextId` argument to be passed to
26 // Runtime.evaluate, so fetch the current context id it first.
28 const { context } = await Runtime.executionContextCreated();
29 const contextId = context.id;
31 let { result } = await Runtime.evaluate({
32 expression: "this.obj = {foo:true}; this.obj",
35 console.log("1", result);
36 ({ result } = await Runtime.evaluate({
37 expression: "this.obj",
40 console.log("2", result);
41 ({ result } = await Runtime.evaluate({
42 expression: "this.obj.foo",
45 console.log("3", result);
47 // receive console.log messages and print them
49 Log.entryAdded(({entry}) => {
50 const {timestamp, level, text, args} = entry;
51 const msg = text || args.join(" ");
52 console.log(`${new Date(timestamp)}\t${level.toUpperCase()}\t${msg}`);
55 // turn on navigation related events, such as DOMContentLoaded et al.
58 const onLoad = Page.loadEventFired();
59 await Page.navigate({url: "data:text/html,test-page<script>console.log('foo');</script><script>'</script>"});