C# 我已成功地将CefSharp嵌入到.NET4.0应用程序中。可以让jQuery调用在DOM上工作吗?
我想知道这是否可行。我已经阅读了GitHub示例,该示例为我提供了类的源代码(尽管我无法从这个GitHub构建CefSharp本身) 然而,我确实尝试了从这个链接下载二进制文件,然后通过引用这些示例构建了我的C#win32应用程序,这相当顺利,大约8个小时后,我得到了一个可以工作的嵌入式浏览器Yipee。然而,我现在正处于我想要操纵DOM的位置——我已经读到,你只能用webView.E来实现这一点评估脚本(“某些脚本”)和webView.ExecuteScript(“某些脚本”),因为无法通过cefsharp直接访问DOM 我想知道的是,我可以调用jQuery方法吗?如果我加载的页面已经加载了jQuery,我可以在c#中执行以下操作吗 目前,这引发了一个异常。我正在尝试找出答案;我甚至应该尝试从cefsharp DLL使用jQuery,还是必须坚持使用标准的老式JavaScript,这将花费我5倍的时间来编写C# 我已成功地将CefSharp嵌入到.NET4.0应用程序中。可以让jQuery调用在DOM上工作吗?,c#,jquery,cefsharp,C#,Jquery,Cefsharp,我想知道这是否可行。我已经阅读了GitHub示例,该示例为我提供了类的源代码(尽管我无法从这个GitHub构建CefSharp本身) 然而,我确实尝试了从这个链接下载二进制文件,然后通过引用这些示例构建了我的C#win32应用程序,这相当顺利,大约8个小时后,我得到了一个可以工作的嵌入式浏览器Yipee。然而,我现在正处于我想要操纵DOM的位置——我已经读到,你只能用webView.E来实现这一点评估脚本(“某些脚本”)和webView.ExecuteScript(“某些脚本”),因为无法通过c
我希望堆叠者会有答案。我尝试过cefsharp的Wiki和论坛,但它们没有提供太多线索;我发现的唯一例子是老式JavaScript。是的,你可以使用jQuery,但是你只能在DOM完全加载后使用它。要做到这一点,你需要使用PropertyChanged当IsLoading属性更改为false且IsBrowserInitialized属性设置为true时,要检查的WebView事件 下面是我如何在我的一个项目中执行此操作的一个片段。正如您所看到的,一旦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();
}
}
}
}
//
///初始化WebView控件
///
私有void InitialiseWebView()
{
//禁用缓存。
BrowserSettings=新建BrowserSettings();
settings.ApplicationCacheDisabled=true;
settings.PageCacheDisabled=true;
//初始化WebView。
this.webView=新的webView(string.Empty,设置);
this.WebView.Name=string.Format(“{0}WebBrowser”,this.Name);
this.WebView.Dock=DockStyle.Fill;
//设置并重新设置WebView的封送处理。
this.chromiumMarshal=new chromiumMarshal(新操作(()=>{this.FlushQueuedMessages();this.initialising=false;}));
this.WebView.RegisterJsObject(“封送”,this.chromiumMarshal);
//设置WebView的事件处理程序。
this.WebView.PropertyChanged+=this.WebView\u PropertyChanged;
this.WebView.PreviewKeyDown+=新的PreviewKeyDownEventHandler(this.WebView\u PreviewKeyDown);
this.Controls.Add(this.WebView);
}
///
///处理CefSharp.WinForms.WebView的PropertyChanged事件。
///
///事件的来源。
///事件参数。
私有void WebView_PropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
//浏览器初始化后,加载选项卡的HTML。
如果(!this.webviewsready)
{
if(e.PropertyName.Equals(“IsBrowserInitialized”,StringComparison.OrdinalIgnoreCase))
{
this.WebView.isready=this.WebView.isbrowseriInitialized;
如果(this.webviewsready)
{
字符串resourceName=“Yaircc.UI.default.htm”;
使用(Stream Stream=Assembly.getExecutionGassembly().GetManifestResourceStream(resourceName))
{
使用(StreamReader=新StreamReader(stream))
{
this.WebView.LoadHtml(reader.ReadToEnd());
}
}
}
}
}
//一旦HTML完成加载,就开始加载初始内容。
if(e.PropertyName.Equals(“IsLoading”,StringComparison.OrdinalIgnoreCase))
{
如果(!this.WebView.IsLoading)
{
这个.SetSplashText();
if(this.type==IRCTabType.Console)
{
this.SetupConsoleContent();
}
GlobalSettings=GlobalSettings.Instance;
此.LoadTheme(settings.ThemeFileName);
如果(this.webview已初始化!=null)
{
this.webviewsinitialized.Invoke();
}
}
}
}
您在评论中更清楚地表明,您要做的是将jQuery加载到一个尚未加载jQuery的页面中。int0x90关于在jQuery源代码的本地副本上运行ExecuteScript
的建议可能适合您。不过,我想提醒您,许多页面将无法与其作者提供的所有额外JS兼容rs从来没有这样做过。谷歌和Facebook就是两个大的例子。它们都定义了一个不是jQuery的$
操作符,所以踩在上面几乎肯定会打破它们
底层CEF库公开了许多直接从C++操纵DOM元素的方法,CEFApple还没有公开,因为它们还没有太多的需求。虽然这听起来是您想在这里使用的,但是如果您查看CEFHARP源,则可能不需要太多的工作来公开它们。我自己还没有试过。如果你想试一下,你也可以将问题发布到
JQuery在CefSharp中应该可以很好地工作,因为ExecuteScript只运行你在全局范围内给出的所有内容。你能更具体地解释一下“抛出”是什么意思吗?在尝试使用jQuery之前,您可能想使用getElementById等简单的旧JS编写一些测试代码,并确认其工作正常,以减少可能出现的错误。是的,这样做当然是个好主意,事实上,我让简单的旧JS工作得很好,但是当尝试使用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();
}
}
}
}