Alin
Alin

Reputation: 382

Inject javascript into HtmlDocument in C#

Is there a simple way to inject a javascript into a HtmlDocument object without using PhantomJS? Is there such a method in HtmlAgilityPack.HtmlDocument class?

Any advice is apreciated, Thank you!

Upvotes: 2

Views: 663

Answers (2)

You can inject a script tag (with script in it) in a HTMLDocument.

You still need something to execute the javascript though. If you want your c# code to somehow evaluate the result of the javascript - especially if you are doing dom manipulation - then PhantomJS is probably the best way to go as it's a headless browser that you can run without user interaction and interface.

The short answer is yes, but i think what you want is not possible without an engine that knows both javascript and the html dom. There are alternatives to PhantomJS like http://slimerjs.org/ so my cheeky answer is still yes ;)

Hint: Don't be afraid to run PhantomJS or the like.

Upvotes: 1

Dani Ribas
Dani Ribas

Reputation: 54

You can create the element with

HtmlElement script = doc.CreateElement("SCRIPT");

Set the value with the script that you want and then attach to the Header of the HtmlDocument

Upvotes: 1

Related Questions