简体   繁体   中英

C#, WebBrowser, how to inject and execute JavaScript before any other script on the page?

I know how to inject my own scripts into web pages on load event, but it doesn't always work as expected. Sometimes the page succeeds to run its own scripts before mine and it makes my whole app fail. I try do disable the possibility the page spawns another browser window. I have an app running on my server, when it starts Internet Explorer instances randomly - it soon crashes the whole machine which is a disaster.

I made a script which changes window.open method and it does the trick perfectly - except the case when the page pops up another window BEFORE my code is executed. Is there a way to freeze JS before I finish injecting my script? Maybe is there a way to inject my script in an earlier point, before DocumentCompleted event? But how?

This doesn't directly answer your question about how to do it in the browser - but I'm aware of a similar thing that is done by Avast internet security and it does it using an HTTP proxy.

The idea is to intercept HTML pages as they are received over the wire, and inject your script into the HTML itself; thus there's no way that any other script can execute before it.

Whilst that might sound scary - you can do it using the technology underpinning the very excellent Fiddler HTTP debugger - the FiddlerCore API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM