问题
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.
- How can I get the class of a JSCValue without having to check it?
- Why is the current check not working?
- 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:
Looking at the API documentation, that doesn't currently seem possible. The jsc-glib API is fairly limited.
Because
result
is the array holding the object, so in JavaScript terms you're evaluating[body] instanceof HTMLBodyElement
and notbody instanceof HTMLBodyElement
.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, tojsc_value_object_get_property()
.
来源:https://stackoverflow.com/questions/58383168/how-to-access-a-jscvalue-objects-properties