遍历Awesomium JSObject [英] Loop through Awesomium JSObject

查看:109
本文介绍了遍历Awesomium JSObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带有Awesomium Web浏览器的C#Windows Form应用程序.

我正在尝试从表中获取一些行并将其解析为数组. JSPart在浏览器内部运行良好.

这是我在C#中使用的代码:

JSObject villageRows = view.ExecuteJavascriptWithResult("document.getElementById(\"production_table\").getElementsByTagName(\"tbody\")[0].getElementsByTagName(\"tr\");");
if (villageRows == null)
{
    return;
}

现在在Chrome中返回2个tr行,但是以后会更多,所以我希望可以使用foreach遍历元素,但找不到任何遍历它的方法./p>

有人知道吗?

解决方案

我将在Javascript中使用匿名函数来解析表并将内容作为字符串数组的数组返回.这将更易于在C#中进行解析.

有关解析以下内容的示例,请参见 http://jsfiddle.net/stevejansen/xDZQP/. Javascript中的表格. (旁注:我将检查您的数据源是否提供了REST API或类似的东西来访问此数据;解析HTML确实很脆弱.)

这大致就是我将C#和JS结合起来解决您的问题的方式(未经测试的C#).请注意,您为IWebView.ExecuteJavascriptWithResult使用了错误的返回类型.

 const string JAVASCRIPT = @"(function () {
  var table = document.getElementById('production_table'),
      records = [];

  if (table == null) return;

  table = table.getElementsByTagName('tbody');

  if (table == null || table.length === 0) return;

  // there should only be one tbody element in a table
  table = table[0];

  // getElementsByTagName returns a NodeList instead of an Array
  // but we can still use Array#forEach on it
  Array.prototype.forEach.call(table.getElementsByTagName('tr'),

  function (row) {
     var record = [];
      Array.prototype.forEach.call(row.getElementsByTagName('td'),
      function (cell) {
        record.push(cell.innerText);
      });
      records.push(record);
  });

  return records;
})();";

JSValue result = view.ExecuteJavascriptWithResult(JAVASCRIPT);
JSValue[] records;
JSValue[] record;

if (result.IsNull || !result.IsArray)
    return;

records = (JSValue[])result;

foreach(JSValue row in records) 
{
    if (row == null || row.IsNull || !row.IsArray)
      continue;

    record = (JSValue[])row;

    foreach(JSValue cell in record) 
    {
      if (cell.IsNull || !cell.IsString)
        continue;
      System.Diagnostics.Debug.WriteLine((string)cell);
    }
}
 

I'm making an C# Windows Form Application with an Awesomium webbrowser inside it.

I'm trying to get some rows from an table and parse them to an array. The JSPart is running inside the browser fine.

Here's the code that i use inside C#:

JSObject villageRows = view.ExecuteJavascriptWithResult("document.getElementById(\"production_table\").getElementsByTagName(\"tbody\")[0].getElementsByTagName(\"tr\");");
if (villageRows == null)
{
    return;
}

That returns now 2 tr rows inside Chrome, but that will be more later, so i was hoping that i could loop through the elements with an foreach, but i can't find any way to loop through it.

Does anybody got any idea?

解决方案

I would use an anonymous function in Javascript to parse the table and return the contents as an array of array of strings. This will be easier to parse in C#.

See http://jsfiddle.net/stevejansen/xDZQP/ for an example of parsing a table in Javascript. (Sidenote: I would check to see if your data source provides a REST API or similar to access this data; parsing HTML is really fragile.)

This is roughly how I would combine C# and JS to solve your problem (the C# is untested). Note you were using the wrong return type for IWebView.ExecuteJavascriptWithResult.

const string JAVASCRIPT = @"(function () {
  var table = document.getElementById('production_table'),
      records = [];

  if (table == null) return;

  table = table.getElementsByTagName('tbody');

  if (table == null || table.length === 0) return;

  // there should only be one tbody element in a table
  table = table[0];

  // getElementsByTagName returns a NodeList instead of an Array
  // but we can still use Array#forEach on it
  Array.prototype.forEach.call(table.getElementsByTagName('tr'),

  function (row) {
     var record = [];
      Array.prototype.forEach.call(row.getElementsByTagName('td'),
      function (cell) {
        record.push(cell.innerText);
      });
      records.push(record);
  });

  return records;
})();";

JSValue result = view.ExecuteJavascriptWithResult(JAVASCRIPT);
JSValue[] records;
JSValue[] record;

if (result.IsNull || !result.IsArray)
    return;

records = (JSValue[])result;

foreach(JSValue row in records) 
{
    if (row == null || row.IsNull || !row.IsArray)
      continue;

    record = (JSValue[])row;

    foreach(JSValue cell in record) 
    {
      if (cell.IsNull || !cell.IsString)
        continue;
      System.Diagnostics.Debug.WriteLine((string)cell);
    }
}

这篇关于遍历Awesomium JSObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆