简体   繁体   中英

Setting attribute using HTMLRewriter on Cloudflare worker

I am trying to set the value attribute of my #email field via a Cloudflare worker.

I define my email as test@test.com but how can I send this into ElementHandler ?

At the moment I am able to set the value attribute to be XXXX and this works successfully.

async function handleRequest(request) {

  const newResponse = new Response(response.body, response)

  const email = 'test@test.com';

  return new HTMLRewriter()
     .on("#email", new ElementHandler())
     .transform(newResponse)

}

class ElementHandler {
   element(e) {
     e.setAttribute('value', 'XXXX')
   }
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

You can give your ElementHandler class a constructor that accepts parameters which you store in properties of the object:

class ElementHandler {
  constructor(value) {
    this.value = value;
  }

  element(e) {
    e.setAttribute('value', this.value)
  }
}

Then construct it like:

new ElementHandler(email)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM