<commit_before>#!/usr/bin/env node

/**
 * Copyright 2020 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as puppeteer from 'puppeteer';
import {
  DOMModel,
  cssPath,
} from './dom';

export default async (url: string) => {
  const browser = await puppeteer.launch({
    headless: false,
    defaultViewport: null,
    executablePath: '/usr/local/google/home/janscheffler/dev/chromium/src/out/Default/chrome',
  });

  const model = new DOMModel();
  const page = await browser.newPage();
  const session = await page.target().createCDPSession();
  session.on('DOM.setChildNodes', (e) => {
    model.setChildNodes(e);
  });

  session.on('DOM.documentUpdated', async (e) => {
    const document = await session.send('DOM.getDocument');
    model.load(document);
  });

  // Setup puppeteer
  console.log(`const puppeteer = require('puppeteer');`)
  console.log(`(async () => {`);
  console.log(`  const browser = await puppeteer.launch({headless: false, defaultViewport: null});`);
  console.log(`  const page = await browser.newPage();`);
  console.log(`  await page.goto('${url}');`);

  // Open the initial page
  await page.goto(url);

  // Get the initial document and initialse the model
  await session.send('DOM.enable');
  const document = await session.send('DOM.getDocument');
  model.load(document);

  // Queue for events to allow processing them in order
  const events = [];

  async function handleEvent() {
    const e = events.shift();
    const node = model.get(e.nodeId);
    if (!node) return;
    const selector = cssPath(node);
    if (!selector) return;

    if (e.name === 'change') {
      try {
        const value = await page.evaluate(selector => document.querySelector(selector).value, selector);
        console.log(`  await page.type('${selector}', '${value}');`);
      } catch (e) { }
    } else if (e.name === 'click') {
      console.log(`  await page.waitForSelector('${selector}', {visible: true});`);
      console.log(`  await page.click('${selector}');`);
    }

    if (events.length) {
      handleEvent();
    }
  }

  // Enqueue every DOM event for processing
  session.on('DOM.DOMEvent', async (e) => {
    events.push(e);
    if (events.length === 1) {
      handleEvent();
    }
  });

  // Finish the puppeteer script
  return new Promise(resolve => {
    browser.on('disconnected', () => {
      console.log(`})()`);
      resolve();
    });
  });
}
<commit_msg>Align with current state of CDP backend<commit_after>#!/usr/bin/env node

/**
 * Copyright 2020 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as puppeteer from 'puppeteer';
import {
  DOMModel,
  cssPath,
} from './dom';

export default async (url: string) => {
  const browser = await puppeteer.launch({
    headless: false,
    defaultViewport: null,
    executablePath: '/usr/local/google/home/janscheffler/dev/chromium/src/out/Default/chrome',
  });

  const model = new DOMModel();
  const page = await browser.newPage();
  const session = await page.target().createCDPSession();
  session.on('DOM.setChildNodes', (e) => {
    model.setChildNodes(e);
  });

  session.on('DOM.documentUpdated', async (e) => {
    const document = await session.send('DOM.getDocument');
    model.load(document);
  });

  // Setup puppeteer
  console.log(`const puppeteer = require('puppeteer');`)
  console.log(`(async () => {`);
  console.log(`  const browser = await puppeteer.launch({headless: false, defaultViewport: null});`);
  console.log(`  const page = await browser.newPage();`);
  console.log(`  await page.goto('${url}');`);

  // Open the initial page
  await page.goto(url);

  // Get the initial document and initialse the model
  await session.send('DOM.enable');
  await session.send('DOM.startDOMEventReport');
  const document = await session.send('DOM.getDocument');
  model.load(document);

  // Queue for events to allow processing them in order
  const events = [];

  async function handleEvent() {
    const e = events.shift();
    const node = model.get(e.nodeId);
    if (!node) return;
    const selector = cssPath(node);
    if (!selector) return;

    if (e.type === 'change') {
      try {
        const value = await page.evaluate(selector => document.querySelector(selector).value, selector);
        console.log(`  await page.type('${selector}', '${value}');`);
      } catch (e) { }
    } else if (e.name === 'click') {
      console.log(`  await page.waitForSelector('${selector}', {visible: true});`);
      console.log(`  await page.click('${selector}');`);
    }

    if (events.length) {
      handleEvent();
    }
  }

  // Enqueue every DOM event for processing
  session.on('DOM.DOMEvent', async (e) => {
    events.push(e);
    if (events.length === 1) {
      handleEvent();
    }
  });

  // Finish the puppeteer script
  return new Promise(resolve => {
    browser.on('disconnected', () => {
      console.log(`})()`);
      resolve();
    });
  });
}

{'fraction_non_alphanumeric': 0.15173674588665448, 'fraction_numerical': 0.0029915240152900116, 'mean_word_length': 3.351409978308026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1823981', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1804, 'n_words': 532}