1

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?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Mathlight
  • 6,436
  • 17
  • 62
  • 107
  • 1
    I used Awesomium in a project not so long ago and struggled to get content from pages. In the end I switched and used the HTML Agility Pack that can be found here: http://htmlagilitypack.codeplex.com/ I found it far easier to 'scrape' content from pages. – SteveB Feb 14 '14 at 16:25
  • @bhs, i've worked on an little project with htmlafility before. Don't know exactly how that went anymore. But i like awesomium because it's freaking fast. So i hope that there's an "simple" answer, or else i'll be back to the htmlafilitypack... – Mathlight Feb 14 '14 at 16:30
  • I used them both together - the app was a Windows wrapper around a payment gateway that used an Awesomium win browser component to display the page. – SteveB Feb 14 '14 at 16:32
  • @bhs, the problem is that the whole project is depending on an web session, so i think that i can't use both of them... If that was possible, i would be very happy :D – Mathlight Feb 14 '14 at 16:33

1 Answers1

4

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);
    }
}
Steve Jansen
  • 9,398
  • 2
  • 29
  • 34
  • EThis looks good to me. I'm now on my phone, but i will check this out tomorow. Thank you for the explanation – Mathlight Feb 15 '14 at 22:46
  • Thank you. It worked. There was only 1 thing. this line of code( and the other 2 wich are similar ) gave me an error: `if (row == null || `: `Ambiguous user defined conversions 'Awesomium.Core.JSValue.implicit operator Awesomium.Core.JSValue(string)' and 'Awesomium.Core.JSValue.implicit operator Awesomium.Core.JSValue(Awesomium.Core.JSObject)' when converting from '' to 'Awesomium.Core.JSValue'`. So after i deleted the `==null`, it worked. – Mathlight Feb 16 '14 at 07:18
  • Also, inside your `foreach cell`, You do another check with the if statement. I suppose you meant to check on `cell` instead of `row`? – Mathlight Feb 16 '14 at 07:20
  • @Mathlight - thanks, I updated the answer with these 3 fixes. – Steve Jansen Feb 16 '14 at 13:28