[英]Setting attribute using HTMLRewriter on Cloudflare worker
I am trying to set the value attribute of my #email field via a Cloudflare worker.我正在尝试通过 Cloudflare 工作人员设置我的 #email 字段的 value 属性。
I define my email as test@test.com
but how can I send this into ElementHandler
?我将我的电子邮件定义为test@test.com
但如何将其发送到ElementHandler
?
At the moment I am able to set the value attribute to be XXXX
and this works successfully.目前我能够将 value 属性设置为XXXX
并且这成功地工作。
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:您可以为ElementHandler
类提供一个构造函数,该构造函数接受您存储在对象属性中的参数:
class ElementHandler {
constructor(value) {
this.value = value;
}
element(e) {
e.setAttribute('value', this.value)
}
}
Then construct it like:然后像这样构造它:
new ElementHandler(email)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.