How can I read Javascript variable with Awesomium? c++

201 views Asked by At

I want to read a Javascript value from the page, but how?

    std::string jscript("[appdata]");
    JSValue value = view_->web_view()->ExecuteJavascriptWithResult(WSLit(jscript.c_str()), WSLit(""));
    JSArray& arr = value.ToArray();
    std::string appdata = arr[0].ToString;

When I try to use this I get Awesomium::JSValue::ToString: non-standard syntax; use '&' to create a pointer to member

tbh I have no idea where to put &

3

There are 3 answers

2
Mike Klimentiev On

My C++ is rather rusty but don't you need to add parentheses at the end?

std::string appdata = arr[0].ToString();

As it stands, the code seems to be trying to get a pointer to a member function ToString and assign it to std::string variable.

0
bugraarslan On

Well, here is the answer for the future wonderers:

    JSValue MyValue = view_->web_view()->ExecuteJavascriptWithResult(WSLit("appdata"), WSLit("")).ToString();
    WebString asd(MyValue.ToString());
    std::string appdata = ToString(asd);
0
Lab Hatter On

Try changing:

std::string appdata = arr[0].ToString;

to

Awesomium::WebString appdata = arr[0].ToString();

As Mike Klimentiev said, you're assigning the method ToString to the variable appdata. When you add the parentheses, it makes a function call rather than a straight assignment of the function to the variable.

Also, when you add the parentheses, the compiler error message is telling you that it cannot convert the type "Awesomium::WebString" to type "std::string". At the very beginning of the line you have std::string appdata This declares a variable appdata of type std::string. However the method ToString() returns a type of Awesomium::WebStringfor which there is no conversion to the type std::string

EDIT: @bugraarslan Based on experience, I would be wary of your workaround. There is no easy conversion between the two types, because they didn't write one. Usually, this means you can either use the type Awesomium::WebString or fight the library by using a familiar type namely, std::string. Libraries do not tire in their stubbornness, but do as you please

I don't have enough reputation to comment. So, I had to amend my answer instead.