Min*_*ang 4 c# jquery winforms chromium-embedded cefsharp
我正在使用ChromiumWebBrowser加载网站,在加载页面后,我将执行一些脚本
browser.ExecuteScriptAsync(script)
Run Code Online (Sandbox Code Playgroud)
但该网站不使用jquery,因此很难编写我的脚本代码.我想将jquery注入该站点以便更轻松地编写脚本.我该怎么做?非常感谢你
编辑:
我的电脑里有一个jquery文件.我想将它添加到我想要抓取数据的页面.我尝试使用LoadingStateChanged事件,但没有工作.
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
{
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
将此代码设置为script变量为字符串,然后调用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
});
}
});
})();
Run Code Online (Sandbox Code Playgroud)
我将整个jquery js文件作为内联代码推送并且效果很好:
browser.ExecuteScriptAsync(File.ReadAllText(@"content\jquery.1.11.1.min.js"));
Run Code Online (Sandbox Code Playgroud)
减少加载时间......