How to access a JSCValue Object's properties

◇◆丶佛笑我妖孽 提交于 2019-12-11 10:54:26

问题


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.

回答1:


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().



来源:https://stackoverflow.com/questions/58383168/how-to-access-a-jscvalue-objects-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!