注意下面的代码
async function handleRequest(req) {
const res = await fetch(req)
return rewriter.transform(res)
}
class AttributeRewriter {
constructor(attributeName) {
this.attributeName = attributeName
}
element(element) {
const attribute = element.getAttribute(this.attributeName)
if (attribute) {
element.setAttribute(
this.attributeName,
attribute.replace('/product/', '/p-'),
)
}
}
}
const rewriter = new HTMLRewriter()
.on('a', new AttributeRewriter('href'))
.on('link', new AttributeRewriter('href'))
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
此代码工作正常说明:我将用此代码替换/product/与/p-现在我将用b.com和static.a.com替换这些a.com值与cdn.b.com 我'将在这段代码中全部替换它们