Question

I have created c++ bindings using Javascriptcore within webview, so that my c++ fns. and objects can be accessed from html. I have followed this tutorial to create these bindings. http://parmanoir.com/Taming_JavascriptCore_within_and_without_WebView

I have no problem in creating and using bindings till I reload the page. As soon as I call "location.reload()" in my html page, I lose all the bindings.

Sample html code is:

<html>

<head>
    <style type="text/css">
    body {
        background-color: transparent;
    }
    p {
        color: black;
        background-color: rgb(255, 0, 255);
        position: absolute;
        top: 0;
        left: 5;
        margin: 0;
        padding: 0;
    }
    </style>

    <script type="text/javascript" language="JavaScript1.4"><!--

    function testDummyModule()
    {
        if ( window.Dummy ) 
        {   
            console.log("*** Dummy ID: " + window.Dummy.id);

        }
        else 
        {
            console.log("[!] Dummy not supported.");

        }

    }

    function init()
    {
        console.log("Testing Dummy");
        testDummyModule();
        console.log("Finished testing Dummy");
        location.reload();

    }

    </script>
</head>
    <body onload="init();">
        <!--<p> This Page is loading without any problems</p>
        <br>-->
        <div id="test"></div>
        <div id="text">Test Page</div>
    </body>
</html>

When page is reloaded after call to location.reload, I get the error: "Dummy, not supported."

I tried, recreating the bindings(by deleting c++ classes that are creating bindigs and creating them again) as soon as I get setUrl call, but still I get the same error.

Any clues, how can I make the bindings available again

Was it helpful?

Solution

By reloading the page webkit receives a new 'global' context for that new page and your bindings must be added to this new context. There should be no need to delete the bindings and recreate them, just add them to this new context.

I'm not sure what your C++ code looks like but for instance I have just added a rebind function to my Javascript binding class that receives the new context from webkit and rebinds the "dummy" object.

void JsDummy::rebind(JSGlobalContextRef ctx)
{
    // m_jsDummyClassRef is a JSClassRef created and stored in the JsDummy class
    JSObjectRef JsDummyObjectRef = JSObjectMake(ctx, m_jsDummyClassRef, this);

    JSObjectRef globalObjectRef = JSContextGetGlobalObject(ctx);
    JSStringRef DummyName = JSStringCreateWithUTF8CString("Dummy");

    // Attach the JavaSript object "Dummy" to the new global context
    JSObjectSetProperty(ctx, globalObjectRef, DummyName, JsDummyObjectRef, kJSPropertyAttributeDontDelete, 0);
    JSStringRelease(DummyName);
}

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top