How to modify window.document with mitmproxy?

Hi All,
I’m trying to use mitmproxy and python scripts to intercept both request and response to change some window.document properties on the fly.

I’ve sucessfully run mitmproxy examples to simply replace HTML page content with python’s string replace function, but it is not the case here. I’ve browsed documentation and examples, but cannot find any way to access this top level objects of HTML DOM.

What I expect is the mitmproxy to change some window.document properties in the request before the server receive it and to modify the response content before the client software receive it.

Is there anyway to access and modify document’s DOM properties in mitmproxy?