jul*_*ppy 7 c# jquery cefsharp
我试图找出这是否可能.我已经通过GitHub示例https://github.com/chillitom/CefSharp给了我类的源代码(虽然我无法从这个GITHUB构建CefSharp本身.
然后,我尝试从这个链接https://github.com/downloads/ataranto/CefSharp/CefSharp-1.19.0.7z下载二进制文件,然后通过参考这些示例构建我的C#win32应用程序,这相当顺利地进行了8个小时左右我有一个工作的嵌入式浏览器,yipeee.但是,我现在处于想要操作DOM的位置 - 我已经读过你只能使用webView.EvaluateScript("some script"); 和webView.ExecuteScript("some script"); 因为cefsharp无法提供直接DOM访问
所以我想知道的是.我可以调用jQuery方法吗?如果我加载的页面已经加载了jQuery,我可以在c#中执行以下操作吗?
webView.ExecuteScript("$(\"input#some_id\").val(\"user@example.com\")"));
Run Code Online (Sandbox Code Playgroud)
目前这引发了一个例外.我想找出来; 我是否应该尝试使用cefsharp DLL中的jQuery,或者我是否必须坚持标准的旧学校JavaScript,这将花费我5倍的时间来编写...?
我希望堆叠器有答案.我已经为cefsharp尝试了wiki和论坛,但是它们并没有提供太多的线索; 我发现的唯一例子是旧式的JavaScript.
是的,您可以使用jQuery,但只能在DOM完全加载后才能使用它.为此,您需要使用WebView的PropertyChanged事件来检查IsLoading属性何时更改为false并且IsBrowserInitialized属性设置为true.
请参阅下面的一个片段,了解我如何在我的一个项目中执行此操作.正如您所看到的,一旦IsLoading属性发生更改,我就会调用一些方法来设置WebView中的内容,这可以通过您正在执行的方式通过ExecuteScript调用jQuery来完成.
/// <summary>
/// Initialise the WebView control
/// </summary>
private void InitialiseWebView()
{
// Disable caching.
BrowserSettings settings = new BrowserSettings();
settings.ApplicationCacheDisabled = true;
settings.PageCacheDisabled = true;
// Initialise the WebView.
this.webView = new WebView(string.Empty, settings);
this.WebView.Name = string.Format("{0}WebBrowser", this.Name);
this.WebView.Dock = DockStyle.Fill;
// Setup and regsiter the marshal for the WebView.
this.chromiumMarshal = new ChromiumMarshal(new Action(() => { this.FlushQueuedMessages(); this.initialising = false; }));
this.WebView.RegisterJsObject("marshal", this.chromiumMarshal);
// Setup the event handlers for the WebView.
this.WebView.PropertyChanged += this.WebView_PropertyChanged;
this.WebView.PreviewKeyDown += new PreviewKeyDownEventHandler(this.WebView_PreviewKeyDown);
this.Controls.Add(this.WebView);
}
/// <summary>
/// Handles the PropertyChanged event of CefSharp.WinForms.WebView.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event arguments.</param>
private void WebView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// Once the browser is initialised, load the HTML for the tab.
if (!this.webViewIsReady)
{
if (e.PropertyName.Equals("IsBrowserInitialized", StringComparison.OrdinalIgnoreCase))
{
this.webViewIsReady = this.WebView.IsBrowserInitialized;
if (this.webViewIsReady)
{
string resourceName = "Yaircc.UI.default.htm";
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream))
{
this.WebView.LoadHtml(reader.ReadToEnd());
}
}
}
}
}
// Once the HTML has finished loading, begin loading the initial content.
if (e.PropertyName.Equals("IsLoading", StringComparison.OrdinalIgnoreCase))
{
if (!this.WebView.IsLoading)
{
this.SetSplashText();
if (this.type == IRCTabType.Console)
{
this.SetupConsoleContent();
}
GlobalSettings settings = GlobalSettings.Instance;
this.LoadTheme(settings.ThemeFileName);
if (this.webViewInitialised != null)
{
this.webViewInitialised.Invoke();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)