Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of onbeforescriptexecute in Chrome

Tags:

javascript

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

like image 902
everconfusedGuy Avatar asked Oct 22 '25 03:10

everconfusedGuy


1 Answers

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.

like image 136
Marius Tancredi Avatar answered Oct 23 '25 17:10

Marius Tancredi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!