javascript - Awesomium - vb.net 获取 html 内容

标签 javascript vb.net tinymce-4 awesomium

我通过 Awesomium 浏览器在 Windows 窗体中使用 TinyMce。我创建了一个像这样的用户控件,并将其放置在 Windows 窗体上:

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports Awesomium.Core
Imports Awesomium.Windows

Partial Public Class TinyMCE
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Property HtmlContent() As String
        Get
            Dim content As String = String.Empty

            If Not WebCore.IsInitialized Then
                WebCore.Initialize(New WebConfig() With {.LogLevel = LogLevel.Verbose})
            Else

                Dim html As JSValue = webBrowserControl.ExecuteJavascriptWithResult("GetContent")

                content = html.ToString

            End If

            Return content
        End Get
        Set(value As String)
            If WebCore.IsInitialized Then
                Dim objResult As JSObject = webBrowserControl.ExecuteJavascriptWithResult("window")
                objResult.InvokeAsync("SetContent", value)
            End If

        End Set
    End Property

    Public Sub CreateEditor()
        If File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html\template\tinymce\js\tinymce\tinymce.min.js")) Then
            webBrowserControl.Source = New Uri("file:///" & Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html\template\tinymce.htm").Replace("\"c, "/"c))
        Else
            MessageBox.Show("Could not find the tinyMCE script directory. " & (Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html\template\tinymce\js\tinymce\tinymce.min.js")), "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub
End Class

就 TinyMCE 而言,我的 HTML 相当简单:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title>tinyMCE WYSIWYG Editor</title>
</head>

<script type="text/javascript" src="tinymce/js/tinymce/tinymce.min.js"></script>

<!-- Script functions to expose tinyMCE internals that get called from code using InvokeScript method. -->
<script type="text/javascript">
  function GetContent() {
    return tinyMCE.get('tinyMceEditor').getContent();
  }

  function SetContent(htmlContent) {
    tinyMCE.get('tinyMceEditor').setContent(htmlContent);
  }
</script>

<!-- TinyMCE -->
  <script type="text/javascript">
      tinymce.init({
          selector: 'textarea',
          menu: {
              file: { title: 'File', items: 'newdocument' },
              edit: { title: 'Edit', items: 'undo redo | cut copy paste pastetext | selectall' },
              insert: { title: 'Insert', items: 'link media | template hr' },
              view: { title: 'View', items: 'visualaid' },
              format: { title: 'Format', items: 'bold italic underline strikethrough superscript subscript | formats | removeformat' },
              table: { title: 'Table', items: 'inserttable tableprops deletetable | cell row column' },
              tools: { title: 'Tools', items: 'spellchecker code' },
              plantwatch: { title: 'My Menu', items: 'dataloop collectorloop processloop historian emailgroup alertgroup menusave' }
          },
          menubar : 'plantwatch file edit insert view format table tools',
          plugins: [
    'advlist autolink lists link image charmap print preview hr anchor pagebreak',
    'searchreplace wordcount visualblocks visualchars code fullscreen',
    'insertdatetime media nonbreaking save table contextmenu directionality',
    'emoticons template paste textcolor colorpicker textpattern imagetools codesample toc'
          ],
          toolbar1: 'undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
          toolbar2: 'forecolor backcolor emoticons | codesample',
          image_advtab: true,

          setup: function (editor) {
              editor.addMenuItem('dataloop', {
                  text: 'Data Loop',
                  onclick: function () {
                      editor.insertContent('{LOOP:Data}');
                  }
              });
              editor.addMenuItem('collectorloop', {
                  text: 'Collector Loop',
                  onclick: function () {
                      editor.insertContent('{LOOP:Collector}');
                  }
              });
              editor.addMenuItem('processloop', {
                  text: 'Process Loop',
                  onclick: function () {
                      editor.insertContent('{LOOP:Process}');
                  }
              });
              editor.addMenuItem('historian', {
                  text: 'Historian Server Name',
                  onclick: function () {
                      editor.insertContent('{HistorianServerName}');
                  }

              });
              editor.addMenuItem('emailgroup', {
                  text: 'Email Group Name',
                  onclick: function () {
                      editor.insertContent('{EmailGroupName}');
                  }

              });
              editor.addMenuItem('alertgroup', {
                  text: 'Alert Group Name',
                  onclick: function () {
                      editor.insertContent('{AlertGroupName}');
                  }
              });

          }

      });


  </script>
<!-- /TinyMCE -->

<body>
  <form method="post">
    <!-- Gets replaced with TinyMCE, remember HTML in a textarea should be encoded -->
      <textarea name="tinyMceEditor" cols="1" rows="1" style="width:100%; height: 78%"></textarea>
  </form>
</body>
</html>

然后,我的 Windows 窗体上有一个调用以下内容的按钮:

Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click MsgBox(tinyMceEditor.HtmlContent) 结束子

我本来希望返回内容,但它只返回函数本身的全文,如下:

function GetContent() {
        return tinyMCE.get('tinyMceEditor').getContent();
      }

当然,它以字符串形式返回,因为这就是我的代码构造它的方式。不过,我不确定为什么它不返回 HTML。

最佳答案

现在您只是引用 GetContent 函数,因此它返回定义,您需要调用它来获取结果。

更改:

将 html 变暗为 JSValue = webBrowserControl.ExecuteJavascriptWithResult("GetContent")

致:

Dim html As JSValue = webBrowserControl.ExecuteJavascriptWithResult("GetContent();‌​")

关于javascript - Awesomium - vb.net 获取 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41027389/

相关文章:

javascript - React 路由给出意外的 token 错误

javascript - 如何使用 Javascript 在字符串中出现另一个字符串标记之前插入一个字符串标记

c# - 使用必填字段验证器时如何清除控件?

javascript - 如何在没有额外换行符的情况下在 TinyMCE 中粘贴纯文本?

javascript - 在设计中 : How can I get paragraph contents with its characterStyles?

javascript - 多个 IP 的正则表达式,以逗号分隔,带或不带子网

.net - 如何从 VB.NET 打开 Outlook "New mail message"窗口

asp.net - 如果请求的输入为空,则 Request.Form 的结果

javascript - TinyMCE:如何删除我之前从段落中选择的 block 格式?

javascript - 使用 jquery 在 TinyMCE 文本区域中选择一个范围