async function handleRequest(request) {
    const response = await fetch(request)
    return new HTMLRewriter().on("head", new HeadHandler()).on("body", new BodyHandler()).transform(response)
}

class HeadHandler {
    element(element) {
        element.append(jsToInsertHead, {html: true})
    }
}

class BodyHandler {
    element(element) {
        element.append(jsToInsertBody, {html: true})
    }
}

const jsToInsertHead = `
<!-- Insert By ECA -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.0/anime.min.js"></script>
`

const jsToInsertBody = `
<!-- Insert By ECA -->
<script>
// Create animation
var animation = anime({
  targets: '#navbarCollapse',
  translateX: 0,
  rotate: '1turn',
  backgroundColor: '#FFF',
  duration: 2000,
  loop: true
});

// Automatically play animation
animation.play();
</script>
`
                    
addEventListener("fetch", event => {
    return event.respondWith(handleRequest(event.request))
})