Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active April 19, 2022 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogweather/53adbd5619103e80676df43ca6b0e592 to your computer and use it in GitHub Desktop.
Save dogweather/53adbd5619103e80676df43ca6b0e592 to your computer and use it in GitHub Desktop.
class AttributeWriter {
constructor(cf_object) {
this.cf_object = cf_object;
}
async element(element) {
const data = this.cf_object;
// Is the visitor in the VIP list?
const visitor_asn = data.asn
const network_name = await VIP_API_ASN.get(visitor_asn)
const is_vip = (network_name !== null)
const message = 'vip: ' + (is_vip ? 'yes' : 'no ') + '; ' + data.asn.toString().padStart(7) + ' - ' + data.asOrganization
// Log the result for debugging
console.log(message);
// Pass the result back as an HTML attribute so that
// JS in the browser can access it.
element.setAttribute('data-cf-vip', message)
}
}
async function handleRequest(request) {
response = await fetch(request);
return new HTMLRewriter().on('body', new AttributeWriter(request.cf)).transform(response);
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment