4

I'm in the process of replacing a .NET's WebBrowser for a WebKit.NET, and one of the functionalities I've been using in the WebBrowser that I can't figure out how to implement in the wrapper I'm writing around WebKit is the ability to pass DOM elements from and to JavaScript and C# code.

In a WebBrowser you'd just call InvokeScript and pass an the DomElement property of an HtmlElement as a parameter, and the JavaScript function would get the element. Same way, you can pass an element from JavaScript to C# and the C# code would get a DomElement object.

In WebKit as far as I know I cannot do this. The InvokeStriptMethod method is broken, but the StringByEvaluatingJavaScriptFromString is not, which let me inject scripts into the current document and invoke them any time later.

Note that these documents won't be my own created documents so I can't do stuff like assigning ID's to every element. This is a web scraper and I need to avoid messing with the document's elements as much as possible.

One hack that would probably work is assigning a temporary ID to the element and keeping the old ID backed up, then I can do a GetElementById call from either JavaScript or C# to get the element, and then reassign the old element's ID. The problem is that WebKit's GetAttribute('id') will give me an empty string when the ID is undefined, so there is no way for my C# code to know whether the ID was an empty string or undefined, and who knows, the current web page's script could rely on the ID being undefined for some reason, so setting the ID to an empty string could mess up their javascript.

Anyone has a better idea or is there any other WebKit method I could use?

svick
  • 236,525
  • 50
  • 385
  • 514
Ted Werley
  • 59
  • 2

1 Answers1

0

you can use this library https://github.com/douglascrockford/JSON-js and use this code

var json_text = JSON.stringify(your_object, null, 2);

your object is now a string and you can do this to make your string an object

var your_object = JSON.parse(json_text);

you can pass your string from javascript to c sharp.

in csharp you can use fastjason and

var o = fastJSON.JSON.Instance.ToObject<List<Retclass>>(s); // return a generic list

and

string str = fastJSON.JSON.Instance.ToJSON(obj, 
                 new fastJSON.JSONParamters { EnableAnonymousTypes = true }); // using the parameters

You can do that using JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

http://www.json.org/

http://www.codeproject.com/Articles/159450/fastJSON

rodolfoprado
  • 126
  • 7