A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/AngleSharp/AngleSharp.Js/issues/29 below:

How to access the DOM from javascript? · Issue #29 · AngleSharp/AngleSharp.Js · GitHub

Hi,

I try to use the following code to access a DOM object inside the HTML:

var config = Configuration.Default.WithJavaScript().WithCss();
var parser = new HtmlParser(config);
// sample html
var source = @"<!doctype html><html><body><div id='test'>content</div></body>";
var document = parser.Parse(source);
var jsService = config.Services.OfType<JavaScriptProvider>().FirstOrDefault(x => x.Engine.Type == "text/javascript");
var jintEngine = jsService.Engine.GetOrCreateJint(document);
// trying to modify the context, but I don't know how to specify the correct thisBinding
jintEngine.EnterExecutionContext(jintEngine.GlobalEnvironment, jintEngine.GlobalEnvironment, ???);

// try to extract the contents of the div with JS
var result = jintEngine.Execute("document.querySelectorAll('#test').innerHTML").GetCompletionValue();

jintEngine.LeaveExecutionContext();
Console.WriteLine(result);

Some remarks:

The problem:
When I run this code (without modifying the execution environment) I get a JavascriptException saying that the 'document' is undefined. I've tried to modify the execution environment, but I have no idea what the correct value for the thisBinding parameter is in the EnterExecutionContext function. How can I set it to the window or the document?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4