Return values from HTMLRewriter to main stream

Hello, I want to parse html tags data using HTMLRewriter and use it in main stream, for example

const hreflangs = new HTMLRewriter()
    .on('link[rel="alternate"]', {
        element(e) {
            // get attributes to read tags
            // not all meta tags have a name and value
            let value = e.getAttribute("hreflang")
            console.log("value:", value) <-- I need this
        }
    });

  html = hreflangs.transform(html); 

Of course I can use some workaround as

let html2 = html.clone();
let htmlbody = await html2.text()

or extract text and generate new Response then, but may be it is possible using rewriter ?

You’d need to do text() on the ‘transformed’ HTML from HTMLRewriter since otherwise, HTMLRewriter wouldn’t have been invoked yet to populate any arrays.

You can see how I’m doing it in this project - I need to get the source attributes of certain elements and use them within my Worker so I buffer the transformed HTML.

1 Like

Thanks - but question is mainly about returning something from rewriter.
As a simple example I need all “href” attributes fired from .on(‘a[class=“link”]’)

And that’s exactly what you can do in the linked example.

1 Like