Hello!
As it stands, it seems that the following listener available in workers:
addEventListener('fetch', event => {})
only intercepts events dispatch by the native browser fetch
methods. This presents an issue when it is necessary to intercept, and work with XMLHttpRequest
s. I have had some luck using the ElementHandler
to insert scripts in the head of pages, to modify the XMLHttpRequest
prototype.
E.g.
...
class ElementHandler {
element(element) {
element.prepend(' <
script >
XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open;
var myOpen = function(method, url) {
// make modifications
this.realOpen(method, finalUrl);
}
//ensure all XMLHttpRequests use our custom open method
XMLHttpRequest.prototype.open = myOpen; <
/script>
', { html: Boolean })
}
}
But this is very messy! Is there any way to intercept XMLHttpRequest in the worker runtime?