语言

Menu
Sites
Language
How to execute JavaScript from Native C code? [Or what's wrong with below function?]

So I'm trying to execute javascript code from native app (in C) and below is the function that keeps failing:

static Evas_Object* load_html_file(Evas_Object* parent)
{
    Evas *e_webview = evas_object_evas_get(parent);
    Evas_Object *browser = ewk_view_add(e_webview);
    Ewk_Settings *settings = ewk_view_settings_get(browser);
    ewk_settings_javascript_enabled_set(settings, EINA_TRUE);


    char* javaScript =
            "function setWallpaperSuccess() {}"
            "function setWallpaperError() {}"
            "function test() {"
            "try"
            "      {"
            "  tizen.systemsetting.setProperty(\"HOME_SCREEN\", \"file:\/\/opt\/usr\/media\/Downloads\/2017-03-20_RiverofLife_1080x1920_no.jpg\", setWallpaperSuccess, setWallpaperError);"
            " }"
            "      catch (error)"
            "      {"
            "         console.log(\"Error: \" + error); return 'exceptionhappened';"
            " } return 'thisworkedorwhat';"
            "}"
            "test();";
    ewk_view_script_execute(browser, javaScript, JScallback, NULL);
    elm_win_resize_object_add(parent, browser);
    evas_object_show(parent);
    evas_object_show(browser);
    return browser;
}

static void JScallback(Evas_Object* o, const char* result_value, void* user_data) {
    dlog_print(DLOG_INFO, LOG_TAG, "[scriptCallback] return value: %s\n", result_value);
}

 

sdb dlog always prints:

I/    My-Experiment-App    (31448): [scriptCallback] return value: exceptionhappened

And these are privileges declared for native app (TizenManifest file):


    <privileges>
        <privilege>http://tizen.org/privilege/mediastorage</privilege>
        <privilege>http://tizen.org/privilege/network.get</privilege>
        <privilege>http://tizen.org/privilege/systemsettings</privilege>
        <privilege>http://tizen.org/privilege/internet</privilege>
        <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
        <privilege>http://tizen.org/privilege/externalstorage</privilege>
    </privileges>

 

Could anyone please help me know why is it failing?

Thanks a much and kind regards,

~Viren

 

编辑者为: Yo Shakya 24 3月, 2017

响应

1 回复
Shaswati Saha

Your Js code is executing without any doubt as it is giving exceptionhappened in the console. It indicates that, the try block is failing and that's why it's entering into the catch block. Please try with a simple console log in the try block instead of using  tizen.systemsetting.setProperty() function and also please write your Js code properly. To ensure that there's nothing wrong with the Js code execution, you may try executing simple Js code like in the following link:

https://developer.tizen.org/ko/community/code-snippet/embed/21273?langredirect=1