Skip to content

How to access the DOM from javascript? #29

@wataabirs

Description

@wataabirs

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:

  • I'm not allowed to modify the html content in the 'source' variable
  • I'm not sure if modifying the execution context is the correct way

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions