使用CEFSHARP逃脱JavaScript和HTML字符串
上下文:
我想使用嵌入Tinymce JS的CEFSHARPBROWSER加载HTML代码。
正确执行此操作的方法是:
Dim browser As ChromiumWebBrowser
Dim strHTMLPageCode As String = "<p>Hello</p>"
browser.GetMainFrame.ExecuteJavaScriptAsync("tinymce.activeEditor.setContent('" & strHTMLPageCode & "');"))
问题:
当strhtmlpagecode获得“真实” html(带有line feed,tag等)时,它不起作用,例如
<html>
<head>
<title>Href Attribute Example</title>
</head>
<body>
<h1>Href Attribute Example</h1>
<p>
<a href="https://www.freecodecamp.org/contribute/">The freeCodeCamp Contribution Page</a> shows you how and where you can contribute to freeCodeCamp's community and growth.
</p>
</body>
</html>
,我必须逃脱字符串。
我尝试过
browser.GetMainFrame.ExecuteJavaScriptAsync("tinymce.activeEditor.setContent('" & HttpUtility.HtmlEncode(strHTMLPageCode) & "');"))
browser.GetMainFrame.ExecuteJavaScriptAsync(HttpUtility.Javascriptencode("tinymce.activeEditor.setContent('" & HttpUtility.HtmlEncode(strHTMLPageCode) & "');")))
browser.GetMainFrame.ExecuteJavaScriptAsync(HttpUtility.JavaScriptStringEncode("tinymce.activeEditor.setContent('") & HttpUtility.HtmlEncode(strHTMLPageCode) & "');")
这些都没有用。
Context:
I want to load HTML code using a CEFSharpBrowser embedding a TinyMce JS.
The correct way to do it is:
Dim browser As ChromiumWebBrowser
Dim strHTMLPageCode As String = "<p>Hello</p>"
browser.GetMainFrame.ExecuteJavaScriptAsync("tinymce.activeEditor.setContent('" & strHTMLPageCode & "');"))
Issue:
It doesn't work when strHTMLPageCode got "real" html (with line feed, tags, etc) e.g.
<html>
<head>
<title>Href Attribute Example</title>
</head>
<body>
<h1>Href Attribute Example</h1>
<p>
<a href="https://www.freecodecamp.org/contribute/">The freeCodeCamp Contribution Page</a> shows you how and where you can contribute to freeCodeCamp's community and growth.
</p>
</body>
</html>
So, i had to escape the string.
I tried
browser.GetMainFrame.ExecuteJavaScriptAsync("tinymce.activeEditor.setContent('" & HttpUtility.HtmlEncode(strHTMLPageCode) & "');"))
browser.GetMainFrame.ExecuteJavaScriptAsync(HttpUtility.Javascriptencode("tinymce.activeEditor.setContent('" & HttpUtility.HtmlEncode(strHTMLPageCode) & "');")))
browser.GetMainFrame.ExecuteJavaScriptAsync(HttpUtility.JavaScriptStringEncode("tinymce.activeEditor.setContent('") & HttpUtility.HtmlEncode(strHTMLPageCode) & "');")
None of these worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于HTML字符串,该字符串具有“非规则”线供稿。
我能够看到此问题,这要归功于开发器控制台,这要感谢:
browser.showdevtoolsdocked(me)
The issue was in the html string which had "non regular" line feed.
I was able to see this issue thanks to the developper console, displayable thanks to :
browser.ShowDevToolsDocked(Me)