Welcome Guest Search | Active Topics | Sign In | Register

Injecting jquery in EO webbrowser Options
khurram.saddique
Posted: Wednesday, January 11, 2023 12:21:17 PM
Rank: Newbie
Groups: Member

Joined: 7/31/2017
Posts: 2
Hi Support,
I am having hard time in injecting jquery after load url is complete . Let me show you my code.
I have tried two ways.
first i tried
Code: C#
public WebView webView { get; set; }
 if (listSiteIds.Count > 0)
            {
                ParallelOptions parallelOptions = new ParallelOptions
                {
                    MaxDegreeOfParallelism = 50
                };
                Parallel.ForEach(listSiteIds, parallelOptions, siteId =>
                {
                    var eoThreadRunner = new ThreadRunner(siteId.ToString());

                    eoThreadRunner.Send(() =>
                    {
                        previewTasks.Add(Task.Factory.StartNew(() =>
                        {
                            previewGenerator = new PreviewGeneratorEOHeadless(eoThreadRunner.CreateWebView(), siteId);
                            previewGenerator.StartPreviewingAsync();
                        }));
                    });

                });
                _defaultDelay = minDelayInSeconds;
            }
 public async Task StartPreviewingAsync(){
// do some initialization and load url
InjectJquery();

}
private string GetJqueryVersion()
        {
            try
            {
                return webView.EvalScript("return $.fn.Jquery").ToString();
            }
            catch (Exception exception)
            {

            }
            return "";
        }
public void InjectJquery(){
 string jQueryUrl = string.Format("window.setTimeout(function() {{ {0} }}, 5);", Properties.Resources.jquery_min); ;
webView.EvalScript($"var jq = document.createElement('script'); jq.src = '{jQueryUrl}'; document.getElementsByTagName('head')[0].appendChild(jq);");
}

GetJqueryVersion always return nothing it should return the version of jquery

second thing tried
Code: C#
string jqueryUrl = JsPath + "jqueryMinv1_11_2.js";
                    webView.EvalScript("var script = document.createElement('script');" +
                                   "script.src = '" + jqueryUrl + "';" +
                                     "document.getElementsByTagName('head')[0].appendChild(script);");

and when i invoke jquery function it gives exception $ is not defined whats wrong with the code
eo_support
Posted: Wednesday, January 11, 2023 12:42:21 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 23,576
Hi,

You should not use Async methods to call EvalScript. EvalScript should be called inside the same thread as the WebView (directly inside your eoThreadRunner.Send or Post).

If that does not resolve the issue, you can try to use the built-in debuging feature to debug the web page and see if any error has occurred in the script code:

https://www.essentialobjects.com/doc/webbrowser/advanced/debug.html

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.