Last active
April 19, 2022 01:18
-
-
Save dogweather/53adbd5619103e80676df43ca6b0e592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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