1

I have this puppeteer script, which should translate CSharp roughly to TypeScript, running against this website http://www.carlosag.net/tools/codetranslator/ :

const puppeteer = require('puppeteer')
const fs = require('fs')

const wait = ms => new Promise(res => setTimeout(res, ms))

start()

async function start() {
  let b = await puppeteer.launch({
    browserContext: 'default',
    headless: false,
    args: ['--disable-dev-shm-usage'],
  })
  let p = await b.newPage()
  p.setUserAgent(
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
  )

  await visit('http://www.carlosag.net/tools/codetranslator/')

  await p.waitForSelector('#_code')
  // await p.evaluate(
  //   () => (document.querySelector('#_colorize').checked = false),
  // )

  async function visit(url, i = 0) {
    console.log(new Array(i + 1).join('  ') + url)
    try {
      await p.goto(url)
    } catch (e) {
      // console.log(e)
      await b.close()
      b = await puppeteer.launch()
      p = await b.newPage()
      await visit(url, i)
    }
  }

  await translateCode(
    'tst.ts',
    `namespace R3.Geometry
{
  using Math = System.Math;
  using R3.Core;
  using System.Diagnostics;

  public static class Euclidean2D
  {
  }
}`,
  )

  // await b.close()

  async function translateCode(path, code) {
    await p.evaluate(() => {
      document.querySelector('#_src').selectedIndex = 2
    })

    await p.evaluate(() => {
      document.querySelector('#_result').innerHTML = ''
    })

    await p.evaluate(code => {
      document.querySelector('#_code').value = code
      var event = new Event('change')
      document.querySelector('#_code').dispatchEvent(event)
    }, code)

    // await p.evaluate(() => {
    //   // Translate(true)
    // })

    let i = 0
    while (i < 10) {
      console.log('before', i)
      
      await wait(500)
      const output = await p.evaluate(() => {
        return (
          document.querySelector('#_result')?.innerText?.trim() ??
          'dang'
        )
      })

      console.log('after', i, output)
      i++

      if (output) {
        fs.writeFileSync(path, output)
        break
      }
    }
  }
}

Every time, in headed mode it fails and exits at this line:

const output = await p.evaluate(() => {
  return (
    document.querySelector('#_result')?.innerText?.trim() ??
    'dang'
  )
})

It exits with this error every time.

./node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:329
                error: new Errors_js_1.ProtocolError(),
                    ^

ProtocolError: Protocol error (Runtime.callFunctionOn): Target closed.
    at ./node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:329:24
    at new Promise (<anonymous>)
    at CDPSessionImpl.send (./node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:325:16)
    at ExecutionContext._ExecutionContext_evaluate (./node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:211:46)
    at async ExecutionContext.evaluate (./node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)

Node.js v19.1.0

My logs say:

before 0
after 0
before 1

So it gets past the first loop at least. My hunch is it is getting the request response in the Translate method, but for some reason is throwing an error, and I don't know how to better debug this.

I checked around the web for that error and found https://stackoverflow.com/a/51989560/169992, but it says it is likely a "run out of memory" issue or calling browser.close() on puppeteer early. I don't think this website http://www.carlosag.net/tools/codetranslator/ has a memory problem, and I am certain I am not calling browser.close early. How can I better debug what is going on? Or better yet, how can I get past this error?

The script I posted should run as is.

I tried running my script.

Attribute
  • 11
  • 2
  • You need to `await` your `setUserAgent` call. Beyond that, this is a rather unusual setup on many levels. What are you trying to achieve on this site exactly? I wouldn't create any functions until your code works. Correctness first, then abstraction. – ggorlen Jan 18 '23 at 16:13

0 Answers0