JavaScript evaluation fails with the message: $ is not defined

0

VS 2019, .NET 5, AngleSharp* V0.14.0

I am looking for some clarity on the steps needed to evaluate the JavaScript inside a script tag. Here is the JavaScript I am trying to evaluate:

    <script>
    $(function () {      
        $("#TextBoxPIN").bind('focus mouseenter', function () {
            $("#PIN").css("color", "#fc4258");
        });

        $("#TextBoxPIN").bind('blur mouseleave', function () {
            $("#PIN").css("color", "yellow");
        });
    });


    if (window.requestIdleCallback) {
        requestIdleCallback(function () {
            Fingerprint2.get(function (components) {
                var murmur = Fingerprint2.x64hash128(components.map(function (pair) { return pair.value }).join(), 31);
                $("#Fingerprint").val(murmur);
            })
        })
    } else {
        setTimeout(function () {
            Fingerprint2.get(function (components) {
                var murmur = Fingerprint2.x64hash128(components.map(function (pair) { return pair.value }).join(), 31);
                $("#Fingerprint").val(murmur);
            })
        }, 500)
    }

    function isIE() {
        // IE 10 and IE 11
        return /Trident\/|MSIE/.test(window.navigator.userAgent);
    }

    $(document).ready(function () {        
        if (isIE() == true) {
            $('#NOIE').addClass('BoldAndRed');
    }
    });
</script>

I initially configured my parser context as:

        var requester = new HttpClientRequester(_httpClient);
        _parserContext = BrowsingContext.New(
            Configuration.Default.With(requester)
                .WithDefaultLoader()
                .WithCss()
                .WithJs()
                .WithEventLoop()
            );

Here is the code block that attempts to evaluate the JavaScript:

        var js = doc.QuerySelector<IHtmlScriptElement>("div.main div.width script");
        if (js == null)
            throw new ApplicationException("Could not find the fingerprint script block");

        var results = doc.ExecuteScript(js.Text);

When configured as above, it looks like jQuery is not being downloaded. Therefore, I am getting the error "$ is not defined".

I have structured my code as per the Extension Methods documentation from the github repo.

If I pass a LoadOptions instance, with IsResourceLoadingEnabled = true, I get two different errors. If I have the CSS engine loaded I get this error:

  System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=AngleSharp.Css
  StackTrace:
   at AngleSharp.Css.FeatureValidators.WidthFeatureValidator.Validate(IMediaFeature feature, IRenderDevice renderDevice)

If I do not load the CSS engine, I get the following:

    {""}
    Column: 32287
    Data: {System.Collections.ListDictionaryInternal}
    Error: {TypeError}
    HResult: -2146233088
    HelpLink: null
    InnerException: null
    LineNumber: 2
    Location: {Jint.Parser.Location}
    Message: ""
    SerializationRemoteStackTraceString: null
    SerializationStackTraceString: "   at Jint.Engine.Execute(Program program)
   at Jint.Engine.Execute(String source)
   at AngleSharp.Js.EngineInstance.RunScript(String source, JsValue context)
   at AngleSharp.Js.EngineExtensions.RunScript(EngineInstance engine, String source)
   at AngleSharp.Scripting.JsScriptingService.EvaluateScript(IDocument document, String source)
   at AngleSharp.Js.JsApiExtensions.ExecuteScript(IDocument document, String scriptCode)
    ......
    SerializationWatsonBuckets: null
    Source: "jint"
    StackTrace: "   at Jint.Engine.Execute(Program program)
   at Jint.Engine.Execute(String source)
   at AngleSharp.Js.EngineInstance.RunScript(String source, JsValue context)
   at AngleSharp.Js.EngineExtensions.RunScript(EngineInstance engine, String source)
   at AngleSharp.Scripting.JsScriptingService.EvaluateScript(IDocument document, String source)
   at AngleSharp.Js.JsApiExtensions.ExecuteScript(IDocument document, String scriptCode)
   .......
    TargetSite: {Jint.Engine Execute(Jint.Parser.Ast.Program)}
    _HResult: -2146233088
    _data: {System.Collections.ListDictionaryInternal}
    _dynamicMethods: null
    _errorObject: {TypeError}
    _exceptionMethod: null
    _helpURL: null
    _innerException: null
    _ipForWatsonBuckets: 0x00007ffb8390b766
    _message: ""
    _remoteStackTraceString: null
    _source: null
    _stackTrace: {byte[384]}
    _stackTraceString: null
    _watsonBuckets: null
    _xcode: -532462766
    _xptrs: 0x0000000000000000

What is the correct approach to use to have all the scripts (or certain scripts) from a web site loaded, so that evaluation of JS functions can happen?

I did configure a class that derived from IConsoleLogger by

.WithConsoleLogger( ctx => new StandardConsoleLogger())

My implementation was never called.

sealed class StandardConsoleLogger : IConsoleLogger
{
    public void Log(Object[] values)
    {
        var elements = values.Select(m => (m ?? String.Empty).ToString());
        var content = String.Join(", ", elements);
        System.Diagnostics.Debug.WriteLine(content);
    }
}

Thank you for the guidance. It is appreciated.

javascript
anglesharp
asked on Stack Overflow Feb 14, 2021 by mhsimkin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0