Equivalent of onbeforescriptexecute in Chrome
javascript
Solution
Here is a polyfill of `onbeforescriptexecute` event: https://gist.github.com/jspenguin2017/cd568a50128c71e515738413cd09a890
It works on Chrome and Edge, but ironically not on Firefox.
To print the script content, just do `console.log(e.script.textContent);` inside the event handler. You can call `e.replacePayload(payload);` to change the script to run, or `e.preventDefault()` to block the script.
`e.preventDefault();` takes precedence over `e.replacePayload(...);`, if `e.replacePayload(...);` is called multiple times, the last call will override all previous calls. You can call `e.stopPropagation();` to ensure your new payload gets registered.
Unfortunately, `textContent` does not always work properly for huge scripts. It should be fine for debugging. Also, it only works for "hard coded" inline scripts, not those dynamically injected from other scripts. You can still monitor those scripts, but you can't interfere with their execution.
It's a bit tricky for `onblur`, you might have to use a custom observer that scans for it.
Problem
Is there an equivalent of the onbeforescriptexecute or beforescript execute event in Chrome? I would like to intercept and print out the javascript code that is being executed before any javascript on the page is loaded. This should include Javascript within script tags and other attributes like onblur, etc. Thanks