Search code examples
javascriptc++webkitjavascriptcorewebkitgtk

How to access a JSCValue Object's properties


With the following code:

#include <webkit2/webkit-web-extension>
/* Skipping through a lot of code */
{
  JSCValue* result = jsc_context_evalutate(jsCtx, "document.getElementsByTagName('body')", -1);
  std::cout << jsc_value_to_string(jsc_value_object_get_property_at_index(result, 0)) << "\n";
  if (jsc_value_object_is_instance_of(result, "HTMLBodyElement"))
    std::cout << "Instance of HTMLBodyElement\n";
}

I get [object HTMLBodyElement] printed but not Instance of HTMLBodyElement. I have a few questions about this.

  1. How can I get the class of a JSCValue without having to check it?
  2. Why is the current check not working?
  3. How can I access other properties of the object? When I tried to increase the index, all I got was undefined and when I used jsc_value_object_enumerate_properties() I only got one address in memory. My goal is to access the CSS, Tag, ID/Class, parent elements, and children elements. I do not know how I can turn a char** into usable information.

Solution

  • Answers to your questions in order:

    1. Looking at the API documentation, that doesn't currently seem possible. The jsc-glib API is fairly limited.

    2. Because result is the array holding the object, so in JavaScript terms you're evaluating [body] instanceof HTMLBodyElement and not body instanceof HTMLBodyElement.

    3. Impossible to tell for sure without seeing your code, but it's likely you called jsc_value_object_enumerate_properties() on the array as well, and that array would only have one enumerable property, namely index 0. If you call it on the body element, then you should be able to get the value of each property by passing each string from the array of strings returned from that function, to jsc_value_object_get_property().