Wrangler Workers: HTMLRewriter.on with ElementHandler not working

348 views Asked by At

I'm trying to use my Worker to rewrite the contents of an HTML page with this syntax:

async function handleIndex(request) {
    const init = {
        headers: {
          "content-type": "text/html;charset=UTF-8",
        },
      }

    const response = await fetch(HTML_URL, init)

    return new HTMLRewriter()
        .on(
          "div",
          new ElementHandler()
        )
        .transform(response)
}

This is directly taken from the HTMLRewriter documentation, but it's giving me this error whenever I hit the endpoint that calls it:

Uncaught (in promise)
TypeError: The "listener" argument must be of type Function. Received type object
    at c (worker.js:1:2687)
    at l (worker.js:1:2897)
    at u.addListener (worker.js:1:5518)
    at worker.js:1:1327

It's my first time using Workers; what am I doing wrong?

1

There are 1 answers

0
Jasper Huang On

FIXED: Some IDEs will suggest you import HTMLRewriter from ‘event’. DON’T DO THIS!