cefsharp 注入jquery脚本文件获取

5

I'm using ChromiumWebBrowser to load a website, and after page loaded, I'll execute some script

browser.ExecuteScriptAsync(script)

But that website not use jquery, so difficult to code my script. I want to inject jquery into that site to write my script easier. How can I do it? Thank you very much

EDIT:

I have had a jquery file in my computer. And I would like to add it to the page where I want to crawl data. I tried using LoadingStateChanged event, but not worked.

private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
    ChromiumWebBrowser browser = (ChromiumWebBrowser)sender;
    lbStatus.SetText(e.IsLoading ? "Loading..." : browser.Address);
    if (!e.IsLoading)
    {
        //Load jquery
    }
    else
    {

    }
}
share  improve this question   
  •  
    You can check if the page has access to jQuery, and if not add it from a CDN, if (!jQuery) { /* load from CDN */ }. See here for how to do the latter – Rory McCrossan May 3 '16 at 8:33
  • 1
    How can I add script from local file? I'm trying use ChromiumWebBrowser to crawl data – Minh Giang May 4 '16 at 6:25

3 Answers

10
 

Set this code to script variable as string and then call browser.ExecuteScriptAsync(script)

(function () {
    // more or less stolen form jquery core and adapted by paul irish
    function getScript(url, success) {
        var script = document.createElement('script');
        script.src = url;
        var head = document.getElementsByTagName('head')[0],
            done = false;
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function () {
            if (!done && (!this.readyState
                || this.readyState == 'loaded'
                || this.readyState == 'complete')) {
                done = true;
                success();
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);
            }
        };
        head.appendChild(script);
    }
    getScript('http://code.jquery.com/jquery-latest.min.js', function () {
        if (typeof jQuery == 'undefined') {
            console.log('Sorry, but jQuery wasn\'t able to load');
        } else {
            console.log('This page is now jQuerified with v' + $.fn.jquery);

            $(document).ready(function () {
                alert(1);
                //here you can write your jquery code
            });
        }
    });
})();
share  improve this answer   
  •  
    @amirpaia by this code we can inject any JS file, right? – Amogh Nov 19 '16 at 7:16
  •  
    @Amogh yes you can – amirpaia Nov 21 '16 at 7:23
  •  
    @amirpaia I am able to inject other any JS file, thanks to your snippet but I also want to inject CSS, any idea? – Amogh Nov 25 '16 at 14:46
  •  
    @Amogh you can replace document.createElement('script') with document.createElement("link") and set href property of <link> tag – amirpaia Dec 4 '16 at 7:05 
  •  
    Awesome; thanks a lot for share... but; this could be blocked by some security procedure? – MiBol Oct 26 '17 at 0:16
  • 1
    @MiBol I don't think so because your script runs on the page like other scrips. – amirpaia Oct 28 '17 at 9:26
  •  
    I needed to change the protocol part of the jquery url, as such: location.protocol + '//code.jquery.com/jquery-latest.min.js' – Dan Rayson May 29 '18 at 18:36
 
6

i push the entire jquery js-file as inline-code and works great:

browser.ExecuteScriptAsync(File.ReadAllText(@"content\jquery.1.11.1.min.js"));

reduces loading times...

share  improve this answer   
  •  
    what's not working? Is there an error message? Sometimes conflicts can occur if another jQuery version has already been loaded into the page. Otherwise I had never had any problems with this variant. – MaxKlaxx Mar 11 '19 at 13:36
0

it's very easy :)

 string script_1 = "document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src = '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'";

browser.ExecuteScriptAsync(script_1);

and for call any custom jquery function or class:

 string script_2 = "$('.contact').css('color','#aabbcc');";

 browser.ExecuteScriptAsync(script_2);
share  improve this answer   

Your Answer

 
 
 

By clicking “Post Your Answer”, you agree to our terms of serviceprivacy policy and cookie policy

posted @ 2020-09-13 18:28  IDOER  阅读(1729)  评论(0编辑  收藏  举报