CEFsharp浏览器注入jQuery

时间:2018-10-04 04:03:36

标签: c# winforms cefsharp

我试图将jquery注入到CEFsharp浏览器中,但是它不起作用,不知道为什么!

这是我尝试过的代码片段。还有其他方法可以将jQuery加载到CEFsharp浏览器中的加载页面

 string query = "(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
                });
            }
        });
    })();";

     LoginWebbrowser.ExecuteScriptAsync(query);

1 个答案:

答案 0 :(得分:0)

string jq = @"var scr = window.document.createElement('script');
scr.type = 'text/javascript';
scr.src = 'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js';
var head = window.document.getElementsByTagName('head')[0];
head.appendChild(scr);"
LoginWebbrowser.EvaluateScriptAsync(jq);